Archived topic
Conditionally hide products on woocommerce shop
3 replies · Started by Raul on September 19, 2020
Hi team,
I found this old David's topic https://generatepress.com/forums/topic/conditional-formatting-of-woo-product-by-category/ while looking to hide products based on a ACF conditional (true/false). Following your example, I am able to add a class to hide products via CSS, but my issue is that the product pagination is of course not affected; Let's say I have 60 products per page, and when I hide 40 products with CSS, I still get page 1, 1-60 results, but only 20 are being displayed in the first page.
// Add class to not-sold-individually products - To hide via CSS
function tdp_category_id_class( $classes ) {
global $post;
if ( get_field( 'artwork_not_for_sale_alone' ) ) {
$classes[] = 'not-sold-separately';
}
return $classes;
}
add_filter( 'post_class', 'tdp_category_id_class' );
Can you think of any solution for this? I believe I could use pre_get_post but don't really know how to use it with the shop.
Hi there,
i would suggest giving those products a category term that you can then exclude from the archives using the snippet Woo provides here:
https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/
Thanks David. I tried and it works great so far.
The only issue is that you have to manually select that new product category instead of just using the ACF field. Also I had to hide the new product category from the category term list, using this –in case anyone else is interested:
https://wordpress.stackexchange.com/questions/156252/exclude-one-category-from-get-the-term-list
Best
Glad to be of help.
Probably possible to have the ACF field add the Tax term for you - maybe ACF support has an answer for that.