0
votes

We want to hide "Have a coupon? Add one..." on WooCommerce checkout, if a coupon is already added OR when a customer add a coupon on the checkout page.

Currently we habe this code below and it works when a customer enters a coupon on the cart page and then navigate to the checkout page. In this case, the "Have a coupon? Add one..." message is not visible. If no coupon in added on the cart page the message is visible.

This is working fine! But it does not work when a customer adds a coupon on the checkout page.

1.) We get the message "Coupon added" But the coupon message to add one is still visible AND also the coupon is not calculated in the order table. => After a page refresh is everything correctly.

2.) When a coupon is removed by the customer on checkout, then we get the message that the coupon is removed, but the discount is still visible in the order table. => After a page refresh it displays everything right again.

So now I'm trying to refresh the page after a coupon was added or removed. But I have problems to get the right event. I guess we must do this via js? Or is there a PHP approach?

add_filter( 'woocommerce_coupons_enabled', 'woocommerce_coupons_enabled_checkout' );

function woocommerce_coupons_enabled_checkout( $coupons_enabled ) {
  
    global $woocommerce;
    
    if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
        return false;
    }
    return $coupons_enabled;
}
1
Normally when adding a coupon on cart or on checkout, the applied coupon is displayed and totals tare refreshed… So if it's not the case, there is something else that is making trouble, like your theme, a plugin or some other custom code made by you…LoicTheAztec
Thanks. Yeah normally... The only way to check that is to deactivate all or?Nik7
Yes try that making a DB backup before…LoicTheAztec
It is comming from the payment plugin. So I have to contact the developer. ThanksNik7
Is there a way to found out what part of the plugin made this problem? So maybe I can share this info to them in order that they can fix this faster.. it's a pain :(Nik7

1 Answers

0
votes

Your code should be like this

add_filter( 'woocommerce_coupons_enabled', 'woocommerce_coupons_enabled_checkout' );

function woocommerce_coupons_enabled_checkout( $coupons_enabled ) {
  if(is_checkout()){
    global $woocommerce;
    
    if ( ! empty( $woocommerce->cart->get_applied_coupons() ) ) {
        $coupons_enabled = false;
    }
    }
    return $coupons_enabled;
}

Edit: okay you need to check if page is checkout or cart then run the script. I have added condition in code.