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.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.