WooCommerce disable 2 coupons to be applied in specific 2 coupons only

Viewed 21

How can I execute such a scenario:

when a customer enters coupon A,

if coupon B is already applied to the shopping cart,

then coupon A cannot be applied at the same time.

I know that there is an option to define that one coupon cannot be applied with other coupons, but I do want to be able to applied 2 coupons in the same time, but only make the restriction to specific cases only.

1 Answers

Place the following code in your atcive theme functions.php

add_filter( 'woocommerce_coupon_message', 'filter_woocommerce_coupon_message', 10, 3 );
function filter_woocommerce_coupon_message( $msg, $msg_code, $coupon ) {
    $coupon_code_a = 'couponA';
    $coupon_code_b = 'couponB';

    if ( WC()->cart->has_discount( $coupon_code_a ) ) {
        $msg = 'You cant use CouponA while CouponB is applied';
    }
    if ( WC()->cart->has_discount( $coupon_code_b ) ) {
        $msg = 'You cant use CouponB while CouponA is applied';
    }
    return $msg;
}

add_action( 'woocommerce_before_cart','apply_coupons_conditionaly');
function apply_coupons_conditionaly() {
    $coupon_code_a = 'couponA';
    $coupon_code_b = 'couponB';
    if ( WC()->cart->has_discount( $coupon_code_a ) ) {
        WC()->cart->remove_coupon( $coupon_code_b );
    }
    if ( WC()->cart->has_discount( $coupon_code_b ) ) {
        WC()->cart->remove_coupon( $coupon_code_a );
    }
    
}
Related