2

I'm trying to set up a coupon on my client's WooCommerce site so that a percentage discount applies if the total cart is below a cap amount or a fixed amount is equal or greater than the cap amount.

Let's say that the cap for the cart total is 200. If the cart total is below this cap, 10% discount is applied. But if the cart total is 200 or greater, then the fixed amount of 20 is applied as the discount.

For example:

  • My cart total is 190. Since this is less than the cap of 200, the discount amount is computed as 10%, which is 19 is applied
  • My cart total is 210. Since this is greater than the cap of 200, then the fixed amount of 20 is applied.

How do I set my WooCommerce to apply a percentage discount or fixed cart depending on the total?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Jun Dolor
  • 609
  • 2
  • 11
  • 25

2 Answers2

5

You can use a custom function hooked in woocommerce_before_calculate_totals action hook where you will define 2 coupons codes:

  • A percentage discount coupon code (of 10%)
  • A fixed amount discount coupon code (of $20)

The code:

add_action( 'woocommerce_before_calculate_totals', 'auto_add_coupons_total_based', 10, 1 );
function auto_add_coupons_total_based( $cart ) {

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

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // HERE define your coupon code
    $coupon_percent = 'uget10percent'; # <===  <===  <===  <===  <===  <===
    $coupon_fixed = 'uget20off'; # <===  <===  <===  <===  <===  <===  <===

    // Get cart subtotal
    $subtotal = 0;
    foreach($cart->get_cart() as $cart_item ){
        $subtotal += $cart_item['line_subtotal'];
        $subtotal += $cart_item['line_subtotal_tax']; // with taxes
    }

    // Coupon type "percent" (less than 200)
    if( $subtotal < 200 && ! $cart->has_discount( $coupon_percent ) ){
        // If coupon "fixed amount" type is in cart we remove it
        if( $cart->has_discount( $coupon_fixed ) )
            $cart->remove_coupon( $coupon_fixed );

        // Apply the "percent" type coupon code
        $cart->add_discount( $coupon_percent );
    }
    // Coupon type "fixed amount" (Up to 200)
    elseif( $subtotal >= 200 && ! $cart->has_discount( $coupon_fixed ) ) {
        // If coupon "percent" type is in cart we remove it
        if( $cart->has_discount( $coupon_percent ) )
            $cart->remove_coupon( $coupon_percent );

        // Apply the "fixed amount" type coupon code
        $cart->add_discount( $coupon_fixed );
    }
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works.

If you want to apply it on subtotal without taxes you will have to comment this line:

$subtotal += $cart_item['line_subtotal_tax']; // with taxes

OR you can also use a negative fee (so a discount) instead of coupons this way:

add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_total', 25, 1 );
function discount_based_on_total( $cart ) {

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

    $total = $cart->cart_contents_total;

    // Percentage discount (10%)
    if( $total < 200 )
        $discount = $total * 0.1;
    // Fixed amount discount ($20)
    else
        $discount = 20;

    // Add the discount
    $cart->add_fee( __('discount', 'woocommerce'), -$discount );
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works.

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • This looks promising - I get the idea how the code works, I'll give it a try. I forgot to mention that the discounting rule will apply only to products of a specific category. It looks like I'll do some tweaking on the code. Thanks :-) – Jun Dolor Jan 16 '18 at 13:42
  • @JunDolor Once you will have tried this, let me know which solution is the best for you. For coupons you can limit each coupon itself for specific product categories or product Ids … This can be done in the 2nd function, but it should be a new question. – LoicTheAztec Jan 16 '18 at 21:35
  • Thanks @LoicTheAztec I've tried the first option and it worked. However, the coupon is applied immediately. I would prefer that the user would enter the coupon code and apply before the code is run. I tried using the hook woocommerce_applied_coupon, but could not get the code to run. I'm still trying to figure a work around. I'll provide updates – Jun Dolor Jan 18 '18 at 15:20
  • 1
    Noted on this @LoicTheAztec, your answer is now accepted, (and appreciated :-) ). I'll prepare the follow-up question. Thanks – Jun Dolor Jan 19 '18 at 12:49
  • @JunDolor This is what I exactly looking. Have you adjusted the discounting rule will apply only to products of a specific category? If so how to achieve this? I tried to apply the progressive discount by including product ID's but that is not working. – Vinayagam Sep 20 '18 at 17:22
  • @LoicTheAztec - How to achieve the same for a specific category of products? – Vinayagam Sep 20 '18 at 17:26
0

Ok so I finally figure out how to use this code so that it only triggers when the coupon is added. I also added a script to notify the buyer that the coupon discount limit has been reached

add_action( 'woocommerce_before_calculate_totals', 'auto_add_coupons_total_based', 10, 1 ); function auto_add_coupons_total_based( $cart ) {

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

// HERE define your coupon code
$coupon_percent = 'xyz20'; # <===  <===  <===  <===  <===  <===
$coupon_fixed = 'fixedamount'; # <===  <===  <===  <===  <===  <===  <===

// Get cart subtotal
$subtotal = 0;
foreach($cart->get_cart() as $cart_item ){
    $subtotal += $cart_item['line_subtotal'];
    $subtotal += $cart_item['line_subtotal_tax']; // with taxes
}

//Set HERE the limit amount
$limit = 40; //without Tax


// Coupon type "percent" (less than 200)
if( $subtotal < 200 && ! $cart->has_discount( $coupon_percent ) ){
    // If coupon "fixed amount" type is in cart we remove it
    if( $cart->has_discount( $coupon_fixed ) )
        $cart->remove_coupon( $coupon_fixed );


}
// Coupon type "fixed amount" (Up to 200)
if( $subtotal >= 200 && $cart->has_discount( $coupon_percent ) ) {
    // If coupon "percent" type is in cart we remove it
    if( $cart->has_discount( $coupon_percent ) )
        $cart->remove_coupon( $coupon_percent );

    // Apply the "fixed amount" type coupon code
    $cart->add_discount( $coupon_fixed );


    // Displaying a custom message
        $message = __( "The total discount limit of $$limit has been reached", "woocommerce" );
        wc_add_notice( $message, 'notice' );



}

}

shaun
  • 11
  • 1