Support

Account

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

  • 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 );
        }
      }
    }
    ?>