
本文详解如何使用 wordpress 原生函数与自定义 sql 查询,动态生成包含每个产品分类的商品数量、该分类下所有商品 _regular_price 总和,以及全局汇总的 html 表格,适用于 woocommerce 商城数据看板或运营报表场景。
本文详解如何使用 wordpress 原生函数与自定义 sql 查询,动态生成包含每个产品分类的商品数量、该分类下所有商品 _regular_price 总和,以及全局汇总的 html 表格,适用于 woocommerce 商城数据看板或运营报表场景。
在 WooCommerce 网站中,常需向管理员或客户展示各商品分类的销售价值概览(如邮票、硬币、卡片等类目的总标价)。由于 WooCommerce 默认不提供“分类级价格聚合”功能,需结合 get_terms() 获取分类列表,并通过高效 SQL 联查 wp_postmeta 与 wp_term_relationships 表,精准汇总 _regular_price 字段值。
以下为完整、可直接集成到 WordPress 页面模板(如 page-products-summary.php)或自定义短代码中的 PHP 实现:
<?php
// 1. 获取所有产品分类(含空分类)
$categories = get_terms(
array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'hide_empty' => false,
'fields' => 'all'
)
);
// 2. 初始化汇总数组
$totals_per_category = array();
$product_counts = array();
$total_all_categories = 0;
$total_product_count = 0;
global $wpdb;
// 3. 遍历每个分类,执行聚合查询
foreach ($categories as $category) {
$term_id = $category->term_id;
// 查询该分类下所有产品的 _regular_price 总和
$total_price = $wpdb->get_var(
$wpdb->prepare(
"SELECT COALESCE(SUM(pm.meta_value), 0)
FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->term_relationships} tr ON pm.post_id = tr.object_id
INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID
WHERE tr.term_taxonomy_id = %d
AND pm.meta_key = '_regular_price'
AND p.post_status = 'publish'
AND p.post_type = 'product'",
$term_id
)
);
// 查询该分类下已发布商品数量
$product_count = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(DISTINCT p.ID)
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id
WHERE tr.term_taxonomy_id = %d
AND p.post_status = 'publish'
AND p.post_type = 'product'",
$term_id
)
);
$totals_per_category[$category->term_id] = (float) $total_price;
$product_counts[$category->term_id] = (int) $product_count;
$total_all_categories += (float) $total_price;
$total_product_count += (int) $product_count;
}
// 4. 渲染 HTML 表格
?>
<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>
<?php foreach ($categories as $category): ?>
<?php
$price = isset($totals_per_category[$category->term_id]) ? $totals_per_category[$category->term_id] : 0;
$count = isset($product_counts[$category->term_id]) ? $product_counts[$category->term_id] : 0;
$formatted_price = wc_price($price); // 使用 WooCommerce 格式化货币(支持多币种/小数位)
?>
<tr>
<td style="text-align: left;">
<a href="<?php echo esc_url(get_term_link($category)); ?>">
<?php echo esc_html($category->name); ?>
</a>
</td>
<td style="text-align: center;"><?php echo esc_html($count); ?></td>
<td style="text-align: right;"><?php echo $formatted_price; ?></td>
</tr>
<?php endforeach; ?>
<tr>
<td style="text-align: left; font-weight: bold;">**总计**</td>
<td style="text-align: center; font-weight: bold;"><?php echo esc_html($total_product_count); ?></td>
<td style="text-align: right; font-weight: bold;"><?php echo wc_price($total_all_categories); ?></td>
</tr>
</tbody>
</table>✅ 关键优化说明:
- 使用 $wpdb->prepare() 防止 SQL 注入,提升安全性;
- 添加 p.post_status = 'publish' 和 p.post_type = 'product' 条件,确保仅统计有效商品;
- 采用 COALESCE(SUM(...), 0) 避免 NULL 导致计算中断;
- 调用 wc_price() 进行本地化货币格式化(自动适配站点设置的小数位、千分位符与货币符号);
- 分类链接使用 get_term_link() 保证 URL 正确性(兼容层级结构与重写规则)。
⚠️ 注意事项:
- 请将此代码置于主题的 page 模板或通过 add_shortcode() 封装为短代码使用,切勿直接放入 functions.php 的全局作用域,否则可能引发执行时机错误;
- 若网站商品量极大(>10,000),建议添加缓存机制(如 wp_cache_set() / wp_cache_get()),避免每次页面加载重复查询;
- 如需包含变体商品的价格,请额外联查 woocommerce_order_items 或扩展逻辑处理 _price 元数据来源——本方案默认仅统计简单商品及变体的 _regular_price(即基础售价)。
通过以上实现,你将获得一个性能可靠、语义清晰且符合 WooCommerce 最佳实践的分类价值统计表格,助力精细化运营分析。

















