3

I am adding coupons in woocommerce programmatically, and is working quite fine via ajax from another page, not the checkout page. However, everytime I go to the checkout page, it says, "Coupon has been applied", but I already applied the coupon from a different page. How do I disable this message from showing when going to the checkout page, after I have performed my ajax for applying a coupon dynamically? Is there some sort of setting or function to call to disable this message from showing on the cart page when a coupon has already been applied from an ajax function?

Here's my php for applying the coupon via ajax from a separate page (not the checkout page):

if (!WC()->cart->add_discount( sanitize_text_field( $coupon_code )))
{
    $notices = wc_get_notices();
    if (!empty($notices) && isset($notices['error'])) {
        $last_fail = end($notices['error']);
        echo $last_fail;
    }
    die();
}
else
{
    $notices = wc_get_notices();

    // Get last element of array only!
    if (!empty($notices) && isset($notices['success'])) {
        $last_success = end($notices['success']);
        echo $last_success;
    }
    die();
}

And this works fine. As you can see, I grab errors and have them returned within the response of the ajax call, and output errors that way. I also output something for success, which is fine. The problem I'm having is that after this function executes via ajax, and I browse to the checkout page to see my product and coupon code that was applied, it puts notice at the top of the page, saying "Coupon applied Successfully." and I don't want this notice to appear, but I don't want to get rid of all notices, just this one, if the coupon was not applied on the checkout page, there is no need for this wc_notice to appear when browsing to the checkout page.

How to tell woocommerce not to apply this coupon notice to the checkout page when I browse to the checkout page after adding coupons manually from another page (via ajax)?

Solomon Closson
  • 6,111
  • 14
  • 73
  • 115

3 Answers3

3

You can use the "woocommerce_coupon_message" filter to hide the success message for the coupon. As you want to remove it for checkout page only, just check if current page is checkout page or not.

You can use the following code :

function remove_msg_filter($msg, $msg_code, $this){
if(is_checkout()){
    return "";
}
return $msg;
}

add_filter('woocommerce_coupon_message','remove_msg_filter',10,3);

Note : If you also want to change message for error then use "woocommerce_coupon_error" filter.

Domain
  • 11,562
  • 3
  • 23
  • 44
  • I tried this, but it doesn't seem to be working. Coupon notices still showing on checkout page. – Solomon Closson Nov 25 '15 at 16:26
  • I also notice that the notices keep piling up, they never get cleared after each coupon submission. So, I find myself having to grab the last `notices['error']` using `end(notices['error']);` to get the error specific to that coupon being added. – Solomon Closson Nov 25 '15 at 19:41
  • I hope you have written add_filter outside the ajax function. Using this, will remove all the coupon message on checkout page. As this filter is provided within the add_coupon_message() which is called by add_discount() function, this should work for you. – Domain Nov 26 '15 at 09:40
  • I'm sorry, but this filter doesn't work. I'm not using it inside of the ajax function. I am simply adding it to the themes `functions.php` file, but it doesn't get rid of the coupon messages at all. They are still there and they are still stacking on top of 1 another when I visit the checkout page. – Solomon Closson Nov 29 '15 at 22:50
  • I've tried on both, `woocommerce_coupon_message` and `woocommerce_coupon_error`, neither of them work. – Solomon Closson Nov 29 '15 at 22:57
  • BTW, I managed to fix this, simply by adding: `wc_clear_notices();` before `die();` in both cases. Thanks anyways. – Solomon Closson Nov 29 '15 at 23:14
1

The way to fix this is to add wc_clear_notices(); before die(); in the function call, so as to remove all notices that the function add_discount adds to the array that gets returned from wc_get_notices() function call.

if (!WC()->cart->add_discount( sanitize_text_field( $coupon_code )))
{
    $notices = wc_get_notices();
    if (!empty($notices) && isset($notices['error'])) {
        $last_fail = end($notices['error']);
        echo $last_fail;
    }
    wc_clear_notices();
    die();
}
else
{
    $notices = wc_get_notices();

    // Get last element of array only!
    if (!empty($notices) && isset($notices['success'])) {
        $last_success = end($notices['success']);
        echo $last_success;
    }
    wc_clear_notices();
    die();
}
Solomon Closson
  • 6,111
  • 14
  • 73
  • 115
  • why this reply is setted as the correct one but have -1 votes? is this the right one or not? – rollsappletree Jan 23 '18 at 08:44
  • Probably because the correct way to handle this is actually different from the actual way to handle it. And I've answered my own question which worked for my situation. – Solomon Closson Apr 12 '19 at 15:04
-1

This way may be a bit of a lazy way of approaching this. But if you inspect element of the page, find the message, target it and add 'display:none;' in the CSS.

That should do the trick. Try and make it specific for that page only. A unique class is usually page-id found at the top of the page in inspect element

TJDev
  • 133
  • 3
  • 13
  • Sorry, but doesn't seem there is even a coupon specific class to target here, to be able to hide just coupon-related notices. I don't want to hide all notices. Just those related to coupons on the checkout page only. – Solomon Closson Nov 25 '15 at 16:35