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

WooCommerce迷你购物车实现原价划线折扣价显示方法

问题描述

现有代码已经实现了两档基于购买数量的阶梯折扣规则,通过过滤器钩子在购物车页面实现了常规原价添加删除线、折扣价突出展示的效果,需要在迷你购物车(mini cart)模块中实现完全一致的视觉展示效果。
效果参考

现有实现代码

add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 9999 );

function bbloomer_quantity_based_pricing( $cart ) {
    
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
    
    // 定义折扣阈值和规则
    $threshold1 = 2; // 购买数量≥2触发第一档折扣
    $discount1 = 0.05; // 第一档折扣:单价减5%
    $threshold2 = 3; // 购买数量≥3触发第二档折扣
    $discount2 = 0.1; // 第二档折扣:单价减10%
    
    // 遍历购物车商品计算折扣
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) 
    {
        if( has_term( array('cartucce-inkjet'), 'product_cat', $cart_item['product_id'] ) ){
            if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) 
            {
                $price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );          
                $cart_item['data']->set_price( $price );
            } 
            elseif ( $cart_item['quantity'] >= $threshold2 ) 
            {
                $price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
                $cart_item['data']->set_price( $price );
            }    
        }
    }   
}

function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
    // 获取商品对象
    $product = $cart_item['data'];
    
    if ( is_a( $product, 'WC_Product' ) ) {
        // 获取商品原价
        $regular_price = $product->get_regular_price();
        // 获取折扣后单价
        $new_price = $cart_item['data']->get_price();

        // 当原价和折扣价不一致时,输出带删除线原价+突出折扣价的格式
        if ( ! empty ( $regular_price ) && $regular_price != $new_price ) {
            $price_html = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $new_price ) . '</ins>';        
        }
    }
    return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );
问题原因

阶梯定价逻辑挂载在woocommerce_before_calculate_totals钩子上,属于全局生效的逻辑,迷你购物车计算价格时已经能正确拿到折扣后的实际价格,但现有价格格式化(原价加删除线、折扣价突出)逻辑只绑定到了普通购物车页的woocommerce_cart_item_price过滤器,迷你购物车渲染商品价格时不会触发这个钩子,因此不会展示对应样式。

解决方案

不需要修改原有阶梯定价逻辑,只需要新增一段针对迷你购物车的价格格式化代码,复用和购物车页完全一致的HTML结构即可,将以下代码追加到现有代码的末尾:

// 迷你购物车实现和购物车页一致的原价删除线+折扣价突出效果
add_filter( 'woocommerce_widget_cart_item_quantity', 'custom_minicart_discount_price_display', 10, 3 );
function custom_minicart_discount_price_display( $output, $cart_item, $cart_item_key ) {
    $product = $cart_item['data'];
    if ( is_a( $product, 'WC_Product' ) ) {
        $regular_price = $product->get_regular_price();
        $discounted_price = $cart_item['data']->get_price();
        $item_quantity = $cart_item['quantity'];
        
        // 仅当存在折扣时替换展示格式,保持和购物车页完全一致的HTML结构
        if ( ! empty( $regular_price ) && $regular_price != $discounted_price ) {
            $formatted_price = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $discounted_price ) . '</ins>';
            // 保留迷你购物车默认的「数量 × 单价」结构
            $output = '<span class="quantity">' . sprintf( '%s &times; %s', $item_quantity, $formatted_price ) . '</span>';
        }
    }
    return $output;
}

注意事项

  • 上述代码基于WooCommerce官方默认迷你购物车模板编写,如果主题对迷你购物车模板做了自定义重写,钩子名称可能存在差异,需要对应主题模板调整
  • 代码生效后,迷你购物车的价格样式会和购物车页完全一致,自动继承主题已有的<del>删除线、<ins>折扣价样式,不需要额外写CSS
  • 原有折扣计算逻辑不需要做任何调整,全局所有购物车场景(结算页、迷你购物车、购物车页)都会自动应用对应阶梯折扣

内容的提问来源于stack exchange,提问作者Giorgio Di Raimondo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.03 10:21:39