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;
}

Similar questions

Cart discount based on cart item count and only for items that are not in sale
In WooCommerce, I would like to give a discount of 10% specifically for those products that are not on sale. If cart item count is 5 or more items and not on sale, then I give a discount of 10%. I use the following code to get a discount based on cart item count restriction here: But I don't know how to apply the discount only for items that are no...
WordPress Multisite: How to make all media library items available for every site?
I have a WordPress Multisite where the main site has all the images in the media library I want to use in each additional site, however I can't figure out a way to get all the images to show up in the additional sites' media library. I've looked at the Network Shared Media plugin but it doesn't allow me to add the images where I need them. Only in ...
How to only show category description and title if there's a description available?
Is it possible to only show the code below if there's a category description? I have these codes on my single posts in Wordpress but sometimes, I forgot to add descriptions on a new category that I added. So when a user visits the post, they will just see the word About Category and no description at all, making it looks like an incomplete article....
Change a while from listning all items to list only x number of items (for maybe)
Can someone PLEASE help a newbie to change the while to list only x amount of items? my end goal is to list 10 posts... not 1000s ;) lol my code is:
Mailchimp RSS feed for Wordpress site includes only new items, FEEDITEM-based feed has older items
I am trying to create a Mailchimp campaign email that includes two sections, one with the latest items from our news feed and one with the latest items from our events feed. Our site is Wordpress-based. When I create an email with a single RSS feed from the news items using an RSS-based campaign, I get the most recent items, with the newest items f...
Category count but only if category has at least one post
I need to display the number of categories which I can do with this... ...but I only want it to count categories that have at least one post. Thanks in advance.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.