WooCommerce: Hide Categories From Product Cat Widget

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!

woocommerce-hide-non-current-categories-widget
WooCommerce case study: hide current product category from the Product Categories Widget

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;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

15 thoughts on “WooCommerce: Hide Categories From Product Cat Widget

  1. 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

    1. 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!

  2. 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;
    }

    1. This one guys all the other code was causing issues for me eliminating some children where the children were parents.

  3. I have a production site and a test site but are under maintenance….
    Where can I contact you outside here?
    Leo

    1. No problem – feel free to email me at Info-at-Businessbloomer.com

      1. e-mail sent…

        1. Leo, it must have gone into the spam and I deleted it 🙁 Can you send that again please? Thank you

  4. 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

    1. Thanks for your feedback Leo! Could you give me the link to your website so I can do some testing please? Thank you 🙂

    2. 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)

      1. Cheers for the helpful feedback!

  5. wonder if it work for hidde a text area?

    1. Of course Renato, whatever you can target with CSS/PHP can be hidden!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *