I am using several plugins with woocommerce/wordpress to make it so that each of my product variations will show up in my product list as separate products, but only one of each color is set to display (so if i have a product with 3 sizes and 3 colors, there are 9 products, but since only one of each color is visible, only 3 products show up in the list.). I wrote a script that would transfer the visibility settings to the next product of that same color once the currently visible product went out of stock.
The problem I'm having is even though everything looks right, the product isn't showing in the products list until i go in on the admin side, edit the product, change the visibility settings to hidden and then back to display (so that it lets me hit the save button, nothing actually changes from when the page loaded though), and then hit save and it shows up as its supposed to.
So I must be missing something in the database that is being queried but I cant tell what, both the posts and post_meta tables look identical before and after hitting save on the admin side. The only field that changed is the _edit_lock
field in the post_meta table.
Here is part of the script I wrote to transfer visibility settings, I originally had this issue and found it was because the product was marked out of stock still, so i added that line in the end and it seemed to be working, but now something else is causing it:
...
// $child_id is the product that the visibility settings are being transferred to, it should at this point be hidden
// $visibility is the settings of the product that was visibile
// swap visibility settings of the products
$child_visibility = get_post_meta($child_id,"_visibility",true);
update_post_meta($product->get_id(),"_visibility",$child_visibility);
update_post_meta($child_id,"_visibility",$visibility);
// copy color taxonomies over in case they were not entered
// this saved time so that the admin only had to enter taxonomy information on the visible products
$terms = get_the_terms( $product->get_id(), 'product_color');
$termArray = array();
foreach($terms as $term) {
$termArray[] = $term->name;
}
wp_set_object_terms( $child_id, $termArray, 'product_color', false );
// i dont remember what this was for
delete_transient( 'jck_wssv_term_counts' );
// make sure new item is not marked out of stock
wp_remove_object_terms( $child_id, 'outofstock', 'product_visibility' );
wp_remove_object_terms( $child_id, 'exclude-from-catalog', 'product_visibility' );