WooCommerce: Display Cart Item Subtotal With Coupon Discount

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 ๐Ÿ™‚

Show price after coupon discount@ WooCommerce Cart page

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;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: How to Fix the “Cart is Empty” Issue
    For some reason, sometimes you add products to cart but the cart page stays empty (even if you can clearly see the cart widget has products in it for example). But don’t worry – it may just be a simple cache issue (and if you don’t know what cache is that’s no problem either) or […]
  • WooCommerce: Remaining $$$ to Get Free Shipping Notification @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. So, here’s how we show a simple message on […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]

Rodolfo Melogli

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. Follow @rmelogli

52 thoughts on “WooCommerce: Display Cart Item Subtotal With Coupon Discount

  1. This is awesome, thank you.

  2. 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

    1. 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!

  3. 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?

    1. 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!

  4. 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.

  5. 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

  6. Don’t normally comment but thanks! Really helped us out

    1. Excellent!

  7. not working

    1. Please expand on that?

  8. 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.

    1. 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!

  9. Thank you, Rodolfo, for this tip, I really appreciate it!!

    1. You’re welcome!

    1. Welcome!

  10. Just the article I was looking for. thank you

    1. Great!

  11. Bookmarked – Thank you Rodolfo

    1. Awesome!

  12. 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

    1. 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

  13. 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!

    1. Ahah excellent Andrei ๐Ÿ™‚ Sure, feel free to use the contact form if you need custom work. Thank you!

  14. 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.

    1. Thank you Ami ๐Ÿ™‚

  15. 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.

    1. I’m sure there is – have you found anything worth sharing? ๐Ÿ™‚

  16. 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!

    1. Bogdan, thanks so much for your comment! Did you change the code of the coupon in the PHP snippet?

    2. 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?

      1. 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 ๐Ÿ™‚

    3. Ohhh, thank you! Never thought of that!

    4. 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!

      1. 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

  17. Hello,
    if i want to add 2 different coupon codes, how will it be possible. btw thank you for the snippet, awesome work..

    1. 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

  18. 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!

    1. 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

  19. 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

    1. Thank you Chris ๐Ÿ™‚

  20. How can we use this function, but using the coupon codes already established, rather than a percentage off as per your code?

    1. 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

  21. 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

    1. 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!

  22. Excellent, thanks!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *