You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"
Does this snippet (still) work?
Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.
Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!
Business Bloomer Founder
Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.
52 thoughts on “WooCommerce: Display Cart Item Subtotal With Coupon Discount”
This is awesome, thank you.
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!
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!
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:
I hope you or someone else will make use of it.
(disclaimer: use it at your own risk, no guarantees! 😉 )
Don’t normally comment but thanks! Really helped us out
Please expand on that?
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!!
Just the article I was looking for. thank you
Bookmarked – Thank you Rodolfo
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?
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!
This is very good!!!
Today,I complete my web site by using your description.
If you have any problem…
Can i chat with you about it?
Ahah excellent Andrei 🙂 Sure, feel free to use the contact form if you need custom work. Thank you!
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? 🙂
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.
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:
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
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.
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
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!
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!