Rasmus Bundsgaard
Rasmus Bundsgaard

Reputation: 75

Limit cart to only use 1 coupon - woocommerce

how can I limit my coupon so a customer won't be able to use multiple coupon on the same checkout?

I'm using WP Woocommerce 4.2.2

Upvotes: 3

Views: 8683

Answers (2)

Nico Real
Nico Real

Reputation: 129

The following code will remove the first applied coupon, if customer applies another coupon code, so there will be only one applied coupon in cart (the last applied coupon):

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

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

    // For more than 1 applied coupons only
    if (  sizeof($cart->get_applied_coupons()) > 1 && $coupons = $cart->get_applied_coupons() ){
        // Remove the first applied coupon keeping only the last appield coupon
        $cart->remove_coupon( reset($coupons) );
    }
}

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

Upvotes: 0

Skatox
Skatox

Reputation: 4284

You can do this by activating the option Individual use only, at the coupon editing form. This will block the use of additional coupons.

For more information check the official documentation.

Upvotes: 3

Related Questions