2

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

Here's my 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 );
    }

}

This code seems to work when adding the 5percent discount, but once I go over €100 it doesn't apply the 10percent discount.

It just keeps applying the 5percent discount.


UPDATE:

This code works like a charm. Credit goes to 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 );
    }
}
Arne De Belser
  • 121
  • 4
  • 13

1 Answers1

2

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • As you said, I've encoutered this. I was updating my code and I got everything working. Like if someone removed an item from the cart, the discount was being applied correct. But from the moment I leave to cart, to go back to the shop and add another item, it doesn't update the percentage discount. – Arne De Belser Sep 07 '17 at 06:18
  • 1
    I will try this and come back to you. First of all thank you for your time, it's probably going to help me out a lot. – Arne De Belser Sep 07 '17 at 06:37
  • Tested and your code works perfectly. Upvoted! Now let me try to figure out what you did. Still learning PHP etc, so it's a good practice to dissect the code and learn to understand it. – Arne De Belser Sep 07 '17 at 06:42
  • I got a few questions regarding this code, just to clarify. 1. You use $percent which has a numeric value , but where does it get specified how it applies this % discount to the total value? Is this some kind of variable that WordPress recognizes in $cart_object? 2. In the last phrase you write -$discount, why is there a ' - ' in front of the dollar sign? I've been looking at [this source](http://woocommerce.wp-a2z.org/oik_api/wc_cartadd_fee/) to understand 'add_fee' – Arne De Belser Sep 07 '17 at 08:02
  • @ArneDeBelser 1) The discount is applied to the WC_Cart `cart_contents_total` property (cart total value), but does not use other cart totals as they are calculated with this discount and are not available in this hook. The available methods and properties for the WC_Cart object are [**HERE**](https://docs.woocommerce.com/wc-apidocs/class-WC_Cart.html) … 2) I use a negative fee => so a discount… add_fee is used to add a surcharge normally. A negative surcharge is a discount. – LoicTheAztec Sep 07 '17 at 08:56