A client asked me to customize the Product Categories Widget that comes included with WooCommerce. In this case, they wanted to show ALL categories but the current one (when looking at a category page of course).
You can also use this tutorial to hide certain category IDs from the widget, or maybe “all product category names that start with a given string of text”. Case studies are millions, but the code base is always the same – we’ll make use of the “woocommerce_product_categories_widget_args” filter and provide a list of product categories to exclude. Enjoy!
PHP Snippet: Hide Current Category From The WooCommerce Product Categories Widget
/**
* @snippet Hide Current Category @ WooCommerce Prod Cat Widget
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @testedwith WooCommerce 3.9
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_product_categories_widget_args', 'bbloomer_hide_current_cat_prod_cat_widget' );
function bbloomer_hide_current_cat_prod_cat_widget( $args ) {
if ( is_product_category() ) {
$current_cat_id = get_queried_object_id();
$args['exclude'] = $current_cat_id;
}
return $args;
}
Can we do the opposite? For example, can we show only the current category, the parent category of the current category, and the subcategories of the said parent category?
For example, when you are in the Apple category, it will be displayed as follows.
Appearances
– Fruits
— Apple
— Pear
— Strawberry
what’s hidden
– Vegetables
— Pepper
— Lettuce
— Tomatoes
Hello Ilhan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
It’s still working great. 🙂 Rodolfo articles are very helpful, and thank you very much.
Note: if you want to use it also for subcategories use this code:
.woocommerce .widget_product_categories ul.product-categories > li:not(.cat-parent) {
display:none;
}
Excellent 🙂
This one guys all the other code was causing issues for me eliminating some children where the children were parents.
I have a production site and a test site but are under maintenance….
Where can I contact you outside here?
Leo
No problem – feel free to email me at Info-at-Businessbloomer.com
e-mail sent…
Leo, it must have gone into the spam and I deleted it 🙁 Can you send that again please? Thank you
Hi Rodolfo…your snippet works only with first level sub cats links
does not work with shop level page…list is empty with no categories
Any suggest to display first level categorie list in shop page?
Regards
Leo
Thanks for your feedback Leo! Could you give me the link to your website so I can do some testing please? Thank you 🙂
Hi Leo, I had the same issue. I got this working for my second and third tier navigation by changing to ‘cat-parent’ instead of ‘current-cat’. Work well for my needs.
.woocommerce .widget_product_categories ul.product-categories > li:not(.cat-parent)
Cheers for the helpful feedback!
wonder if it work for hidde a text area?
Of course Renato, whatever you can target with CSS/PHP can be hidden!