i want to hide/remove specific messages from woo commerce without modifying basic woocommerce plugin. There are several types of messages related to coupon like
- Coupon code already applied!
- Sorry! Coupon 12345 already applied to your cart. (here i essentially want to hide coupon code)
and several others similar to these coupon codes.
i just want to hide these type of coupon/cart messages, others are fine like "Product successfully added!" or any other error messages.
Basically the aim is to show all other messages (Error and success messages) but dont wanna show coupon messages and a coupon code in to these messages.
So, is there any way to do this by doing any hook etc, like one i've found to eliminate all message strings (if i am not wrong).
add_filter( 'woocommerce_coupon_message', '__return_empty_string' );
- One more thing is, one message is repeating on cart page several times when i add product in to the car. "Coupon code already applied!" 2,3, to 4 times.
I know this is an old thread, but I think I have just worked out how to do this for the coupon error messages.
On my store I use WooCommerce Smart Coupons which allows coupons to be sold as gift cards. I don't want people to be able to purchase gift cards using a gift card, so I have added the gift card category to the exclude category list on the coupon usage restriction.
Anyway, I wanted to alter the error message if somebody tried to use a gift card coupon code when there was a gift card in the cart. This is the code I used:
The
$err_code
for specific error messages can be found in the filewoocommerce/includes/class-wc-coupon.php
.In my case, I wanted to edit error message for error code 114 (E_WC_COUPON_EXCLUDED_CATEGORIES). I also wanted my custom message to only appear if a 114 error was triggered by a gift card being in the cart, not for every 114 error. To solve this I added in the
if ( in_array( '15', $categories ) )
. What this does is check to see if the error message has been triggered by a product that is in category 15 (this being the gift card category for my store. Change 15 to whichever category you are using).The
$instance
variable is what WooCommerce uses to pass the details of the cart/coupon back to the function.I'm very new to coding, so I am sorry if my code and my explanation isn't great, but it definitely appears to work for me. I added it into
functions.php
.So the crux of your question is - how can I customise WooCommerce coupon messages?
I have half an answer - I've customised coupon messages (the green boxed ones) using the "woocommerce_coupon_message" filter. BUT I've not yet been able to get the coupon error messages (the red boxed ones) working using the "woocommerce_coupon_error" filter.
I've tried conditional statements based on a few different methods from Class WC_Cart to no avail. I just cant seem to "intercept" (and then customise) the error messages before they're printed. If somebody has a solution to Coupon Errors I'd be happy to hear it.
Anyhow... the below function is hooked into the "woocommerce_before_cart" & "woocommerce_before_checkout_form" actions so the function works for either page.
It can obviously be customised to no end, but my example basically tests for a valid coupon and then you can change the message or return nothing. You can also test for other conditions to throw up custom notices of all sorts! Much better then modifying template files! :-)
Okay, found solution
go to woocommerce tempaltes, copy notices folder and edit the desired template, in my case its
error.php
copy/edit code