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

Woocommerce登录用户同时显示商品原价与促销价功能修改求助

完整修改后代码

// Variable and simple product displayed prices
add_filter( 'woocommerce_get_price_html', 'custom_get_price_html', 20, 2 );
function custom_get_price_html( $price, $product ) {
    // 未登录用户仅显示原价
    if( !is_user_logged_in() ){
        if( $product->is_type('variable') ){
            $reg_min = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('min') ) );
            $reg_max = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('max') ) );
            if( $reg_min != $reg_max ){
                if( $reg_min == 0 && $reg_max > 0 ){
                    $price = wc_price( $reg_max );
                }elseif( $reg_min > 0 && $reg_max == 0 ){
                    $price = wc_price( $reg_min );
                }else{
                    $price = wc_format_price_range( $reg_min, $reg_max );
                }
            }else{
                if( $reg_min > 0 ) $price = wc_price( $reg_min );
            }
        }elseif( $product->is_type('simple') ){
            $reg_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
            if( $reg_price > 0 ) $price = wc_price( $reg_price );
        }
        return $price;
    }

    // 已登录用户显示划掉原价+促销价
    if( $product->is_type('variable') ){
        // 取原价区间
        $reg_min = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('min') ) );
        $reg_max = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('max') ) );
        // 取促销价区间
        $sale_min = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_sale_price('min') ) );
        $sale_max = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_sale_price('max') ) );

        // 格式化原价
        if( $reg_min != $reg_max ){
            if( $reg_min == 0 && $reg_max > 0 ){
                $reg_html = wc_price( $reg_max );
            }elseif( $reg_min > 0 && $reg_max == 0 ){
                $reg_html = wc_price( $reg_min );
            }else{
                $reg_html = wc_format_price_range( $reg_min, $reg_max );
            }
        }else{
            $reg_html = $reg_min > 0 ? wc_price( $reg_min ) : '';
        }

        // 格式化促销价
        if( $sale_min != $sale_max ){
            if( $sale_min == 0 && $sale_max > 0 ){
                $sale_html = wc_price( $sale_max );
            }elseif( $sale_min > 0 && $sale_max == 0 ){
                $sale_html = wc_price( $sale_min );
            }else{
                $sale_html = wc_format_price_range( $sale_min, $sale_max );
            }
        }else{
            $sale_html = $sale_min > 0 ? wc_price( $sale_min ) : '';
        }

        // 拼接价格:划掉原价+促销价
        if( $reg_html && $sale_html ){
            $price = '<del class="original-price">' . $reg_html . '</del> <ins class="sale-price">' . $sale_html . '</ins>';
        }elseif( $sale_html ){
            $price = $sale_html;
        }
    }elseif( $product->is_type('simple') ){
        $reg_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
        $sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
        if( $reg_price > 0 && $sale_price > 0 ){
            $price = '<del class="original-price">' . wc_price($reg_price) . '</del> <ins class="sale-price">' . wc_price($sale_price) . '</ins>';
        }elseif( $sale_price > 0 ){
            $price = wc_price($sale_price);
        }
    }
    return $price;
}

// Product Variation displayed prices
add_filter( 'woocommerce_available_variation', 'custom_variation_price', 10, 3);
function custom_variation_price( $data, $product, $variation ) {
    $reg_price = wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) );
    $sale_price = wc_get_price_to_display( $variation, array( 'price' => $variation->get_sale_price() ) ) ;

    if( is_user_logged_in() ){
        if( $reg_price > 0 && $sale_price > 0 ){
            $data['price_html'] = '<del class="original-price">' . wc_price($reg_price) . '</del> <ins class="sale-price">' . wc_price($sale_price) . '</ins>';
        }else{
            $data['price_html'] = wc_price( $sale_price );
        }
    }else{
        $data['price_html'] = wc_price( $reg_price );
    }
    return $data;
}

// Set the correct prices in cart
add_action( 'woocommerce_before_calculate_totals', 'set_item_cart_prices', 20, 1 );
function set_item_cart_prices( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ){
        if( ! is_user_logged_in() ){
            $cart_item['data']->set_price( $cart_item['data']->get_regular_price() );
        }
    }
}

说明

  • 代码直接替换你当前使用的旧代码即可生效,无需额外配置
  • 价格样式默认继承WooCommerce原生的划掉原价、高亮促销价样式,如需自定义可以通过.original-price和.sale-price两个CSS类调整样式
  • 保留了原有的购物车、结算页价格逻辑,未登录用户无法享受促销价结算,符合活动规则

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 05:45:03