Website Building » WooCommerce » How Do You Get Product Count With Respect to Categories in WooCommerce?

How Do You Get Product Count With Respect to Categories in WooCommerce?

Last updated on January 15, 2023 @ 4:32 pm

When it comes to setting up an online store, WooCommerce is one of the most popular choices for WordPress users. If you’re using WooCommerce to sell products on your WordPress site, you may have noticed that there’s no built-in way to show product counts next to your category names. This can be a bit confusing for customers, especially if you have a lot of products in each category.

Fortunately, there’s a simple workaround that will let you add product counts to your WooCommerce category pages.

All you need to do is add a few lines of code to your WordPress theme. In this article, we’ll show you how to add product counts to WooCommerce categories in four easy steps.

Step 1: Edit Your WordPress Theme Files

The first thing you need to do is edit your theme’s functions.php file. You can do this from your WordPress dashboard by going to Appearance > Editor.

On the Edit Themes page, select the functions.php file from the list on the right hand side. Once you’ve selected the file, click on the ‘Edit’ button at the top of the page.

PRO TIP: If you are using WooCommerce to manage your eCommerce store, it is important to know how to get the product count with respect to categories. This article will explain how to do this.

Step 2: Add The Code Snippet

Next, you need to add the following code snippet to your functions.php file:

function my_product_cat_count_span( $links ) {
$links = str_replace( ' (', ' (', $links );
$links = str_replace( ')', ')', $links );
return $links;
}
add_filter( 'wp_list_categories', 'my_product_cat_count_span' );

Step 3: Save Your Changes and Test It Out

Once you’ve added the code snippet, click on the ‘Update File’ button to save your changes.

Step 4: Style The Product Counts With CSS

If you want to style the product counts, you can do so with CSS. For example, if you want to change the color of the product counts, you would add the following CSS to your theme’s stylesheet:

.cat-item a:after {
color: #ff0000; /* Change color here */
}

And that’s all there is to it! You should now have product counts next to your category names on your WooCommerce category pages.

Kathy McFarland

Kathy McFarland

Devops woman in trade, tech explorer and problem navigator.