How to get product count with respect to categories in WooComerce

Hi I want to display all the categories of products in a loop to display them in category menu along the numbers of products each category contains. Some thing like that

so far I have done this to get all categories

 $args = array(
    'number'     = $number,
    'orderby'    = $orderby,
    'order'      = $order,
    'hide_empty' = $hide_empty,
    'include'    = $ids
);

$product_categories = get_terms( 'product_cat', $args );
foreach( $product_categories as $cat ) { echo $cat-name; }

But I want to know how to display products numbers in each category.

Topic woocommerce-offtopic categories Wordpress

Category Web


$product_categories = get_terms( 'product_cat', $args );

foreach( $product_categories as $cat )  { 
   echo $cat->name.' ('.$cat->found_posts.')'; 
}

count only counts per page whereas found_posts is global.


foreach( $product_categories as $cat )  { 
   echo $cat->name.' ('.$cat->count.')'; 
}

WORKED FINE FOR ME,but How to show the number of product which are sale only?


Category
                        <ul class="Category-list">

                            <?php

            $wcatTerms = get_terms('product_cat', array('hide_empty' => 0, 'parent' =>0));
            $count = $category->category_count;

            foreach($wcatTerms as $wcatTerm) : ?>
            <?php
            $thumb_id = get_woocommerce_term_meta( $wcatTerm->term_id, 'thumbnail_id', true );
            $term_img = wp_get_attachment_url(  $thumb_id );
            ?>
            <li><a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?>(<?php echo $wcatTerm->count;?>)</a></li>
                <?php endforeach;  ?>
                        </ul>
                    </div>

You just need to add $cat->count to get the count of all products in that category. Hope this helps you out.

$args = array(
    'number'     => $number,
    'orderby'    => $orderby,
    'order'      => $order,
    'hide_empty' => $hide_empty,
    'include'    => $ids
);

$product_categories = get_terms( 'product_cat', $args );

foreach( $product_categories as $cat )  { 
   echo $cat->name.' ('.$cat->count.')'; 
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.