1

I have two coupons, one $10 another $5. I want do next, if price be less $50 and customer enter discount coupon $10 replace this coupon by coupon $5.

Example:

The customer added an item in cart with total price $40 and he applied the coupon $10-Off. Now I need replace current coupon with $10-Off to my another coupon $5-Off and continue checkout.

What is the best and simple way I can use? I try change discount price like in code below and this working, but in other places WC show me old coupon and calculate all data from old coupon (in order page etc) so I really need REPLACE one coupon to another ( not calculate their price ) when this coupon added. It like customer enter $5-Off coupon directly to discount field

// Change price for discount
if( ! function_exists('custom_discount') ){

  function custom_discount( $price, $values, $instance ) {

    //$price represents the current product price without discount
    //$values represents the product object
    //$instance represent the cart object

    //Get subtotal
    $subtotal = WC()->cart->get_subtotal();
    $coupon = WC()->cart->get_discount_total();

    //if price < 50 and we have active coupon then we need decrease price to $5
    if( $subtotal < 50 && ! empty( WC()->cart->get_applied_coupons() ) ){
      $price = $subtotal  - 5; //Wrong way ...
    }
    return $price;
  }

  add_filter( 'woocommerce_get_discounted_price', 'custom_discount', 10, 3);

}
4
  • Can't you just create coupon with a minimum spend of for example, $50 as in the question? I mean, that can be easily set when you create the coupon.
    – Sally CJ
    Commented Mar 18, 2020 at 2:22
  • @SallyCJ Thank you for comment. I already have two coupons $5 (not public, nobody know about this coupon) and $10 (public, users can use it). I want check if price < $50, and user try use coupon of $10, replace this coupon to smaller coupon to not allow user use $10 coupon for price which be less then $50 Commented Mar 18, 2020 at 3:16
  • Can I get an explanation for the minus of question? Commented Mar 19, 2020 at 12:32
  • I don't know, I didn't downvote the question or the current answer.
    – Sally CJ
    Commented Mar 19, 2020 at 14:56

2 Answers 2

1
+50

This requires to use an action hook like woocommerce_calculate_totals that is related to cart and Ajax enabled, because it will handle all changes made by the customer in cart page:

add_action( 'woocommerce_calculate_totals', 'discount_based_on_cart_subtotal', 10, 1 );
function discount_based_on_cart_subtotal( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetitions
    if ( did_action( 'woocommerce_calculate_totals' ) >= 2 )
        return;

    // Your coupons settings below
    $coupon_05_off = '$5-Off';
    $coupon_10_off = '$10-Off';

    // When cart subtotal is below 50
    if ( $cart->subtotal < 50 ) {
        if ( $cart->has_discount( $coupon_10_off ) ) {
            $cart->remove_coupon( $coupon_10_off ); // Remove $10.00 fixed discount coupon
            if ( ! $cart->has_discount( $coupon_05_off ) ) {
                 $cart->apply_coupon( $coupon_05_off ); // Add $5.00 fixed discount coupon
            }
        }

    }
    // When cart subtotal is up to 50
    else {
        if ( $cart->has_discount( $coupon_05_off ) ) {
            $cart->remove_coupon( $coupon_05_off ); // Remove $5.00 fixed discount coupon
            if ( ! $cart->has_discount( $coupon_10_off ) ) {
                $cart->apply_coupon( $coupon_10_off ); // Add $10.00 fixed discount coupon
            }
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and work.

5
  • I think hook woocommerce_calculate_totals more correct instead woocommerce_before_cart in previous answer. Thank you! Commented Mar 19, 2020 at 18:04
  • 1
    This looks like it will add a coupon even if the customer didn't have the $10 code already?
    – Rup
    Commented Mar 19, 2020 at 18:06
  • @Rup good point, I think we need remove last if ( ! $cart->has_discount( $coupon_10_off ) ) because if user not enter any coupon it always will get $10 coupon right? Commented Mar 19, 2020 at 18:09
  • @BrotheryuraUpdated my code… It should work now as you expect. Commented Mar 19, 2020 at 18:13
  • 1
    @LoicTheAztec now all fine thank you, I will mark this answer like right answer. Commented Mar 19, 2020 at 18:17
0

I have tested this and can confirm that it works, even after you change the products in the cart to be either below or over $50.

<?php
/**
 * @snippet Add / Remove A Coupon Dynamically based on Cart Subtotal with WooCommerce
 * @sourcecode http://wpharvest.com
 * @author Dragos Micu
 * @compatible WooCommerce 2.4.7
 */
add_action( 'woocommerce_calculate_totals', 'wpharvest_apply_coupons' );
function wpharvest_apply_coupons($cart) {
    global $woocommerce;
    // Set your coupon codes
    $coupon10off = '$10-Off';
    $coupon5off = '$5-Off';

    // Get the cart subtotal
    $cart_subtotal = $cart->subtotal;
    // If cart subtotal is less than 50 add or remove coupons
    if ($cart_subtotal < 50) {
        if ( $woocommerce->cart->has_discount( $coupon10off ) ) {
            WC()->cart->remove_coupon( $coupon10off );
            $woocommerce->cart->add_discount( $coupon5off );
        }
    }
    // If cart subtotal is greater 49 add or remove coupons
    if ($cart_subtotal > 49 ) {
        if ( $woocommerce->cart->has_discount( $coupon5off ) ) {
            WC()->cart->remove_coupon( $coupon5off );
            $woocommerce->cart->add_discount( $coupon10off );
        }
    }
}
3
  • Can I get an explanation for the minus? Commented Mar 19, 2020 at 9:09
  • hi thx for your answer, I don't know who minus your answer like minus my question too, I will check soon your variant. In first view it must working. Thank you! Commented Mar 19, 2020 at 11:53
  • Thank you again @Dragos Micu, your answer very good and working, but LoicTheAztec has more detailed answer, hope next time I will select your answer! Commented Mar 19, 2020 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.