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

为什么修改woocommerce_variation_prices_price的$price后sale_price等于regular_price

问题原因
  • 你写的过滤函数存在基础逻辑缺陷:没有为不满足current_user_can('test')的场景设置返回值,函数会默认返回null,触发WooCommerce价格校验的异常 fallback 逻辑。
  • WooCommerce的变体价格存在联动校验规则:正常销售状态下,price字段值与sale_price(活动期)或regular_price(非活动期)保持一致。你仅修改woocommerce_variation_prices_price的返回值后,校验逻辑发现price与原sale_price不匹配,会判定当前变体不处于活动状态,自动将sale_price重置为regular_price。
  • 不建议在价格过滤器中调用wc_delete_product_transients,每次读取价格都删除瞬态缓存会严重降低店铺性能,缓存仅需在变体价格修改时清空即可。
修复方案

同时添加两个过滤器,分别处理自定义价格逻辑和保留原始sale_price:

  1. 修正价格修改过滤器,补全默认返回逻辑
add_filter('woocommerce_variation_prices_price', 'theme_get_variation_price', 99, 3 );
function theme_get_variation_price($price, $variation, $product){
    if ( current_user_can('test') ){
        return $price - 100;
    }
    // 非目标用户场景返回原始价格
    return $price;
}
  1. 添加sale_price过滤器,强制保留原始活动价,避免被系统重置
add_filter('woocommerce_variation_prices_sale_price', 'theme_preserve_variation_sale_price', 99, 3 );
function theme_preserve_variation_sale_price($sale_price, $variation, $product){
    return $sale_price;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 02:06:03