2

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Playzare
  • 93
  • 3
  • 13

1 Answers1

4

Here is the way to make it work when:

  • adding a specific coupon code when a specific product is added to cart
  • removing a specific applied coupon code when a specific product is removed from cart
  • (in both cases you can display a custom notice)…

The code:

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;

    $coupon_code = 'boxpersonnalisable';
    $targeted_product_ids = array( 1362 );
    $found = false;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ){
        if( in_array( $cart_item['product_id'], $targeted_product_ids ) ){
            $found = true;
            break;
        }
    }

    if ( ! $cart->has_discount( $coupon_code ) && $found ) {
        $cart->add_discount( $coupon_code );
        wc_clear_notices();
        wc_add_notice( __("Your custom notice - coupon added (optional)","woocommerce"), 'notice');
    } elseif  ( $cart->has_discount( $coupon_code ) && ! $found ) {
        $cart->remove_coupon( $coupon_code );
        wc_clear_notices();
        wc_add_notice( __("Your custom notice - coupon removed (optional)","woocommerce"), 'notice');
    }
}

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello, thanks for your code ! But i have 2 bugs, one was an error 500 adding product to the cart, i solved it by deleting the `&& !defined('DOING_AJAX')` but also another problem when you delete the product 1362 from the cart when reloading the checkout button, the total disappear of the cart , think there is a problem somewhere ? – Playzare Apr 09 '18 at 19:52
  • @Playzare Sorry I have updated the code… The problem was not `&& !defined('DOING_AJAX')` but wrong `wc_add_notices()` that need to be `wc_add_notice(` *(without an `s`)* … **Try it works perfectly now…** – LoicTheAztec Apr 09 '18 at 21:12
  • thanks guy for your time but there is always the bug on the checkout and cart total invisible think there is another problem :/ – Playzare Apr 09 '18 at 21:39
  • Thanks works perfectly on your website ! You are the man ! I will look why on my website i have a problem with this code :) thx again ! – Playzare Apr 09 '18 at 22:02
  • 1
    yeah thanks find out the cart was stuck with a product even if clearing cookies was not enough i had to implement a function to clear my cart, now your code works perfectly, thanks again Loic ! – Playzare Apr 09 '18 at 22:14