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

WordPress中如何获取WooCommerce常规价与促销价的折扣百分比?

WooCommerce折扣百分比计算错误排查与修复

常见错误原因及修复方案

1. 除以0的致命错误

当商品常规价$regular_price为0时,代码中($regular_price - $sale_price) / $regular_price会触发除以0的错误,直接导致页面报错。

修复:先判断常规价是否为有效数值且大于0,再执行计算:

add_filter('woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3);
function woocommerce_custom_sales_price($price, $regular_price, $sale_price)
{
    // 验证常规价有效性,避免除以0
    if (!is_numeric($regular_price) || $regular_price <= 0) {
        return $price;
    }
    
    $percentage = round(($regular_price - $sale_price) / $regular_price * 100) . '%';
    $percentage_txt = ' ' . __('You Save ', 'woocommerce') . $percentage;
    
    $formatted_regular = is_numeric($regular_price) ? wc_price($regular_price) : $regular_price;
    $formatted_sale = is_numeric($sale_price) ? wc_price($sale_price) . $percentage_txt : $sale_price . $percentage_txt;
    
    $price = '<del>' . $formatted_regular . '</del> <ins>' . $formatted_sale . '</ins>';
    return $price;
}

2. 价格参数非数值/异常值问题

部分场景下$regular_price或$sale_price可能传入非数值内容,或促销价大于等于常规价,导致计算结果异常。

修复:强制转换为数值并增加有效性校验:

add_filter('woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3);
function woocommerce_custom_sales_price($price, $regular_price, $sale_price)
{
    // 转换为浮点型,确保计算安全
    $regular_price = (float)$regular_price;
    $sale_price = (float)$sale_price;
    
    // 过滤无效价格组合
    if ($regular_price <= 0 || $sale_price < 0 || $sale_price >= $regular_price) {
        return $price;
    }
    
    $percentage = round(($regular_price - $sale_price) / $regular_price * 100) . '%';
    $percentage_txt = ' ' . __('You Save ', 'woocommerce') . $percentage;
    
    $formatted_regular = wc_price($regular_price);
    $formatted_sale = wc_price($sale_price) . $percentage_txt;
    
    $price = '<del>' . $formatted_regular . '</del> <ins>' . $formatted_sale . '</ins>';
    return $price;
}

3. 样式冲突问题

直接拼接<del>/<ins>标签可能和主题默认样式冲突,导致显示异常。

修复:添加自定义类名,方便样式控制:

add_filter('woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3);
function woocommerce_custom_sales_price($price, $regular_price, $sale_price)
{
    $regular_price = (float)$regular_price;
    $sale_price = (float)$sale_price;
    
    if ($regular_price <= 0 || $sale_price >= $regular_price) {
        return $price;
    }
    
    $percentage = round(($regular_price - $sale_price) / $regular_price * 100) . '%';
    $percentage_txt = '<span class="you-save-tag">' . __('You Save ', 'woocommerce') . $percentage . '</span>';
    
    $formatted_regular = wc_price($regular_price);
    $formatted_sale = wc_price($sale_price) . $percentage_txt;
    
    $price = '<del class="product-regular-price">' . $formatted_regular . '</del> <ins class="product-sale-price">' . $formatted_sale . '</ins>';
    return $price;
}

额外注意事项

  • 代码需添加到主题functions.php文件或自定义插件中,避免主题更新丢失。
  • 测试时选择已设置常规价和促销价的商品,验证折扣百分比计算是否正确。
  • 若使用多货币插件,需确保价格参数为当前货币的有效数值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 17:25:15