This is a nice follow up from last week’s snippet “WooCommerce: Slashed Cart Subtotal if Coupon @ Cart“, where I showed how to display original/discounted cart total on the same totals table row.
This time, I want to let users know the original and discounted cart item (product) amount after a certain coupon is applied. Who knows – this might improve your Cart U/X ๐
PHP Snippet: Display Cart Item Subtotal After Coupon Discount @ WooCommerce Cart
/** * @snippet Cart item subtotal slashed if coupon @ Cart * @how-to Get CustomizeWoo.com FREE * @sourcecode https://businessbloomer.com/?p=21881 * @author Rodolfo Melogli * @testedwith WooCommerce 3.3.3 */ add_filter( 'woocommerce_cart_item_subtotal', 'bbloomer_if_coupon_slash_item_subtotal', 99, 3 ); function bbloomer_if_coupon_slash_item_subtotal( $subtotal, $cart_item, $cart_item_key ){ global $woocommerce; // Note: use your own coupon code here $coupon_code = 'barmada'; if ( $woocommerce->cart->has_discount( $coupon_code )) { // Note: apply your own coupon discount multiplier here // In this case, it's a 99% discount, hence I multiply by 0.01 $newsubtotal = wc_price( $cart_item['data']->get_price() * 0.01 * $cart_item['quantity'] ); $subtotal = sprintf( '<s>%s</s> %s', $subtotal, $newsubtotal ); } return $subtotal; }
This is awesome, thank you.
You’re welcome
Hello Sir, this is the solution that I was looking for. But its only on the cart, is it possible to add the function to thank you page? Looking forward to your reply
Hi Dhea, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello,
Tried it but it didnt work. Do i have to replace barmada by my own coupon? The problem is that i have hundreds of coupons( for news letters) so is it possible to make it work with any coupon?
Hello Fourkan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
I don’t normally comment but this helped out with an issue that I’ve been having on my Woocommerce website for days… Much appreciated Rodolfo.
Thank you Dieter!
Hi Rodolfo!
You’re doing a great job with all these WooCommerce snippets, resources, and services. I took advantage of this snippet (and a couple more ๐ ) and adapted it with some more options and tweaks. It’s not for usage with a particular coupon, but with all active, or available, coupon(s).
So, to contribute back, here’s the link to the code in this gist:
https://gist.github.com/Micemade/96c239b188a230552c2bfcfc9a350f7f
I hope you or someone else will make use of it.
(disclaimer: use it at your own risk, no guarantees! ๐ )
Cheers,
Alen
Cool!
Don’t normally comment but thanks! Really helped us out
Excellent!
not working
Please expand on that?
Hi Rodolfo,
I would like to ask how to make sure that cart subtotal and total are updated after this filter ?:)
As code works and discounts are applied to items, but cart totals stays the same.
Hi Marius, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Thank you, Rodolfo, for this tip, I really appreciate it!!
You’re welcome!
Thank you
Welcome!
Just the article I was looking for. thank you
Great!
Bookmarked – Thank you Rodolfo
Awesome!
Thanks a lot Rodolpho!
Can one just copy-paste several times your snippet for it to work with several different coupon codes or it wouldn’t work that way?
Best,
P
Hey Paul, thanks for your comment! No, it’s a little more complex than that – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
How to do do!
Rodolfo!
This is very good!!!
Today,I complete my web site by using your description.
Thank!
If you have any problem…
Can i chat with you about it?
Thank you!
Ahah excellent Andrei ๐ Sure, feel free to use the contact form if you need custom work. Thank you!
Thanks!
I believe this doesn’t take into account the quantities.
So I changed:
$newsubtotal = wc_price( $cart_item[‘data’]->get_price() * 0.8333333 * $cart_item[‘quantity’] );
And now seems to be ok.
Thank you Ami ๐
There has to be a simpler and more universal way to do this. Instead of putting the discount code and discount amount in the code itself, wouldn’t it be easier just to simply strikeout the subtotal and display the total next to it?
That way, it works with any coupons you have and you don’t have to manually add them in your functions.php.
I’m sure there is – have you found anything worth sharing? ๐
Hello,
Thank you for your dedication and very insightful advices on how to create a best experience for our woocommerce basesd web sites!
I wanted to use your snippet but unfortunately it did not work.
Wordpress 4.9.1, Woo 3.2.6
Here are two screenshots I took to show you this problem. Altough the web site is in Romanian I hope the images are self-explanatory.
https://www.dropbox.com/s/3a173lpiumtvi9j/My_carrt.JPG?dl=0
https://www.dropbox.com/s/l5b2m6jbs3nnfne/functions_php.JPG?dl=0
I am looking forward for your answer!
Bogdan, thanks so much for your comment! Did you change the code of the coupon in the PHP snippet?
Hello! I am glad I could be helpful. No, I did not change any part (or punctuation) of the code. Do you think it might be because of php version, which is 7?
Uhm, sorry, I didn’t explain well! You need to change the coupon code inside the snippet (‘barmada’) to your own coupon code. That should fix your problem ๐
Ohhh, thank you! Never thought of that!
๐
However I still have a confusion: I created a coupon, flat discount per product and set it’s value at $10. If I add a product in my cart valued at $159,99, for example, and I apply the coupon code, the slashed price is $1,6 in the cart but in then Cart total area it deductes $10.
In your code you wrote:
// In this case, it’s a 99% discount, hence I multiply by 0.01
$newsubtotal = wc_price( $cart_item[‘data’]->get_price() * 0.01 );
How can I modify it in order to display the value $149,99?
For better understanding I added two pictures:
https://www.dropbox.com/s/xe30wnbhlp1kktq/Coupon_1.JPG?dl=0
https://www.dropbox.com/s/oqatzyzdnupjrty/Coupon_2.JPG?dl=0
Thank you!
Bogdan, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hello,
if i want to add 2 different coupon codes, how will it be possible. btw thank you for the snippet, awesome work..
Hey Tanmoy, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
This works great, how ever it shows a percent on all items, even the ones that are not included in the coupon’s discount. Is there a way to precise this for coupon codes on product categories? Thanks!
Alice, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Awesome, this works so well!
I suggest you just show users a way to also hide the subtotal as this can confuse clients.
Regards
Chris
Thank you Chris ๐
How can we use this function, but using the coupon codes already established, rather than a percentage off as per your code?
Jim, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hi Rodolfo,
Thanks for the snippet. Allways nice to get your ideas and snippets to try out.
I will probably not use this snippet however, since I find the resulting user experience a bit confusing, and the user experience for me is key. The screencap above here shows an item-price and an amount and then without explaination the price is lowered and I don’t know from that screencap why this is happening. So before reading the text at first glance it allready confused me and I had to go to the explaination and code to understand what precisely you where doing here and still wondered why the productprice wasn’t cut also, or instead, so the subtotal would make more sense.
The normal woocommerce interface is build like an invoice, where you can just follow what is added and multiplied one afther the other, after which a discount is applied. So there I can just multiply, add and subtract the numbers as they come, but now it breaks that flow.
In the snippet last week I had simmilar reservation. Normally it shows the subtotal before discount as a starting point after which it presents the discount value and the added delivery cost. After the snippet is applied the discount is allready included in the subtotal, so if I get to the next line and try to apply that discount things don’t add up anymore. I can’t subtract and add and get to the total payable amount anymore.
So as a visitor I have to think and conclude that the discount is allready subtracted and I don’t have to apply the following amount as a discount anymore, but how do I know that is the case except for noticing that it otherwise doesn’t add up? Like the book says: don’t make me think.
In the result of this snippet I have to think also because I have to understand why the subtotal is not the item-price times the amount and have to understand what caused the discount in the list. I might, but why make me think?
Last but not least, I’m not that keen about hardcoding a discount amount (or anything that is also part of the db and managed by the user or admin). There allready are enough ways things can go wrong whithout me having to remember to change a snippet when I wanto change a discount. It just doesn’t seem best practice.
Never mind, offcourse, since one is free to use the snippet or not.
I like it as a coding example though. ๐
Thanks for the good work!
Cheers, Hans
Hans, your feedback is much appreciated! Whether this is useful or not, at least the coding part is a good lesson itself ๐ Keep in touch!
Excellent, thanks!
๐