2

In Woocommerce, I would like to limit the number of coupons allowed at checkout so customer can only use 2 coupons in total, but no more.

I plan to have the coupons usable in conjunction so they can combine any 2 but I do not want them to be able to use an unlimited number of coupons together.

How to limit the number of applied coupons in Woocommerce?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
user3183430
  • 21
  • 1
  • 5

1 Answers1

4

To limit the number of allowed applied coupons, you will use the following that will output an error notice avoiding coupous to be applied when the limit is reached:

add_action( 'woocommerce_applied_coupon', 'custom_applied_coupon', 10, 1 );
function custom_applied_coupon( $coupon_code ){
    $applied_coupons = WC()->cart->get_applied_coupons();
    $coupons_allowed_max = 2; // <=== 2 coupons allowed max

    if( sizeof($applied_coupons) > $coupons_allowed_max ) {
        WC()->cart->remove_coupon( $coupon_code );
        wc_clear_notices();
        wc_print_notice(__("You can only have two applied coupons max…", "woocommerce"), 'error');
    }
}

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

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399