2

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' );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
JStephen
  • 1,059
  • 3
  • 13
  • 28

1 Answers1

6

Since Woocommerce 3 the products visibility is now handled by the 'product_visibility' custom taxonomy for the terms 'exclude-from-catalog' and 'exclude-from-search'… See this thread or this one too.

So you should use instead the WC_Product CRUD setter methods set_catalog_visibility() this way:

// Get an instance of the product variation from a defined ID
$child_product = wc_get_product(child_id);
// Change the product visibility
$child_product->set_catalog_visibility('visible');
// Save and sync the product visibility
$child_product->save();

This will also update the transients cached data, and will do the trick...

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks, this fixed it. I also added `$product->set_catalog_visibility('hidden') ` and saved to hide the previously visible product. – JStephen Jun 01 '18 at 14:35
  • hi, is there a way to add more options to the visibility feature? like we can do with product status? – Temani Afif Oct 08 '20 at 11:28
  • @TemaniAfif How that? The only options available for this method are: 'hidden', 'visible', 'search' and 'catalog'. – LoicTheAztec Oct 08 '20 at 11:33
  • Yes but I am wondering if I can register more options to have more avaiable like we can do with product status (https://jilt.com/blog/woocommerce-custom-order-status-2/). Not related to this question by the way – Temani Afif Oct 08 '20 at 11:35
  • @TemaniAfif I have never tried to do that… It's up to you to see what you can do about. – LoicTheAztec Oct 08 '20 at 11:37
  • @JStephen That is exactly what I did, because simply setting it to 'visible' (even though it was visible before as well) and saving it didn't seem to work. However, today I've noticed it doesn't work as expected. Somehow, some products disappears from my store because of those lines (300 out of 2000) and I am unable to select them, because they are stored as 'visible' in the database. I have to re-save every single product in my store now. I don't recommend using this solution on an automatic base. – Kristián Filo Jan 27 '21 at 14:10