Show WooCommerce product count in theme

Couldn’t Google the solution so after some time a came up with the following solution:

// functions.php
function my_product_count() {
    $product_count = 0;

    // Get count for all product categories
    foreach (get_terms('product_cat') as $term)
        $product_count += $term->count;

    return $product_count;
}
<!-- header.php -->
<div class="counter">
    <span><?php echo my_product_count() ?><br>products</span>
</div>
Advertisements

About Michal Zuber

Biker and rollerblader. Owner and developer at http://nevilleweb.sk/ Co-founded http://neville.sk/ Blog at https://michalzuber.wordpress.com/
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

7 Responses to Show WooCommerce product count in theme

  1. martijn says:

    what will this look like? and where do I put it?

  2. martijn says:

    thanks for your reply. I am using woocommerce and would like to have a plugin or widget that shows the number of products I have in my store but I can’t find anything like that…

  3. martijn says:

    yes, I do use that but that’s not really what I mean. I would like to show the total amount of products on the homepage or footer/sidebar

  4. Alipio Salvador Gabriel says:

    why not

    $count_posts = wp_count_posts(‘product’);

    $published_posts = $count_posts->publish;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s