【问题标题】:Display Product Count in WooCommerce category with subcategory在带有子类别的 WooCommerce 类别中显示产品数量
【发布时间】:2020-09-17 22:54:37
【问题描述】:

如何在 WooCommerce 中显示带有子类别的类别中的产品总数? 我正在尝试使用此代码,但它只能在没有子类别的类别中完美运行。

add_action( 'woocommerce_before_shop_loop', 'add_product_count_view', 10);

  function add_product_count_view() {
    global $wp_query;

    $category_id = $wp_query->get_queried_object()->term_id;
    $term = get_term( $category_id, 'product_cat' );  

    echo $term->name . '(' . $term->count . ')';
  }

【问题讨论】:

    标签: wordpress woocommerce count categories product


    【解决方案1】:

    将此代码粘贴到function.php中:

      add_action( 'count_product_title', 'add_product_count_view', 10);
    
      function add_product_count_view() {
        global $wp_query;
    
        $category_id = $wp_query->get_queried_object()->term_id;
        //echo sprintf( _n( '%d товар', '%d товаров', $term->count ), $term->count );
        $query = new WP_Query( array(
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'id',
                'terms' => $category_id, 
                'include_children' => true,
            ),
        ),
        'nopaging' => true,
        'fields' => 'ids',
        ) );
    
        if( function_exists("is_shop") && $category_id != 0) {
        echo '(' .esc_html( $query->post_count ) . ')';
        }
      }
    

    粘贴到您的模板中 <?php do_action('count_product_title');?>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-09
      • 2017-03-20
      • 2018-08-18
      • 2018-05-17
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 2022-07-08
      相关资源
      最近更新 更多