I am trying to create a function to make coupon codes conditional in the cart and checkout pages.
The conditions are as follows: (1) if couponX is applied remove couponY if it exists. (2) if couponY is applied remove couponX if it exists.
The possibility to add and remove more than one coupon would be helpful also.
I have this, and it seems to work well except it wont allow me to add overstock10 coupon after save5today is added.
add_action( 'woocommerce_before_calculate_totals', 'auto_add_remove_coupon' );
function auto_add_remove_coupon( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$save5today = 'save5today';
$overstock10 = 'overstock10';
if ( $cart->has_discount( $save5today ) ) {
$cart->remove_coupon( $overstock10 );
wc_clear_notices();
wc_add_notice( __("Your custom notice - coupon added (optional)","woocommerce"), 'notice');
} elseif ( $cart->has_discount( $overstock10 ) ) {
$cart->remove_coupon( $save5today );
wc_clear_notices();
wc_add_notice( __("Your custom notice - coupon removed (optional)","woocommerce"), 'notice');
}
}