You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce按产品分类统计商品总价值及汇总功能求助

WordPress/WooCommerce 产品分类价值统计实现方案

原代码问题梳理

  • 调用get_categories()未指定分类法,默认拉取的是普通文章分类,不是WooCommerce的产品分类(对应自定义分类法为product_cat)
  • 未关联查询每个分类下的产品数据,无法获取商品数量、常规售价字段
  • 内层循环直接遍历分类术语对象做累加,逻辑完全不匹配业务需求,没有读取产品的regular_price元数据

可直接使用的实现代码

推荐封装成短代码,粘贴到当前主题的functions.php文件中,之后在需要展示表格的页面插入[product_category_value_table]即可渲染出你要的表格效果:

// 产品分类价值统计表格短代码
add_shortcode('product_category_value_table', 'render_product_cat_value_table');
function render_product_cat_value_table() {
    // 初始化全站总计变量
    $total_count = 0;
    $total_value = 0;
    $cat_rows = [];

    // 获取所有WooCommerce产品分类,排除未分类
    $categories = get_terms([
        'taxonomy' => 'product_cat',
        'hide_empty' => true, // 隐藏没有产品的分类
        'exclude' => get_option('default_product_cat'), // 排除默认未分类
    ]);

    // 遍历每个分类统计数据
    foreach ($categories as $cat) {
        // 查询当前分类下的所有产品
        $products = get_posts([
            'post_type' => 'product',
            'posts_per_page' => -1,
            'tax_query' => [
                [
                    'taxonomy' => 'product_cat',
                    'field' => 'term_id',
                    'terms' => $cat->term_id,
                    'include_children' => false, // 不统计子分类产品,需要统计子分类就改成true
                ]
            ],
            'post_status' => 'publish',
        ]);

        $cat_count = count($products);
        $cat_value = 0;
        foreach ($products as $p) {
            $product = wc_get_product($p->ID);
            // 累加常规售价,跳过价格为空的产品
            $regular_price = floatval($product->get_regular_price());
            $cat_value += $regular_price;
        }

        // 累加到全站总计
        $total_count += $cat_count;
        $total_value += $cat_value;

        // 拼接当前分类行
        $cat_link = get_term_link($cat->term_id);
        $cat_rows[] = <<<HTML
<tr>
    <td style="text-align: left;"><a href="{$cat_link}">{$cat->name}</a></td>
    <td style="text-align: center;">{$cat_count}</td>
    <td style="text-align: right;">{$cat_value},-</td>
</tr>
HTML;
    }

    // 拼接总计行
    $total_row = <<<HTML
<tr>
    <td style="text-align: left;"><strong>总计</strong></td>
    <td style="text-align: center;"><strong>{$total_count}</strong></td>
    <td style="text-align: right;"><strong>{$total_value},-</strong></td>
</tr>
HTML;

    // 拼接完整表格
    $html = <<<HTML
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th style="text-align: left;">分类</th>
<th style="text-align: center;">商品数量</th>
<th style="text-align: right;">总价值</th>
</tr>
</thead>
<tbody>
    {$cat_rows}
    {$total_row}
</tbody>
</table>
</div>
HTML;
    return $html;
}

可调整的配置项

  • 如果需要统计子分类下的产品,把tax_query里的include_children参数值改成true
  • 如果需要展示空分类,把get_terms里的hide_empty改成false
  • 如果要统计商品的实际售价而不是常规售价,把$product->get_regular_price()改成$product->get_price()
  • 价格后缀的,-可以根据你的货币格式自行替换调整

注意:如果你的产品数量非常多(超过1000件),建议给统计逻辑加缓存,避免每次加载页面都重复查询拖慢速度。

内容的提问来源于stack exchange,提问作者Denny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.02 03:39:31