【问题标题】:Progressive discount based on cart total in WooCommerce基于 WooCommerce 中购物车总数的累进折扣
【发布时间】:2017-09-06 11:30:15
【问题描述】:

我正在尝试在 WooCommerce 购物车中自动应用 3 个不同的优惠券代码。

这是我的code

add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );

function apply_matched_coupons() {
    global $woocommerce;

$coupon_code5 = '5percent';
$coupon_code10 = '10percent';
$coupon_code55 = '15percent';

if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

    if ( $woocommerce->cart->cart_contents_total >= 50 && $woocommerce->cart->cart_contents_total < 100 && $woocommerce->cart->cart_contents_total != 100 ) {

        $woocommerce->cart->add_discount( $coupon_code5 );

    } elseif ($woocommerce->cart->cart_contents_total >= 100 && $woocommerce->cart->cart_contents_total < 150 && $woocommerce->cart->cart_contents_total != 150 ) {

        $woocommerce->cart->add_discount( $coupon_code10 );

    } else {

        $woocommerce->cart->add_discount( $coupon_code15 );
    }

}

添加 5% 折扣时,此代码似乎有效,但一旦我超过 100 欧元,它就不会应用 10% 折扣。

它只是继续应用 5% 的折扣。


更新:

这段代码就像一个魅力。归功于LouicTheAztek

add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );
function progressive_discount_based_on_cart_total( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_total = $cart_object->cart_contents_total; // Cart total

    if ( $cart_total > 150.00 )
        $percent = 15; // 15%
    elseif ( $cart_total >= 100.00 && $cart_total < 150.00 )
        $percent = 10; // 10%
    elseif ( $cart_total >= 50.00 && $cart_total < 100.00 )
        $percent =  5; // 5%
    else
        $percent = 0;

    if ( $percent != 0 ) {
        $discount =  $cart_total * $percent / 100;
        $cart_object->add_fee( "Discount ($percent%)", -$discount, true );
    }
}

【问题讨论】:

    标签: php wordpress woocommerce cart discount


    【解决方案1】:

    使用具有不同购物车百分比折扣的多张优惠券是一场噩梦,因为当客户添加新商品、删除商品、更改数量以及添加(或删除)优惠券时,您必须处理......

    您最好使用下面这个简单的代码,它将根据购物车总金额添加购物车折扣(这里我们使用负费用,即折扣)

    add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );
    function progressive_discount_based_on_cart_total( $cart_object ) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        $cart_total = $cart_object->cart_contents_total; // Cart total
    
        if ( $cart_total > 150.00 )
            $percent = 15; // 15%
        elseif ( $cart_total >= 100.00 && $cart_total < 150.00 )
            $percent = 10; // 10%
        elseif ( $cart_total >= 50.00 && $cart_total < 100.00 )
            $percent =  5; // 5%
        else
            $percent = 0;
    
        if ( $percent != 0 ) {
            $discount =  $cart_total * $percent / 100;
            $cart_object->add_fee( "Discount ($percent%)", -$discount, true );
        }
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    此代码已经过测试并且可以工作。

    【讨论】:

    • 正如你所说,我已经遇到了这个。我正在更新我的代码,一切正常。就像有人从购物车中删除了一件商品一样,折扣是正确应用的。但是从我离开购物车的那一刻起,回到商店并添加另一件商品,它并没有更新百分比折扣。
    • 我会试试这个,然后回来找你。首先感谢您的宝贵时间,这可能会对我有很大帮助。
    • 经过测试,您的代码运行良好。点赞!现在让我试着弄清楚你做了什么。还在学习 PHP 等,所以剖析代码并学习理解它是一个好习惯。
    • 我有几个关于这段代码的问题,只是为了澄清一下。 1.您使用具有数值的 $percent ,但它在哪里指定如何将此百分比折扣应用于总价值?这是 WordPress 在 $cart_object 中识别的某种变量吗? 2. 最后一句你写-$discount,为什么美元符号前面有一个'-'?我一直在查看this source 以了解“add_fee”
    • @ArneDeBelser 1) 折扣应用于 WC_Cart cart_contents_total 属性(购物车总值),但不使用其他购物车总值,因为它们是使用此折扣计算的,并且在此挂钩中不可用. WC_Cart 对象的可用方法和属性是HERE ... 2)我使用负费用=> 所以折扣... add_fee 用于正常添加附加费。负附加费就是折扣。
    猜你喜欢
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 2019-01-31
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    相关资源
    最近更新 更多