Posted on

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        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @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 this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

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.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  10. Excellent, thanks!

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.