title: Auto apply or remove a coupon in Woocommerce cart for a specific product id
body: <p>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 ?</p>
  
  <p>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.</p>
  
  <pre><code>add_action( 'woocommerce_before_cart', 'bbloomer_apply_matched_coupons' );
  
  function bbloomer_apply_matched_coupons() {
      global $woocommerce;
      $coupon_code = 'boxpersonnalisable'; 
      if ( $woocommerce-&gt;cart-&gt;has_discount( $coupon_code ) ) return;
      foreach ( $woocommerce-&gt;cart-&gt;cart_contents as $key =&gt; $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-&gt;cart-&gt;add_discount( $coupon_code );
          wc_print_notices();
      }
      }
  }
  </code></pre>
  
  <p>Thank you very much</p>