I wanted to remove brackets for products count on my child theme of Storefront Woothemes theme. I also wanted to add some round background to this count.
So I’ve put this into my child-theme functions.php
/*
* Removes brackets for count after categories name
*/
function filter_woocommerce_subcategory_count_html( $mark_class_count_category_count_mark, $category ) {
$mark_class_count_category_count_mark = ' <mark class="count">' . $category->count . '</mark>';
return $mark_class_count_category_count_mark;
};
Then added some CSS to style them with background and to show category counts rounded.
mark.count {
background: #afad00;
border-radius: 100%;
color: #fff;
display: inline-block;
font-size: 1em;
line-height: 1.42;
text-align: center;
vertical-align: text-bottom;
width: 1.46em;
}
Remove Subcategories Product Counts On Some Pages
After I modified product count display on categories, I’ve decided to remove product counts on all pages except homepage. But conditions only work with add_action, not with filters. So here’s the working example.
/** Remove counts functionality for all except home */
function iggy_remove_counts() {
if( is_front_page() || is_home() ) {
// Remove brackets for count after categories name
add_filter( 'woocommerce_subcategory_count_html', 'filter_woocommerce_subcategory_count_html', 10, 2 );
function filter_woocommerce_subcategory_count_html( $mark_class_count_category_count_mark, $category ) {
$mark_class_count_category_count_mark = ' <mark class="count">' . $category->count . '</mark>';
return $mark_class_count_category_count_mark;
}
} else {
// Remove counts
add_filter( 'woocommerce_subcategory_count_html', 'jk_hide_category_count' );
function jk_hide_category_count() {
// No count
}
}
}
add_action('wp', 'iggy_remove_counts');
I’m using a snippet by woothemes to remove product counts completely from all other pages.
Thanks for the snippet.
I wanted to know how did you find $mark_class_count_category_count_mark in woocommerce to change it?
I am using the ‘simply show hooks’ plugin, but it doesn’t tell me anything about the category count.
Did you end up looking through the woocommerce file directory?
Thanks.