How to Hide Empty Category In WooCommerce

WooCommerce Hide Empty Category
Are you looking for a way to hide an empty category in your WooCommerce Store? By default, WooCommerce displays products from different categories on the homepage.

In this brief tutorial, I will share a solution on how you can hide an empty category without using a plugin.

WooCommerce Hide Empty Category

WooCommerce allows you to sort products into different categories and tags. This means that even empty categories are displayed on the shop and home pages.

You may want to hide the empty categories so that users cannot find them.

However, WooCommerce does not have a built-in option to hide categories from the homepage or archives.

It will display the category even when it is empty. Hiding an empty category is an easy task if you are an experienced WordPress developer.

We will use a PHP code snippet to hide the empty categories. However, you can also use CSS, but this will receive negative points from search engines, and it will have a negative effect on your SEO.

Steps to Hide Empty Categories from Category Widget in WooCommerce

Here are the steps that you should follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. First, we need to create some categories and assign products. To do this, click on Products > Categories.
    You can assign categories from here. You have seen that I have assigned five products to the Car Parts Category and four to Njengah Tech from the screenshot below. The first category is empty.product categories
  3. Alternatively, you can also assign categories to products from the product add/edit screen, as shown below:assign categories
  4. The next step is to add the category widget to the sidebar or any other area you want your WooCommerce store.
    To do this, go to Appearance > Widgets, drag the WooCommerce product categories widget, and drop to the primary sidebar area, as shown below:adding a widget
  5. If you refresh the front end, you’ll see all the categories being listed:product categories sidebar
  6. To hide or remove the empty categories listed here, add the following code lines at the end of your theme’s functions.php file.
function woo_hide_product_categories_widget( $list_args ){

            $list_args[ 'hide_empty' ] = 1;

            return $list_args;

}

add_filter( 'woocommerce_product_categories_widget_args', 'woo_hide_product_categories_widget' )
  1. After placing the code above, remember to save the file. Go to the front end and refresh the page. You will notice that the empty category has been removed from the list.empty categories hidden

Conclusion

In summary, this post shows you how you can hide an empty category in your WooCommerce store. This would significantly improve your store’s user experience, as users will only see the categories that have products.

Moreover, I have highlighted that if you hide categories by changing the display to no in the CSS files, you will receive negative points from search engines, and it will have a negative effect on your SEO.

Similar Articles