dswsl2016 2016-07-05 06:18
浏览 47

多个类别的产品数量及其后代的woocommerce

I want to get the number of products in multiple woocommerce categories considering this fact that each of the categories possess some subcategories. For example:

Category with term_id of 1 have the slug of "organ" and possesses 2 subcategories( "internal" with term_id of 2 and "external" with term_id of 3)

Category with term_id of 4 have the slug of "drug-type" and possesses 2 subcategories( "pills" with term_id of 5 and "capsule" with term_id of 6)

My question is how to get the number of products that are in categories "organ" and "drug-type"?

Edit Version 1:

I tested the following code in my home page and it works and gave me the correct number of products in subcategories of a category with the slug of organ.

$main_product_category = get_term_by( 'slug', 'organ', 'product_cat' );
$main_product_category_id = $main_product_category->term_id;
$args = array(
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'parent' => $main_product_category_id,
    'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<div class="quick-search-options">';
foreach ($subcats as $subcat) {
    $link = get_term_link( $subcat->slug, $subcat->taxonomy );
    echo '<span>
                <a href="'. $link .'" class="btn btn-default" data-id="'.$subcat->term_id.'" role="button">'.$subcat->name.$subcat->count.'</a>
              </span>';
}
echo '</div>';

Just to make my case more clear to you.

The diagram of my categories is like this:

enter image description here

The number in parenthesis is the number of products in each categories and when I run the above code it will give me correct number for each "internal" and "external" categories.

But when I test it on ajax in only gave me 0 number of products because it only searching for the products that are exactly belonging to the category it self but the weird thing is that the code is not different from the one that I tested in non ajax way.

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dsh12544 2016-07-05 07:02
    关注

    The below code will give you a count of all categories, including the sub-categories within a category. The subcategories products will be clubbed in the same array key as the parent category:

    $product_categories = get_terms( 'product_cat' );
    $categories_count = array();
    foreach( $product_categories as $key => $value ) {
       $category_term_id = $value->term_id;
    
       if ( $value->parent > 0 ) {
               $category_parent_term_id = $value->parent;
               if ( !isset( $categories_count[$category_parent_term_id] ) ) {
                       $categories_count[$category_parent_term_id] = $value->count;
               } else {
                       $categories_count[$category_parent_term_id] = $categories_count[$category_parent_term_id] + $value->count;
               }
       } else {
               if ( !isset( $categories_count[$category_term_id] ) ) {
                       $categories_count[$category_term_id] = $value->count;
               } else {
                       $categories_count[$category_term_id] = $categories_count[$category_term_id] + $value->count;       
               }
       }
    }
    echo 'Categories Count: <pre>';print_r($categories_count);echo '</pre>';
    

    There won't be any array element present for the subcategories. The count of subcategory products would be within

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度