I have created a custom field true/false, and I want when true is selected in a product not to be displayed in the eshop.
I want to insert the code inside the functions.php
example
if ( in_array( 'subscriber', (array) $user->roles ) || !is_user_logged_in() ) {
$postid = get_the_ID();
$prd_only_for_Customers = get_field('prd_clients', $postid); // The ACF true/false field }
Can anyone help ?
2
Answers
As Howard said your question is incomplete but you can use the following method to set product hidden.
You can use
pre_get_posts
hook in your functions.php. 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 methodsset_catalog_visibility()
this way:This code isn’t tested, let me know if it worked or you faced any problem.
This is my final code if anyone needs something like this