Progressive discount based on cart total in WooCommerce

后端 未结 1 679
抹茶落季
抹茶落季 2020-12-16 08:47

I\'m trying to automatically apply 3 different coupon codes in WooCommerce Cart.

Here\'s my code!

add_action( \'woocommerce_before_cart\', \'apply_ma         


        
相关标签:
1条回答
  • 2020-12-16 09:27

    Using multiple coupons with different cart percentage discount is a nightmare as you have to handle when customer add new items, remove items, change quantities and add (or remove) coupons…

    You should better use this simple code below, that will add a cart discount based on cart total amount (Here we use a negative fee which is a discount):

    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 );
        }
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and works.

    0 讨论(0)
提交回复
热议问题