Hi John.
I really appreciate the time you have taken to explain this.
As you have have highlighted, I will need a developer to work with me on this. Which is a little annoying as i am almost there with what I need the function to do!
I just personally find the wp all import team a little disappointing to not cater for what I feel is a very basic need within the add-on that they have built.
The entire idea of wp all import is to bulk import data. If they sell an add-on to further this for a specific plugin like ACF, then it is unfathomable to me why such a basic update and deletion ability were not incorporated as standard features into this add-on.
Hi John.
Sorry, I’m no php developer which is an issue of course. I’m sort of muddling my way through it, hence why I’m here.
I have a csv for example of 1000 rows. I have a acf repeater (price_comparison) with 5 subfields.
Each row from the csv represents a row for the repeater subfields.
On import of the csv, the result is a table of data which has rows associated by the sku.
Basically a price comparison table.
Each rows has an sku so it knows which product to import to.
When the csv is ran again, the function above updates the subfield “price” if it has changed.
It also checks against subfield “website” to make sure that if there is new data found under the field “website”, which in this case is a new url, then add the new rows data to the product/sku.
The rows that import can never have the same url, so it stops duplicate rows from importing the same url.
What I want the code to do is to check to see if the data exists in the import still and if it doesn’t exist, to delete it.
So basically on update, check price is the same, if price is the same by matching it to “website” then ignore, if it’s changed then update the price.
Never allow a duplicate url (website).
Check if the row still exists and if it doesn’t remove it.
The last part of deletion is the only part it currently does not do with the code above, and yes I am aware that the code above does not contain the delete request as this is the part I’m struggling with.
I am sorry if this doesn’t make sense, as I’ve said. Front end and seo are my areas of expertise , I’ve managed to muddle my way to this stage so I’m not doing too badly 🙂
Hi John.
This code doesn’t ask for the deletion.
I’ve tried 20 different ways of asking this code to delete the row if not in the import file but I can’t get it to work.
The above code is what I have so far which does everything I need it to apart from delete the row, either if it’s no longer present in the file or has been updated.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.