Archived topic
Disable selected woocommerce product categories from showing on shop page
4 replies · Started by Nicola on June 28, 2020
Hi, I'd like to be able to temporarily disable one or two categories from showing on my woocommerce shop page whilst the products are out of stock. There are plugins available that allow me to do this but I would much rather use code, are you able to help please? And is using code or a plugin better than just deleting the category and creating again once I have more stock? I'd be grateful for any help and advice. Thank you, Nicola
Hi there,
Woo provides the following snippet to do that:
https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/
Hi, thanks for your reply but unfortunately woocommerce have made a mistake here. Their page title is "exclude a category from the product page" however the info given is headed "Exclude products from a particular category on the shop page" which is what it does, and it works, but the category is still showing on the product page. Can this code be amended so the category itself doesn't show? Or is it easier to just delete it? thanks, Nicola
Update: Ok managed to find the following code which works for me:
add_filter( 'get_terms', 'exclude_category', 10, 3 );
function exclude_category( $terms, $taxonomies, $args ) {
$new_terms = array();
if ( is_shop() ){
foreach ( $terms as $key => $term ) {
if( is_object ( $term ) ) {
if ( 'books' == $term->slug && $term->taxonomy = 'product_cat' ) {
unset($terms[$key]);
}
}
}
}
return $terms;
}
Glad to hear you found the answer - and thanks for sharing