WooCommerce需求:仅在商品on_sale时显示折扣百分比($percent)
在WooCommerce商品循环中仅显示真实促销商品的折扣百分比
嘿,我来帮你搞定这个需求!要实现“只给真正处于促销状态(确实有折扣)的商品显示折扣百分比,同时正常展示非促销商品”,关键是要准确判断商品的真实促销状态,避免那些设置了销售价但和常规价一致的“伪促销”商品乱显示。
下面分两种场景给你解决方案,一种是用钩子(更推荐,主题更新不会失效),另一种是直接修改模板:
方案一:使用钩子添加(无需修改模板)
直接把这段代码加到你当前主题的functions.php文件里就行:
add_action( 'woocommerce_before_shop_loop_item_title', 'display_real_sale_percentage', 15 ); function display_real_sale_percentage() { global $product; // 非促销商品直接跳过 if ( ! $product->is_on_sale() ) { return; } // 处理简单商品、外部商品、分组商品 if ( $product->is_type( array('simple', 'external', 'grouped') ) ) { $regular_price = (float) $product->get_regular_price(); $sale_price = (float) $product->get_sale_price(); // 确保常规价大于销售价,且常规价不为0(避免除以0错误) if ( $regular_price <= $sale_price || $regular_price == 0 ) { return; } // 计算折扣百分比并取整 $percent = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 ); echo '<span class="discount-percent">-'. $percent .'%</span>'; } // 处理变量商品(多规格商品) elseif ( $product->is_type('variable') ) { $max_percent = 0; $variations = $product->get_available_variations(); // 遍历所有可用规格,找出最大的折扣比例 foreach ( $variations as $variation ) { $var_regular = (float) $variation['regular_price']; $var_sale = (float) $variation['sale_price']; if ( $var_regular > $var_sale && $var_regular > 0 ) { $var_percent = round( ( ( $var_regular - $var_sale ) / $var_regular ) * 100 ); $max_percent = max( $max_percent, $var_percent ); } } // 只有找到有效折扣才显示 if ( $max_percent > 0 ) { echo '<span class="discount-percent">-'. $max_percent .'%</span>'; } } }
方案二:直接修改商品循环模板
如果你习惯修改模板文件,可以找到主题中WooCommerce的商品循环模板(一般是woocommerce/content-product.php),在合适的位置(比如商品图片上方/下方)插入以下代码:
global $product; // 判断是否为真实促销商品 if ( $product->is_on_sale() ) { // 简单商品逻辑 if ( $product->is_type( array('simple', 'external', 'grouped') ) ) { $regular_price = (float) $product->get_regular_price(); $sale_price = (float) $product->get_sale_price(); if ( $regular_price > $sale_price && $regular_price > 0 ) { $percent = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 ); echo '<span class="discount-percent">-'. $percent .'%</span>'; } } // 变量商品逻辑 elseif ( $product->is_type('variable') ) { $max_percent = 0; $variations = $product->get_available_variations(); foreach ( $variations as $variation ) { $var_regular = (float) $variation['regular_price']; $var_sale = (float) $variation['sale_price']; if ( $var_regular > $var_sale && $var_regular > 0 ) { $var_percent = round( ( ( $var_regular - $var_sale ) / $var_regular ) * 100 ); $max_percent = max( $max_percent, $var_percent ); } } if ( $max_percent > 0 ) { echo '<span class="discount-percent">-'. $max_percent .'%</span>'; } } }
额外:给折扣标签加样式
可以在主题的style.css里加一点CSS,让折扣标签更美观:
.discount-percent { position: absolute; top: 10px; right: 10px; background-color: #e74c3c; color: #fff; padding: 4px 8px; border-radius: 4px; font-size: 13px; font-weight: 600; z-index: 10; }
这样处理后,只有**真正有折扣(销售价<常规价)**的商品才会显示折扣百分比,非促销商品完全不会有错误输出,完美实现促销和非促销商品混合展示的需求。
内容的提问来源于stack exchange,提问作者Alírio Guerra
相关产品推荐
相关产品推荐

