Home › Forums › General Issues › ACF wp all import add on – How to delete?
Hi All.
I am posting out to see if i can help anybody else by showing my code as well as asking for some help if possible.
I am looking for somebody to help me with the code below to be able to delete ACF fields as well as update them.
I am really felt let down by what is in general a great plugin (wp all import) as they have sold an add on for ACF which is think is really a bit poor.
1. First of the wp all import ACF add on does not allow importing by unique rows, but rather by collated row data.
2. Once the data is imported, you can not update these fields as a standard feature of the add-on without specific coding capabilities in which wp all import suggest that you to hire a developer.
3. You can not delete fields either, again, you are suggested that you hire a developer.
i really think these are very basic requirements of the ACF add on they have developed and im pretty shocked that these aren’t included in what i feel are very basic functions – for me its like buying a car and being told that you need to buy the wheels and doors afterwards.
Anyhow after some help (which was reluctant) from wp all import, and some help on here i have managed to get my repeater fields to import through wp all import and update the fields based on a condition if value 3 is found, update.
Here is my code:
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 fifth 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
);
$myrow = array(
$subfield1 => $value1,
$subfield2 => $value2,
$subfield3 => $value3,
$subfield4 => $value4,
$subfield5 => $value5
);
$update = true;
$rows = get_field($selector, $id);
foreach ($rows as $index =>$row) {
if ($row[$subfield3] === $value3) {
update_row($selector, $index + 1, $myrow, $id);
$update = false;
break;
}
}
if($update){
add_row($selector, $myrow, $id);
}
}
}
If anybody knows or can help me delete the row if they no longer exist, that would be amazing.
I’m not sure I understand you code. It appears that only one row is being input, how do you determine in your import that a value should be removed?
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.
That does not help me at all.
It appears that you are importing using WPAI from a single column for each field in its own column that represents one sub field in the repeater. The you are using these values to update the one row of the repeater that matches the values in the import?
What about the other rows in the repeater? How to you indicate in the import the other rows? How to you indicate in the import what should be removed?
Somehow you need to compare what is imported against what already exists in the repeater. This means that you still need to import the entire repeater and not just a single value.
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 🙂
So your filter basically runs multiple times for each product post for each row of the import that matches that post.
There is no way to do this in a single function call.
You will need to track a list of every row that is imported and this list of data will need to be updated each time your filter is run.
Do to the way that WPAI works, this information will need to be stored in a session variable because WPAI may or may not import every row in a single request and this is the only way to compile a list of everything during a single import.
An alternative session to this would be two write all of the data out to a temporary file on the server and then load updated and re-save that file every time your filter runs, basically duplicating what a PHP session does.
The data could be as simple as an array something like
$data = array(
$post_id => (
// list of row indexes updated or added
)
);
With this compiled list of data created you would then add a WPAI pmxi_after_xml_import action. This runs after the import is complete and then in this action you can look at your compiled list of rows that were imported and compare them to the rows for each post ID and then delete the missing ones. Basically to do this you would get the repeater for each post again and then look to see what rows should be kept and delete the rest.
On top of this you also need to keep in mind that when deleting rows from an ACF repeater you must go in descending index order to make sure you delete the correct rows. For example let’s say that you need to delete rows 1 & 2. If you delete 1 first, then 2 becomes 1 and 3 becomes 2 and when you attempt to delete row 2 you’ll actually be deleting what was row 3.
To make matters worse, none of this may work. This solution would not be scalable. Updating many products, hundreds or thousands, would likely cause a server timeout during the delete process unless certain precautions are taken. This would depend on your hosting environment and what your hosting company allows, like extending the amount of time the server is allowed to process and possibly preventing the browser from timing on the AJAX request. The latter may not even be possible.
Overall this is several hours or more of R&D work and you’ll need to find a developer if you can’t do it. You might want to look into hiring a freelancer.
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.
Well, I can see where you’re coming from, but WPAI, like every other import plugin for WP is designed to work on 1 row of the import file equals 1 post in WP. I’m not surprised that they consider multiple rows into a single post out of scope.
WPAI is able to populate the entire repeater but this requires having all of the rows and sub fields in a single cell of the import. I have done this and yes, it can be a lot of work to put the data into the cell. When I did this for the client I set up a spreadsheet for them, and I only remember the basics, it was a long time ago. I created an excel spreadsheet that had 2 sheets in it. In one sheet they added the product ID field and a line for each row of the repeater and in the sheet that was imported there was a function built that would build the content for the cell based on the product ID and the data entered into the other sheet. It was extremely complicated, more-so than just entering the data, but it ensured that the client could not enter the data for the repeaters incorrectly.
You must be logged in to reply to this topic.
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.