如何在WooCommerce页面按分类展示产品平均成本?
WooCommerce 按分类展示产品平均成本实现方案
核心思路
通过WP_Query获取目标分类下的所有已发布产品,循环读取每个产品的成本元数据,累计总成本和有效产品数量(排除无成本值的产品),最后计算并格式化输出平均成本。
完整代码实现
1. 基础实现(直接嵌入模板)
将以下代码放在你需要展示平均成本的位置(比如分类归档页模板archive-product.php、自定义页面模板等):
// 目标分类Slug(示例为sale分类) $target_cat_slug = 'sale'; // WP_Query 参数配置 $product_args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1, // 获取分类下所有产品 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $target_cat_slug, ), ), ); $products = new WP_Query($product_args); // 初始化计算变量 $total_cost = 0.0; $valid_product_num = 0; if ($products->have_posts()) { while ($products->have_posts()) { $products->the_post(); global $product; // 读取产品成本元数据(替换为你实际使用的成本字段) // 示例:若使用Cost of Goods插件,字段为_wc_cog_cost;自定义字段则改为你的字段名 $product_cost = get_post_meta(get_the_ID(), '_product_cost', true); // 验证成本值有效性 if (is_numeric($product_cost) && floatval($product_cost) > 0) { $total_cost += floatval($product_cost); $valid_product_num++; } } wp_reset_postdata(); // 重置查询,避免影响后续内容 } // 计算并展示平均成本 if ($valid_product_num > 0) { $average_cost = $total_cost / $valid_product_num; // 使用WooCommerce内置函数格式化价格(适配站点货币设置) $formatted_avg_cost = wc_price($average_cost); ?> <div class="cat-average-cost"> <h3><?php echo esc_html(get_term_by('slug', $target_cat_slug, 'product_cat')->name . ' 分类平均成本'); ?></h3> <p><?php echo sprintf('平均成本:%s', $formatted_avg_cost); ?></p> </div> <?php } else { echo '<p>该分类下暂无有效成本数据的产品</p>'; }
2. 短代码封装(灵活调用)
如果需要在文章、页面或自定义区块中调用,将代码封装为短代码:
function wc_cat_average_cost_shortcode($atts) { // 短代码默认参数 $atts = shortcode_atts( array( 'category' => 'sale', // 默认分类Slug ), $atts, 'wc_cat_avg_cost' ); $target_cat_slug = $atts['category']; $product_args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $target_cat_slug, ), ), ); $products = new WP_Query($product_args); $total_cost = 0.0; $valid_product_num = 0; if ($products->have_posts()) { while ($products->have_posts()) { $products->the_post(); $product_cost = get_post_meta(get_the_ID(), '_product_cost', true); if (is_numeric($product_cost) && floatval($product_cost) > 0) { $total_cost += floatval($product_cost); $valid_product_num++; } } wp_reset_postdata(); } ob_start(); if ($valid_product_num > 0) { $average_cost = $total_cost / $valid_product_num; $formatted_avg_cost = wc_price($average_cost); $cat_name = get_term_by('slug', $target_cat_slug, 'product_cat')->name; ?> <div class="cat-average-cost"> <h3><?php echo esc_html($cat_name . ' 分类平均成本'); ?></h3> <p><?php echo sprintf('平均成本:%s', $formatted_avg_cost); ?></p> </div> <?php } else { echo '<p>该分类下暂无有效成本数据的产品</p>'; } return ob_get_clean(); } add_shortcode('wc_cat_avg_cost', 'wc_cat_average_cost_shortcode');
使用方式:在需要展示的地方插入短代码 [wc_cat_avg_cost category="sale"],替换category参数为目标分类的Slug即可。
关键注意事项
- 成本字段适配:代码中
_product_cost是示例字段,需根据实际情况替换:- 若使用「Cost of Goods for WooCommerce」插件,字段为
_wc_cog_cost - 若为自定义成本字段,替换为你设置的字段名
- 若使用「Cost of Goods for WooCommerce」插件,字段为
- 数据有效性验证:通过
is_numeric()和数值大于0的判断,避免无效数据影响计算结果 - 查询重置:必须调用
wp_reset_postdata(),防止后续WordPress查询出现异常 - 价格格式化:使用
wc_price()函数确保输出的价格符合站点的货币设置(如符号、小数位数)
内容的提问来源于stack exchange,提问作者Vitalii Diakin
相关产品推荐
相关产品推荐

