Reputation: 3863
How to set Catalog visibility hidden in woo-commerce WordPress programmatically?
Like its mentioned here :
https://docs.woothemes.com/document/catalog-visibility-options/
But i can't find any hook or hack, that how to do it in PHP.
Upvotes: 8
Views: 13769
Reputation: 51
To set a product visibility as hidden:
$product = wc_get_product($id);
$product->set_catalog_visibility('hidden');
$product->save();
Other options are: 'visible', 'search' and 'catalog'.
Upvotes: 0
Reputation: 331
I have tried doing this for some days, and there is nothing about it online so I read the woocommerce documentation and discovered that in woocommerce 3.x.x the visibility is a taxonomy called "product_visibility".
To achieve that you should set taxonomy terms, for example:
//Set product hidden:
$terms = array( 'exclude-from-catalog', 'exclude-from-search' );
wp_set_object_terms( $post_id, $terms, 'product_visibility' );
//Set product visible in catalog:
$terms = 'exclude-from-search';
wp_set_object_terms( $post_id, $terms, 'product_visibility' );
//Set product visible in search:
$terms = 'exclude-from-catalog';
wp_set_object_terms( $post_id, $terms, 'product_visibility' );
All possible taxonomy terms:
"exclude-from-catalog"
"exclude-from-search"
"featured"
"outofstock"
Upvotes: 23
Reputation: 3815
The visibility is set in the custom field _visibility
. You can change it with update_post_meta()
:
update_post_meta( $product_id, '_visibility', '_visibility_hidden' );
Possible values:
visible
(Catalog & Search)catalog
(Catalog only)search
(Search only)hidden
(nowhere)Upvotes: 7