WooCommerce: Product Category Counter Shortcode

In WooCommerce, product category counters are the small numbers you see next to category names. These numbers indicate the total number of products within that specific category. They typically appear on shop and archive pages where product categories are listed.

In the past, we’ve seen how to remove the WooCommerce product category counters from the shop page.

Today, we’re looking at a way to add them on the product category page, wherever you wish – in the title, inside the description, in a second description, in a custom block. All you need is a simple shortcode. Enjoy!

That “190” inside the product category description is dynamically generated by the shortcode below. Cool!

PHP Snippet: Shortcode To Get The Product Category Counter @ Product Category Page

This code snippet defines a shortcode called cat-counter and a function to handle that shortcode, bbloomer_return_current_category_counter.

Let’s break it down:

  1. Shortcode definition:
    • add_shortcode( 'cat-counter', 'bbloomer_return_current_category_counter' );
      • This line uses the WordPress function add_shortcode to register a new shortcode.
      • The first parameter, 'cat-counter', specifies the name of the shortcode. This name can be used within your WordPress content to insert the functionality provided by the shortcode.
      • The second parameter, 'bbloomer_return_current_category_counter', is the name of the function that will be executed when the shortcode is used.
  2. Shortcode function:
    • function bbloomer_return_current_category_counter() { ... }
      • This function, bbloomer_return_current_category_counter, is responsible for the logic behind the shortcode.
      • Inside the function:
        • $category = is_product_category() ? get_queried_object() : false;
          • This line checks if the current page is a product category archive page using the is_product_category function.
          • If it is a product category archive, it retrieves the current category object using the get_queried_object function and stores it in the $category variable.
          • If it’s not a product category archive, it simply assigns false to the $category variable.
        • return $category ? $category->count : false;
          • This line checks if the $category variable is not false (meaning it retrieved a category object).
          • If it has a category object, it returns the value of the count property of the object. This count property likely refers to the number of products in that category.
          • If $category is false, it returns false as well.

In summary, this code snippet creates a shortcode that can be used within your WooCommerce product category archive pages. When the shortcode [cat-counter] is used, it will try to retrieve the number of products in the current category and display that number. If it’s not a product category archive page, it will return nothing.

/**
 * @snippet       Get Category Counter @ Woo Product Cat Page
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

add_shortcode( 'cat-counter', 'bbloomer_return_current_category_counter' );

function bbloomer_return_current_category_counter() {
	$category = is_product_category() ? get_queried_object() : false;
	return $category ? $category->count : false;
}

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

  • WooCommerce: Display All Products Purchased by User
    When a WooCommerce customer is logged in, you might want to show them the list of previously purchased products (maybe in a custom “My Account” tab). This is helpful when customers tend to buy the same products over and over again, and therefore you can help them “order again” without having them to search the […]
  • WooCommerce: Display Out of Stock Products (Shortcode)
    A client of mine wanted to show out of stock products on a separate page – so I coded a simple shortcode for you all! You can use this shortcode for different goals. For example, you might want to display what products you’ve sold to enhance customer trust / social proof. So let’s see (1) […]
  • WooCommerce: Recently Viewed Products (Shortcode)
    Currently, you can use a widget or a block to have the user see the list of products they recently viewed. But for now, let’s create our own shortcode… and let’s take advantage of the existing Business Bloomer ClubRated 5.00 out of 5 $29.00 – $999.00 /once Whether you’re only starting with WooCommerce or you’re […]
  • WooCommerce: Exclude Category from ‘product_categories’ Shortcode
    Sometimes solutions are very simple, and you don’t need rocket science to fix your issue! A client of mine needed to hide a category from the Product Categories Shortcode ( BloomerArmada (1) Online Courses (1) WooCommerce Mini-Plugins (30) ); in fact, there is no parameter that allows you to “exclude” a given product category such […]
  • WooCommerce: Display Product Reviews @ Custom Page (Shortcode)
    WooCommerce product reviews shows by default in the “Reviews” tab in the single product page. But what if, like me, you’re using custom sales pages and need to show such reviews elsewhere – by using a shortcode? I’ve spent some time doing this for two Business Bloomer pages, the contact page (beside the request a […]

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

4 thoughts on “WooCommerce: Product Category Counter Shortcode

  1. Thanks! I’ve been looking for something like this to add to meta title tags. Would love to see a snippet that works with product tags, too.

    1. Shouldn’t be too difficult

  2. This is very useful, thanks!

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 *