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:
<?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 );
}
}
}
?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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.