Home > Mobile >  Disable "cart needs payment" for a list of coupon codes in WooCommerce
Disable "cart needs payment" for a list of coupon codes in WooCommerce

Time:08-24

I would like to disable 'cart needs payment' for a list of discount codes:

  • coupon1, coupon2, coupon3, etc..

I have found this code by @LoicTheAztec, and it works fine, but only for a specific discount code:

add_filter( 'woocommerce_cart_needs_payment', 'filter_cart_needs_payment', 10, 2 );
function filter_cart_needs_payment( $needs_payment, $cart  ) {
    // The targeted coupon code
    $targeted_coupon_code = 'coupon1';

    if( in_array( $targeted_coupon_code, $cart->get_applied_coupons() ) ) {
        $needs_payment = false;
    }
    return  $needs_payment;
}

To disable "cart needs payment" for a list of discount codes, I applied the following modification to the existing code:

$targeted_coupon_code = array('coupon1', 'coupon2', 'coupon3');

Unfortunately without the desired result. Any advice?

CodePudding user response:

in_array() is used to check if a certain value exists in an array, to computes the intersection of arrays u can use array_intersect() instead

So you get:

function filter_woocommerce_cart_needs_payment( $needs_payment, $cart ) {
    // Add your coupon codes, several can be entered, separated by a comma
    $targeted_coupon_codes = array( 'coupon1', 'coupon2', 'coupon3' );

    // Coupon code is applied
    if ( array_intersect( $targeted_coupon_codes, $cart->get_applied_coupons() ) ) {
        $needs_payment = false;
    }
    
    return $needs_payment;
}
add_filter( 'woocommerce_cart_needs_payment', 'filter_woocommerce_cart_needs_payment', 10, 2 );
  • Related