Support

Account

Home Forums Add-ons Repeater Field I need help populating repeater field with WP All Import

Solved

I need help populating repeater field with WP All Import

  • Hi,

    I want to populate my repeater field with data from an excel sheet using the WP All Import plugin.
    My repeater field name is ‘car_make_model_year’
    The repeater field has 4 sub-fields:
    car_make
    car_model
    car_year
    car_type

    I came across this https://github.com/KittenCodes/CodeSnippets/issues/73

    And tried to write the code in order to populate all the fields in the repeater field.
    After I run the import using the WP All Import I am facing an issue.
    New rows are created but there are no values in the fields.

    My code looks like this:

    add_action( 'pmxi_saved_post', 'soflyy_add_data', 10, 3 );
    
    function soflyy_add_data( $id, $xml, $update ) {
      $selector = 'car_make_model_year'; // Parent field name
      $subfield1 = 'car_make'; // The repeating field you want to add the first value to
      $subfield2 = 'car_model'; // The repeating field you want to add the second value to
      $subfield3 = 'car_year'; // The repeating field you want to add the third value to
      $subfield4 = 'car_type'; // The repeating field you want to add the forth value to
    
      if ( $value1 = get_post_meta( $id, 'car_make', true )) {
    	  $value2 = get_post_meta( $id, 'car_model', true );
    	  $value3 = get_post_meta( $id, 'car_year', true );
    	  $value4 = get_post_meta( $id, 'car_type', true );
        $row = array( $subfield1 => $value1, $subfield2 => $value2, $subfield3 => $value3, $subfield4 => $value4,  );
        add_row( $selector, $row, $id );
      }
    
      delete_post_meta( $id, 'car_make' );
      delete_post_meta( $id, 'car_model' );
      delete_post_meta( $id, 'car_year' );
      delete_post_meta( $id, 'car_type' );
    }

    1. I really do not understand how is it going to get the values from my excel sheet.
    2. Does anyone knows how to modify this in order to make it work?

    My field data on excel sheet are named like this:

    $brand = car_make
    $model = car_model
    $year = car_year
    $type = car_type

    Thank you in advance!

  • After reading tons of documentation I came up with this:

    add_action( 'pmxi_saved_post', 'soflyy_add_data', 10, 3 );
    
    function soflyy_add_data( $id, $xml, $update ) {
      $selector = 'car_make_model_year'; // Parent field name
      $subfield1 = 'car_make'; // The repeating field you want to add the first value to
      $subfield2 = 'car_model'; // The repeating field you want to add the second value to
      $subfield3 = 'car_year'; // The repeating field you want to add the third value to
      $subfield4 = 'car_type'; // The repeating field you want to add the forth value to
    
      if ( $value1 = get_post_meta( $id, 'my_repeater_data_car_make', true )) {
    	  $value2 = get_post_meta( $id, 'my_repeater_data_car_model', true );
    	  $value3 = get_post_meta( $id, 'my_repeater_data_car_year', true );
    	  $value4 = get_post_meta( $id, 'my_repeater_data_car_type', true );
        $row = array( $subfield1 => $value1, $subfield2 => $value2, $subfield3 => $value3, $subfield4 => $value4,  );
        add_row( $selector, $row, $id );
      }
    
      delete_post_meta( $id, 'my_repeater_data_car_make' );
      delete_post_meta( $id, 'my_repeater_data_car_model' );
      delete_post_meta( $id, 'my_repeater_data_car_year' );
      delete_post_meta( $id, 'my_repeater_data_car_type' );
    }

    and it looks like it works.
    I do not know if this the proper syntax or the best way to do it.

  • WP all import pro has an addon that works with ACF fields. I can’t tell if what you’re doing is right or not because I don’t know how wpai is actually importing the data in the first place.

  • The solution that I am posting actually works for me!
    I will post more info about just for anyone that might need it in the future.

    WP All Import cannot import multiply values to a ACF Repeater field if the values are on multiply rows on the xml sheet.

    As the WP All Import authors suggest there is a workaround for that. You can find it here https://www.wpallimport.com/documentation/developers/code-snippets/#append-acf-repeater-data.

    Unfortunately, the above workaround works only if there is one sub-field in the Repeater field.

    So, for my case where the sub-fields are 4, I need to work on another solution.
    The solution is quite simple if you understand the workaround. You need to create a custom field for each ACF Repeater sub-field.

    For my example my 4 subfields are car_make, car_model, car_year and car_type (you need to use field name as shown on ACF on WP admin panel). So I need to create 4 different custom fields, one for each ACF Repeater sub-field.

    I created the my_repeater_data_car_make, my_repeater_data_car_model, my_repeater_data_car_year and my_repeater_data_car_type.

    Then on the import process I used the custom fields option in order to link the data from my xml to the custom fields, please check here https://prnt.sc/19ray48

    Then I just added the below code in the functions editor.

    add_action( 'pmxi_saved_post', 'soflyy_add_data', 10, 3 );
    
    function soflyy_add_data( $id, $xml, $update ) {
      $selector = 'car_make_model_year'; // Parent field name
      $subfield1 = 'car_make'; // The repeating field you want to add the first value to
      $subfield2 = 'car_model'; // The repeating field you want to add the second value to
      $subfield3 = 'car_year'; // The repeating field you want to add the third value to
      $subfield4 = 'car_type'; // The repeating field you want to add the forth value to
    
      if ( $value1 = get_post_meta( $id, 'my_repeater_data_car_make', true )) {
    	  $value2 = get_post_meta( $id, 'my_repeater_data_car_model', true );
    	  $value3 = get_post_meta( $id, 'my_repeater_data_car_year', true );
    	  $value4 = get_post_meta( $id, 'my_repeater_data_car_type', true );
        $row = array( $subfield1 => $value1, $subfield2 => $value2, $subfield3 => $value3, $subfield4 => $value4,  );
        add_row( $selector, $row, $id );
      }
    
      delete_post_meta( $id, 'my_repeater_data_car_make' );
      delete_post_meta( $id, 'my_repeater_data_car_model' );
      delete_post_meta( $id, 'my_repeater_data_car_year' );
      delete_post_meta( $id, 'my_repeater_data_car_type' );
    }

    Run the import and that’s all.

  • Just to add to this, wp all import was successfully adding the repeater fields but it was also allowing any duplicate data from my import.

    I was also unable to update the csv/xml for example if i had new rows in the csv/xml. The import result was adding more rows of the same data to the product / post.

    I managed to use the code below to fix all of this:

    1. Allow wp all import to detect my repeater fields and identify if the row was duplicate which results in duplicates not being added
    2. The next part of the code allows wp all import to then update with new data from the csv and only add or change data and not add exact carbon copy data from any previous imports
    <?php
    add_action( 'pmxi_saved_post', 'soflyy_add_data', 10, 3 );
    
    function soflyy_add_data( $id, $xml, $update ) {
      $selector = 'price_comparison'; // Parent field name
      $subfield1 = 'price'; // The repeating field you want to add the first value to
      $subfield2 = 'delivery'; // The repeating field you want to add the second value to
      $subfield3 = 'website'; // The repeating field you want to add the third value to
      $subfield4 = 'company_name'; // The repeating field you want to add the forth value to
      $subfield5 = 'logo'; // The repeating field you want to add the forth value to
    
      if ( $value1 = get_post_meta( $id, 'my_repeater_data_price', true )) {
        $value2 = get_post_meta( $id, 'my_repeater_data_delivery', true );
        $value3 = get_post_meta( $id, 'my_repeater_data_website', true );
        $value4 = get_post_meta( $id, 'my_repeater_data_company_name', true );
        $value5 = get_post_meta( $id, 'my_repeater_data_logo', true );
        
        $repeating_fields = array($value1, $value2, $value3, $value4, $value5);
        $unique_fields = array_unique($repeating_fields);
        
        if (count($repeating_fields) !== count($unique_fields)) {
          // There are duplicates, so delete the existing row
          $rows = get_field($selector, $id);
          foreach ($rows as $row) {
            if ($row[$subfield1] === $value1 && $row[$subfield2] === $value2 && $row[$subfield3] === $value3 && $row[$subfield4] === $value4 && $row[$subfield5] === $value5) {
              delete_row($selector, $row['id']);
            }
          }
        }
    
        // Check if the row already exists in the repeating field
        $rows = get_field($selector, $id);
        $row_exists = false;
        foreach ($rows as $row) {
          if ($row[$subfield1] === $value1 && $row[$subfield2] === $value2 && $row[$subfield3] === $value3 && $row[$subfield4] === $value4 && $row[$subfield5] === $value5) {
            $row_exists = true;
            break;
          }
        }
    
        // Add the new row to the repeating field if it doesn't already exist
        if (!$row_exists) {
          $row = array( $subfield1 => $value1, $subfield2 => $value2, $subfield3 => $value3, $subfield4 => $value4, $subfield5 => $value5 );
          add_row( $selector, $row, $id );
        }
      }
    }
    ?>
    
  • hi all,
    i am doing this now for days and finally it worked out with ACF repeater field called “galleryx” which holds two ACF fields

    1. “gallery_tags” a ACF Taxonomy field (return value term id)
    2. “gallery_images” a ACF image field (return value url)

    All work fine when done manyually on my Custom Post Typ “projekte”…also when inmporting as defined by you guys for example one projects adds up 8 image rows..all other project data is also fine if couting their proper rows…also the data seems to be saved like “tagname, tagname”, “imagefile-name” and so on. but just the image is emplty and tags are not checked..i think because its not converted into the taxonomies needed object / serialization etc.

    this i my function:

    <?php
    add_action( 'pmxi_saved_post', 'soflyy_add_data', 10, 3 );
    
    function soflyy_add_data( $id, $xml, $update ) {
    
        // Parent field name.
        $selector = 'galleryx';
    
        // The field to be appended.
        $subfield1 = 'gallery_image';
    	$subfield2 = 'gallery_tags'; 
        
        // Only continue if my_repeater_data contains a value.
        if ( $value1 = get_post_meta( $id, 'my_repeater_data_gallery_image', true ) ) {
            $value2 = get_post_meta( $id, 'my_repeater_data_gallery_tags', true );
    			
            // Format data for repeater.
            $row = array( $subfield1 => $value1, $subfield2 => $value2 );
    
            // Add new repeater row.
            add_row( $selector, $row, $id );
    
        }
        delete_post_meta( $id, 'my_repeater_data_gallery_image' );
    	delete_post_meta( $id, 'my_repeater_data_gallery_tags' );
    }
    ?>

    what do you think?
    thanx so much
    timo

  • Hey all,

    I can’t seem to get the solution from @bigdropgr to work. I have an CSV with four columns (store, name, scm, points), a repeater named ‘leaderboard’ with subfields named ‘store’, ‘name’, ‘scm’ and ‘points’. I set up dummy custom fields as suggested by @bigdropgr and mapped them to the fields in the CSV (screenshot). Then I pasted this into the function editor:

    <?php 
    
    add_action( 'pmxi_saved_post', 'soflyy_add_data', 10, 3 );
    
    function soflyy_add_data( $id, $xml, $update ) {
      $selector = 'leaderboard'; // Parent field name
      $subfield1 = 'store'; // The repeating field you want to add the first value to
      $subfield2 = 'name'; // The repeating field you want to add the second value to
      $subfield3 = 'scm'; // The repeating field you want to add the third value to
      $subfield4 = 'points'; // The repeating field you want to add the forth value to
    
      if ( $value1 = get_post_meta( $id, 'my_repeater_data_store', true )) {
    	$value2 = get_post_meta( $id, 'my_repeater_data_name', true );
    	$value3 = get_post_meta( $id, 'my_repeater_data_scm', true );
    	$value4 = get_post_meta( $id, 'my_repeater_data_points', true );
        $row = array( $subfield1 => $value1, $subfield2 => $value2, $subfield3 => $value3, $subfield4 => $value4 );
        add_row( $selector, $row, $id );
      }
    
      delete_post_meta( $id, 'my_repeater_data_store' );
      delete_post_meta( $id, 'my_repeater_data_name' );
      delete_post_meta( $id, 'my_repeater_data_scm' );
      delete_post_meta( $id, 'my_repeater_data_points' );
    }
    ?>

    I am using the Advanced Custom Fields Add-on, but from what I understand, I don’t need to set anything here since it’s being handled in the Custom Fields area. In the settings area, I’m just updating the ‘leadership’ custom field (screenshot). I’m trying to get this import to populate a repeater on a Page. But it seems to be failing somewhere. Can anyone point me in the right direction? Thanks in advance! Also including this snippet from the import log as it may be helpful.

    [03:53:16] UPDATINGLeaderboardPage`
    [03:53:16] Associate post Leaderboard with current import …
    [03:53:16] CUSTOM FIELDS:
    [03:53:16] ACF ADD-ON:
    [03:53:16] – Custom field my_repeater_data_store is not set to be updated in import settings, skipping …
    [03:53:16] – Custom field my_repeater_data_name is not set to be updated in import settings, skipping …
    [03:53:16] – Custom field my_repeater_data_scm is not set to be updated in import settings, skipping …
    [03:53:16] – Custom field my_repeater_data_points is not set to be updated in import settings, skipping …
    [03:53:16] IMAGES:
    [03:53:16] UPDATED Leaderboard Page (ID: 17)
    [03:53:16] ACTION: pmxi_saved_post
    [03:53:16] ACTION: pmxi_after_post_import
    [03:53:16] —`

  • Hey all,

    I can’t seem to get the solution from @bigdropgr to work. I have an CSV with four columns (store, name, scm, points), a repeater named ‘leaderboard’ with subfields named ‘store’, ‘name’, ‘scm’ and ‘points’. I set up dummy custom fields as suggested by @bigdropgr and mapped them to the fields in the CSV (screenshot). Then I pasted this into the function editor:

    <?php 
    
    add_action( 'pmxi_saved_post', 'soflyy_add_data', 10, 3 );
    
    function soflyy_add_data( $id, $xml, $update ) {
      $selector = 'leaderboard'; // Parent field name
      $subfield1 = 'store'; // The repeating field you want to add the first value to
      $subfield2 = 'name'; // The repeating field you want to add the second value to
      $subfield3 = 'scm'; // The repeating field you want to add the third value to
      $subfield4 = 'points'; // The repeating field you want to add the forth value to
    
      if ( $value1 = get_post_meta( $id, 'my_repeater_data_store', true )) {
    	$value2 = get_post_meta( $id, 'my_repeater_data_name', true );
    	$value3 = get_post_meta( $id, 'my_repeater_data_scm', true );
    	$value4 = get_post_meta( $id, 'my_repeater_data_points', true );
        $row = array( $subfield1 => $value1, $subfield2 => $value2, $subfield3 => $value3, $subfield4 => $value4 );
        add_row( $selector, $row, $id );
      }
    
      delete_post_meta( $id, 'my_repeater_data_store' );
      delete_post_meta( $id, 'my_repeater_data_name' );
      delete_post_meta( $id, 'my_repeater_data_scm' );
      delete_post_meta( $id, 'my_repeater_data_points' );
    }
    ?>

    I am using the Advanced Custom Fields Add-on, but from what I understand, I don’t need to set anything here since it’s being handled in the Custom Fields area. In the settings area, I’m just updating the ‘leadership’ custom field (screenshot). Ideally, these fields will populate a repeater on a Page. Can anyone point me in the right direction here? Any help would be appreciated. Thanks!

    Also including this snippet from the import log, as it may be helpful.

    [03:53:16] UPDATINGLeaderboardPage`
    [03:53:16] Associate post Leaderboard with current import …
    [03:53:16] CUSTOM FIELDS:
    [03:53:16] ACF ADD-ON:
    [03:53:16] – Custom field my_repeater_data_store is not set to be updated in import settings, skipping …
    [03:53:16] – Custom field my_repeater_data_name is not set to be updated in import settings, skipping …
    [03:53:16] – Custom field my_repeater_data_scm is not set to be updated in import settings, skipping …
    [03:53:16] – Custom field my_repeater_data_points is not set to be updated in import settings, skipping …
    [03:53:16] IMAGES:
    [03:53:16] UPDATED Leaderboard Page (ID: 17)
    [03:53:16] ACTION: pmxi_saved_post
    [03:53:16] ACTION: pmxi_after_post_import
    [03:53:16] —`

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.