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.
Catalog Visibility Options transforms your WooCommerce store into a catalog by giving you the option to disable all eCommerce functionality. This includes changing the “Add to Cart” and “Buy Now” buttons as well as disabling the regular checkout and cart processes.
From the admin panel, go to WooCommerce > Product Visibility > Global visibility tab and select the product and category you want to hide.
A private product is one that is not visible to the public on your WooCommerce store. By default, all products in WooCommerce are public, meaning they can be seen and purchased by anyone.
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"
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)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With