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!
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:
- 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.
- This line uses the WordPress function
- 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.
- This line checks if the current page is a product category archive page using the
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. Thiscount
property likely refers to the number of products in that category. - If
$category
is false, it returnsfalse
as well.
- This line checks if the
- This function,
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;
}
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.
Shouldn’t be too difficult
This is very useful, thanks!
Yay!