0
votes

I have a feature on my store that allows me to auto-generate a product quote, after the quote's attributes, prices, and shipping rules, I create a new product on the fly.

I don't want these products to show on my catalogue or searches, so I've been trying to figure out how to update the "Catalog Visibility" to Hidden.

At first I was trying:

update_post_meta( $prod_ID, '_visibility', 'hidden' );

The meta is added, however, looking into it I found out that WooCommerce changed the way it specifies if a product is visible or hidden:: below is from WooCommerce

Product visibility is taxonomy based instead of meta based 3.0.0 introduces a new product visibility taxonomy; catalogue, search, hidden, featured, out of stock are terms. These are set to upgrade and help filter products in the catalogue during frontend queries.

My issue is; how can I modify a taxonomy via PHP.

1
how are you creating your product on a fly?Reigel
@Reigel using Ajax and $newProdID = wp_insert_post( $prod_array ) to create the product. Later i use the $newProdID to add all my objects and post meta neededJpv

1 Answers

2
votes

it should be something like this:

$terms = array( 'exclude-from-search', 'exclude-from-catalog' ); // for hidden..
wp_set_post_terms( $prod_ID, $terms, 'product_visibility', false );

you can get the idea here... woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php#L671