I am applying automatically a coupon when there is a product id 1362 in the cart, but when someone adds another product and delete the 1362 the coupon stays applied, how to prevent this by removing the coupon if there is no 1362 product id in the cart with Woocommerce ?
I know we can restrict coupon to a product but i don't want this, i want my coupon to be applied to all products cart only if there is the product with id 1362 in this cart.
add_action( 'woocommerce_before_cart', 'bbloomer_apply_matched_coupons' );
function bbloomer_apply_matched_coupons() {
global $woocommerce;
$coupon_code = 'boxpersonnalisable';
if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;
foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
// this is your product ID
$autocoupon = array( 1362 );
if( in_array( $values['product_id'], $autocoupon ) ) {
add_filter('woocommerce_coupon_message','remove_msg_filter',10,3);
$woocommerce->cart->add_discount( $coupon_code );
wc_print_notices();
}
}
}
Thank you very much