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

如何让WooCommerce阶梯折扣仅在购物车无折扣商品时生效?

解决WooCommerce阶梯折扣与促销商品叠加问题

要实现「购物车有折扣商品时忽略阶梯折扣,无折扣商品时正常启用」的需求,只需在原有代码中添加购物车折扣商品检测逻辑,具体修改如下:

修改后的完整代码

add_action('woocommerce_cart_calculate_fees', 'woo_discount_total');
function woo_discount_total(WC_Cart $cart) {
    // 后台非AJAX请求直接返回
    if( is_admin() && !defined('DOING_AJAX') ) {
        return;
    }

    // 检测购物车是否存在折扣商品
    $has_discounted_items = false;
    foreach ( $cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        // 判断商品是否有促销价,或是否通过优惠券/活动获得了价格减免
        if ( $product->is_on_sale() || $cart_item['line_subtotal'] != $cart_item['line_total'] ) {
            $has_discounted_items = true;
            break;
        }
    }

    // 如果有折扣商品,直接返回,不执行阶梯折扣
    if ( $has_discounted_items ) {
        return;
    }

    // 原有阶梯折扣逻辑
    $woo_current_price = $cart->subtotal;
    if($woo_current_price >= 3000 && $woo_current_price <= 9990) {
        $discount = $cart->subtotal * 0.02; // 2%折扣
        $cart->add_fee('Sale 2% ', -$discount);
    } elseif($woo_current_price >= 10000) {
        $discount = $cart->subtotal * 0.05; // 5%折扣
        $cart->add_fee('Sale 5% ', -$discount);
    }
}

代码说明

  • 折扣商品检测逻辑:遍历购物车所有商品,通过两个维度判断是否为折扣商品:
    1. $product->is_on_sale():识别商品是否设置了促销价(Sale Price)
    2. $cart_item['line_subtotal'] != $cart_item['line_total']:识别商品是否通过优惠券、批量折扣等方式获得了价格减免
  • 只要购物车存在任意一件折扣商品,就直接跳过阶梯折扣计算;反之则正常执行原有阶梯折扣规则。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 15:32:46