It's impossible to add associate products (other posts) during the initial import due to the fact that while you are importing the first product into WordPress, it's associated parts have not yet been created. For this reason I had to run two imports of the spreadsheet into WordPress.
On the first import, it created all the posts and added all of the non-relational custom fields as they appeared in the spreadsheet. I setup the script the print the post ID's of the imported products in a table format that allowed me to easily copy and paste all of the ID's at once from the script output back into a "Product ID" field in the spreadsheet. The script checks for the existence of a product ID and this field is what will determine if it is creating a new post/product or updating an existing one.
Once all the products had been imported and their respect ID's were added to the spreadsheet, we began the second import. We used the exact same spreadsheet with the only difference being that now it had the WordPress ID's added to a column.
1. if($product['Associated Parts']):
2. $assParts = explode('|',$product['Associated Parts']);
3. unset($assIDs);
4. foreach($assParts as $assPart):
5. unset($assID);
6. if($assPart):
7. $assID = get_page_by_title( $assPart , 'OBJECT' , 'product' );
8. $assIDs[] = $assID->ID;
9. endif;
10. endforeach;
11. $assIDs = array_filter($assIDs);
12. update_field( 'field_542c5dc44272e' , $assIDs , $product['Page ID'] );
13. endif;
Breakdown of the code line by line:
- I check to see if this product has any associated parts assigned to it.
- I convert the pipe separated list of associated parts into an array.
- I unset the array that we will be using to store the array of post ID's. I do this so that the associated parts from the first product are not still stored in the array when we begin processing the second product.
- I begin looping through the array of associated parts. Each item in this array contains the title of the associated part. In order for this to work, the titles must be spelled, formatted and in all other ways identical to the title of the WordPress post.
- Same as 3, only now it's for this particular associated part.
- This line was added to ensure that I wasn't searching for an associated part if that item of the array was an empty string.
- I retrieve the WordPress post item that matches the title of this particular associated item.
- I add the ID of the associated product to a numeric array.
- Close the if statement on line 6.
- End the associated products loop.
- Filter out any empty strings or null values from our array of post ID's.
- We pass the the numeric array containing the post ID's into the Advanced Custom Fields "update_field()" function. The first parameter is the unique field key of the Post Object custom field. The second parameter is the array of associated products ID's. The third parameter is the ID of the product we are editing.
- Close the if statement on line 1.
I had to guess as to the format I needed to use for the value of the field. At first, I tried passing in an array of Post Objects and that didn't work. In the WordPress post editor, I inspected the code and saw that the value of their input field (when another post was selected) was that posts ID. After switching to a numeric array containing the Posts' ID's, the update_field() function accepted them perfectly without incident.
We're done.