如何在WooComells中获取与类别相关的产品数量

时间:2015-10-12 作者:Faisal Naseer

您好,我想在一个循环中显示所有类别的产品,以在类别菜单中显示它们以及每个类别包含的产品数量。像这样的事情

enter image description here

到目前为止,我已经完成了所有分类

 $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; }
但我想知道如何显示每个类别的产品编号。

3 个回复
最合适的回答,由SO网友:asp111 整理而成

您只需添加$cat->count 获取该类别中所有产品的计数。希望这对你有所帮助。

$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.\')\'; 
}

SO网友:mohit saint

foreach( $product_categories as $cat )  { 
   echo $cat->name.\' (\'.$cat->count.\')\'; 
}
对我来说效果很好,但如何显示只销售的产品数量?

SO网友:Purnendu Sarkar
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>