【问题标题】:Number of product in multiple categories and their descendants for woocommercewoocommerce 的多个类别中的产品数量及其后代
【发布时间】:2016-11-06 21:00:27
【问题描述】:

考虑到每个类别都有一些子类别这一事实,我想获得多个 woocommerce 类别中的产品数量。 例如:

term_id 为 1 的类别具有“器官”的 slug,并拥有 2 个子类别(“内部”的 term_id 为 2,“外部”的 term_id 为 3)

term_id 为 4 的类别具有“药物类型”的 slug,并拥有 2 个子类别(term_id 为 5 的“药丸”和 term_id 为 6 的“胶囊”)

我的问题是如何获取“器官”和“药物类型”类别中的产品数量?

编辑版本 1:

我在主页中测试了以下代码,它可以正常工作,并为我提供了具有 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>';

只是为了让你更清楚我的情况。

我的分类图是这样的:

括号中的数字是每个类别中的产品数量,当我运行上面的代码时,它会给我每个“内部”和“外部”类别的正确数字。

但是当我在 ajax 上测试它时,只给了我 0 个产品,因为它只搜索完全属于它自己的类别的产品,但奇怪的是代码与我的代码没有什么不同以非 ajax 方式测试。

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce product categories


    【解决方案1】:

    以下代码将为您提供所有类别的计数,包括类别中的子类别。子类别产品将在与父类别相同的数组键中加入:

    $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>';
    

    子类别不会出现任何数组元素。子类别产品的数量将在

    范围内

    【讨论】:

    • @tovishlalck 感谢您的快速帮助。但问题是我无法在 ajax 调用中获得正确的计数。我的意思是我在编辑过的问题版本 1 中有相同的代码,但 ajax 和默认页面加载的结果不同。你能帮我解释为什么我在 ajax 调用中得到不同的结果。
    • @Ehsan 你在 ajax 调用中得到了什么?结果如何?
    • @tovishlalck 我通过编辑问题添加了完整描述
    • @Ehsan 您粘贴的代码将为“内部”和“外部”提供 0,即使它没有在 ajax 中运行。原因是 get_categories() 仅返回提供的 slug 中的 1 级类别,在本例中为“器官”。
    • 实际上,这段代码没有给我父类别的类别,这些类别有“器官”的“外部”和“内部”,我在每个子类别中得到了正确数量的产品'器官' 通过这种方法 italic bold $subcat-&gt;count
    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2021-05-08
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多