
本文介绍如何在 woocommerce 中为全场商品设置统一折扣(如 15% off)的同时,保留并正确显示原始标价(regular price),以便通过 css 添加删除线效果,提升促销信息的可视化说服力。
本文介绍如何在 woocommerce 中为全场商品设置统一折扣(如 15% off)的同时,保留并正确显示原始标价(regular price),以便通过 css 添加删除线效果,提升促销信息的可视化说服力。
直接修改 woocommerce_product_get_price 和 woocommerce_product_get_regular_price 这两个钩子(如原代码所示)会导致逻辑混乱:你强行将 regular_price 也改为折扣后价格,从而丧失了原价数据源——这正是无法显示划线原价的根本原因。
✅ 正确做法是:仅覆盖销售价(sale_price),而让 regular_price 保持原始值不变。WooCommerce 原生机制会自动识别 sale_price < regular_price 并触发 is_on_sale(),进而启用标准促销模板(如 <del> 包裹原价 + 突出显示现价)。
以下是推荐的实现方案:
✅ 步骤一:设置全场简单商品 15% 折扣(仅设 sale_price)
add_action('woocommerce_before_single_product', 'set_global_sale_price');
add_action('woocommerce_before_shop_loop', 'set_global_sale_price');
function set_global_sale_price() {
// 仅在前台执行,避免后台干扰
if (is_admin()) return;
// 使用全局过滤器动态计算 sale_price(不修改数据库)
add_filter('woocommerce_product_get_sale_price', 'apply_global_discount', 10, 2);
}
function apply_global_discount($sale_price, $product) {
if ($product->is_type('simple') && $product->get_regular_price() > 0) {
$sale_price = round($product->get_regular_price() * 0.85, 2); // 15% off → 85% of regular
}
return $sale_price;
}✅ 步骤二:确保模板正确渲染原价 + 折扣价
WooCommerce 默认的 woocommerce/templates/loop/price.php 和 single-product/price.php 已内置对 is_on_sale() 的判断,并自动输出如下结构:
<span class="price"> <del aria-hidden="true"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>99.99</span></del> <ins><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>84.99</span></ins> </span>
你只需添加 CSS 即可高亮视觉对比:
.woocommerce span.woocommerce-Price-amount del {
color: #999;
text-decoration: line-through;
}
.woocommerce span.woocommerce-Price-amount ins {
font-weight: 700;
color: #e74c3c;
}⚠️ 注意事项
- ❌ 不要覆盖 woocommerce_product_get_regular_price 钩子——否则原价丢失,is_on_sale() 将始终返回 false;
- ✅ sale_price 为动态计算时,get_price_html() 仍能正确识别促销状态并渲染双价;
- ? 如需在自定义模板中手动输出,可用:
if ($product->is_on_sale()) { echo '<span class="regular-price">' . wc_price($product->get_regular_price()) . '</span>'; echo '<span class="sale-price">' . $product->get_price_html() . '</span>'; } - ? 此方案兼容 WooCommerce 6.0+,无需修改数据库,刷新即生效,且不影响后台价格管理。
通过该方法,你既能实现灵活的全局折扣策略,又能充分利用 WooCommerce 原生的价格展示逻辑,确保用户体验专业、合规且可维护。

















