0

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;
}
Nik7
  • 346
  • 2
  • 16
  • 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 Jan 13 '21 at 17:01
  • Thanks. Yeah normally... The only way to check that is to deactivate all or? – Nik7 Jan 13 '21 at 17:33
  • 1
    Yes try that making a DB backup before… – LoicTheAztec Jan 13 '21 at 19:52
  • 1
    It is comming from the payment plugin. So I have to contact the developer. Thanks – Nik7 Jan 13 '21 at 23:35
  • 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 Jan 14 '21 at 16:04
  • Enable wordpress debug: https://stackoverflow.com/questions/61740111/how-to-debug-in-woocommerce-3/61754061? – LoicTheAztec Jan 14 '21 at 17:18

1 Answers1

0

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.

Claymore
  • 130
  • 4
  • Thanks. Now the error is solved with that code. But it does not work as expected. Maybe this is a theme issue GeneratePress or so.. – Nik7 Jan 13 '21 at 17:33
  • 1
    what is the issue now ? I updated my post 2 times. may be you did not copy the latest code. – Claymore Jan 13 '21 at 17:33
  • The problem is, that after add a coupon on checkout page, the page is not refreshing. Also with the code it does not :( I can implement the code with no fatal error. However, the checkout page does not refresh. – Nik7 Jan 13 '21 at 18:15
  • add this in javascript `jQuery( document.body ).on( 'updated_checkout', function(){ location.reload(); });` – Claymore Jan 13 '21 at 20:07