Woocommerce category count only available items
I want to show product count of category by shortcode and I achieve it via this code
add_shortcode( 'products-counter', 'products_counter' );
function products_counter( $atts ) {
$atts = shortcode_atts( [
'category' => '',
], $atts );
$taxonomy = 'product_cat';
if ( is_numeric( $atts['category'] ) ) {
$cat = get_term( $atts['category'], $taxonomy );
} else {
$cat = get_term_by( 'slug', $atts['category'], $taxonomy );
}
if ( $cat && ! is_wp_error( $cat ) ) {
return $cat->count;
}
return '';
}
add_filter('wp_nav_menu_items', 'do_shortcode');
but is it possible only to show products which is on stock?
So example: I have 10 laptops and now it show (10) but I have only 3 on stock so instead of (10) it should show (3) / only to count in stock items in that category.
Solutions
Remove the category count number everywhere: To completely remove the category count number, you can simply copy the following PHP code and add it to your site.
Remove the category count everywhere
// Remove the category count for WooCommerce categories
add_filter( 'woocommerce_subcategory_count_html', '__return_null' );
And to remove the category count for WooCommerce categories using CSS you can add the following code to Customize -> Additional CSS:
Remove the category count everywhere using custom CSS
.woocommerce-loop-category__title mark.count {
display: none;
}
Add custom CSS to style the category count to look better: If you’re wanting the category product count to stay on the categories, but you want it to look better and not have a yellow background then you can add the following custom CSS to Customize -> Additional CSS.
If you know a little more about CSS then you can add more attributes to style it as you want, otherwise, this CSS will make it smaller and lighter so it fits most theme designs and look neater.
Style the category count to look better
.woocommerce-loop-category__title mark.count {
background: none;
font-size: 0.8em;
color: #000;
opacity: 0.4;
letter-spacing: 1px;
}