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        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.9
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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 PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

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? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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