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

WooCommerce代码仅在商店页显示最低价格,单品页失效求排查

问题排查与修复:WooCommerce可变产品单页不显示最低价格

问题原因分析

你的代码仅在商店页面生效、单产品页面失效,核心原因有三点:

  • 单产品页面的可变价格渲染依赖woocommerce_get_price_html过滤器,而你的代码仅挂钩了列表页常用的woocommerce_variable_sale_price_html和woocommerce_variable_price_html
  • 主题自定义了单产品页的价格模板(比如single-product/price.php),跳过了WooCommerce默认的过滤器触发逻辑
  • 过滤器默认优先级(10)过低,被主题或其他插件的高优先级钩子覆盖

修复方案

方案1:补充挂钩单产品页价格过滤器

修改代码,新增单产品页专属的价格钩子,并添加产品类型判断避免影响非可变产品:

add_filter( 'woocommerce_variable_sale_price_html', 'wpglorify_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wpglorify_variation_price_format', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'wpglorify_variation_price_format', 10, 2 ); // 新增单产品页钩子

function wpglorify_variation_price_format( $price, $product ) {
    // 仅处理可变产品
    if ( ! $product->is_type('variable') ) {
        return $price;
    }

    // 获取最低销售价
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    $price = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    // 获取最低常规价
    $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
    sort( $prices );
    $saleprice = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    // 拼接删除线原价与现价
    if ( $price !== $saleprice ) {
        $price = '<del>'. $saleprice . '</del> <ins>' . $price .  '</ins>';
    }
    return $price;
}

add_filter('woocommerce_show_variation_price', function() { return TRUE;});

方案2:检查主题自定义模板

如果你的主题存在single-product/price.php自定义文件,打开后确认:

  • 是否调用了wc_get_price_html( $product )标准函数,而非硬编码价格
  • 若为硬编码,替换为WooCommerce默认函数,确保过滤器能正常触发

方案3:提高过滤器优先级

如果其他插件/主题的钩子覆盖了你的逻辑,将过滤器优先级从10改为99,确保你的代码最后执行:

add_filter( 'woocommerce_variable_sale_price_html', 'wpglorify_variation_price_format', 99, 2 );
add_filter( 'woocommerce_variable_price_html', 'wpglorify_variation_price_format', 99, 2 );
add_filter( 'woocommerce_get_price_html', 'wpglorify_variation_price_format', 99, 2 );

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 03:45:32