If you love Ecommerce as much as I do, and are passionate about Sales Conversion Rate and reducing Shopping Cart Abandonment, today’s snippet will come in handy.
Besides, this is officially the first guest blog on Business Bloomer (have ideas? Send me your proposal here)… so let me officially introduce you to today’s author: Jamie Gill, a WordPress & WooCommerce enthusiast from Bradford, UK.
Jamie managed to code a handy snippet to display inside Cart and Checkout totals the total amount of money a customer saved (sale prices plus coupon discounts). Over the years this snippet went through several revisions, but it’s still working smoothly – enjoy!
PHP Snippet: Show How Much Customer Saved @ WooCommerce Cart and Checkout Pages
/**
* @snippet Display Total Discount @ WooCommerce Cart/Checkout
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli, BusinessBloomer.com
* @testedwith WooCommerce 8
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_cart_totals_after_order_total', 'bbloomer_show_total_discount_cart_checkout', 9999 );
add_action( 'woocommerce_review_order_after_order_total', 'bbloomer_show_total_discount_cart_checkout', 9999 );
function bbloomer_show_total_discount_cart_checkout() {
$discount_total = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
if ( $product->is_on_sale() ) {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$discount = ( (float)$regular_price - (float)$sale_price ) * (int)$values['quantity'];
$discount_total += $discount;
}
}
if ( $discount_total > 0 ) {
echo '<tr><th>You Saved</th><td data-title="You Saved">' . wc_price( $discount_total + WC()->cart->get_discount_total() ) .'</td></tr>';
}
}
I adjusted code for customers with tax in their end prices on WooCommerce.
use it here:
Haven’t tested it, but thanks!
Hi Rodolfo,
Thanks for sharing your snippet! It works great! However I have a slight issue. I translated “You saved” with cyrillic letters in functions.php as my shop is in Bulgarian language, but it appears correct only on desktop. On mobile it keeps showing “You saved”. Can you share some workaround in order to make it appear correctly on both – desktop and mobile? I don’t see an option to translate it with Loco translate.
Thanks!
Hi Angel! You also need to change the “You Saved” string inside the “data-title” TD attribute, because that’s what displays in the Cart page on mobile. Hope this helps!
Yes! Just make sure to change the data-title value as well
Hi Rodolfo,
you snippet works great, but I have an issue on mobile. My shop is in Cyrillic and after changing the “You saved” to Cyrillic it updates only on desktop. Is it possible to change it for mobile as well?
Not sure if this is still active, but I was wondering if I can put a – (minus) sign before the discount number. Thanks in advance!
Of course you can!
Thank you Rodolf, but I have been receiving an error only on my cart page, not my checkout saying A non-numeric value encountered for the line
Any idea on how to fix this?
Thank you John, you’re right, those are considered strings by WooCommerce so PHP throws an error. Try with the updated version and let me know.
How to add that “You save” text in the cart window, not the page ?
Hello Fred, 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 love this works great on desktop and on tablet but on mobile phone I just get
“: $10”
or whatever the amount is not
“You Saved: $10”
as seen on Desktop and tablet
Any pointers ??
Glyn
This is on the Cart Page.. The Checkout shows correct on all devices
Thanks Glyn. I’ve revised the snippet, take a look
Magic many thanks now works on all … desktop, tablet and mobile.
Glyn
Yay!
I use Woocommerce Dynamic Pricing to apply global discounts. This means that the discount is not on a product level, but on a category level. So the product technically does not have a discount, so the line
[PHP]$discount = ( $regular_price * $sale_price ) * $values[‘quantity’];[PHP]
Never did anything, because $sale_price assumes it’s the discounted price. But since the product has no sale price on a product level $sale_price equals 0.
So in the end, $regular_price – 0 was multiplying with $values, and the You saved: line was way beyond the real saving.
Anyway, what I did was define $sale_price as a decimal value that matched my global dynamic price. This is a VERY specific use, but I thought it could be useful for anyone in my situation.
Nice
Hi there,
You are awsome thanks for your blog!
I added this code to my functions.php but i noticed i get an error in my cart saying:
Warning: A non-numeric value encountered in /home/****/functions.php on line 84
I checked this line of code and found it was this line:
$discount = ($regular_price – $sale_price) * $values[‘quantity’];
I also noticed the total savings amount is multiplied by 10…
Any idear to what’s going on here?
Kind regards,
Dominic
Hi Dominic, I’ve revised the snippet, can you check again please?
Hello! Thanks so much for this, works perfectly! Any way that I could add this into my customer emails?
Thank you!
Hi Joanne, 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!
how to do dis in flatsome…whn trying dis code ,getting error
Hi there, 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!
Is there a way to make this work with Booster for WooCommerce? I’m not getting the rate conversion applied to the discount when using a different currency on a single product.
Hi Leandro, 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!
Hi, I noticed in this snippet, it shows the discount amount (Price – sale price), plus I see you have some code to get the discount when a coupon is applied: wc_price( $discount_total + $woocommerce->cart->discount_cart )
However, the result of this is to display the discount (price – sale price) plus the coupon saving excluding tax. This would cause an issue if you display prices including tax. I changed the code line to the below and it seemed to work, although I am not a developer so not 100% sure it is the correct syntax. Nonetheless, I thought it is worth pointing out so that users can get the snippet to work correctly.
wc_price( $discount_total + ($woocommerce->cart->discount_cart + $woocommerce->cart->discount_cart_tax) )
Regards
Sam
Sorry I did not add the correct code tags in my first message : (
Please see below but correctly:
Hi, I noticed in this snippet, it shows the discount amount (Price โ sale price), plus I see you have some code to get the discount when a coupon is applied:
However, the result of this is to display the discount (price โ sale price) plus the coupon saving excluding tax. This would cause an issue if you display prices including tax. I changed the code line to the below and it seemed to work, although I am not a developer so not 100% sure it is the correct syntax. Nonetheless, I thought it is worth pointing out so that users can get the snippet to work correctly.
Regards
Sam
Excellent thank you!
Hi,
How do I get the% sign to the left of the price when showing the discount rate? Woocemmerce original (ex 50%). What I want to do (example %50)
Thanks.
Hello Kamil, 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 think you forget to calculate variable products:
Thank you, I haven’t tested so I trust you ๐
How can we show this total savings on order page in backend. The ‘Total Discount” should show ‘Total Savings’ + ‘Coupon Discount’ + ‘Any additional Discount manually added’
Hi there – 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
I changed your code.
I have the bundles on my site and they did not count correctly.
I get the total amount of products regular and subtract the total amount of the cart and shipping – so I get a discount.
Thanks Yuriy ๐
This code adds nothing to my cart page at all.
I am struggling, as I have the Dynamic Pricing plugin installed – and neither of these code sets work. All it does is add the regular price and displays it next to the “You Saved”. Driving me nuts! lol!
Thanks so much for sharing! Was encountering a php error with the original code, but this code now fixed everything! ๐
Works perfect!
Brilliant ๐
Works like a charm. Thank you!
Thank you ๐
Hi Rodolfo
Thank you very much for this Nice code.
How can i display the sum of regular prices before the discount ?
for Example:
Total Regular Price: 200$
You Save: 50$ // ( This is total discount)
Total: 150$ // (Total you pay)
~R
Mani, 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,
I don’t know why but the code does not work for my site. Any help, please? ๐
Hey David, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?
Hey guys, probably a stupid question, but how do I get the discount amount to show above the total, not below it?
Hey Thian, thanks for your comment! This resource should help: https://businessbloomer.com/woocommerce-visual-hook-guide-cart-page/
I’m using the snippet to let people donate the discount of an order (for a NGO). I did make a form with an input field to make a donation. I want to set as standard value the total discount of the order. What part of the code do I need to realize this? The code is already added to functions.php.
Bussink, 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, and thank you so much. Your snippets are so great! I have a small question about this snippet. I’ d like to know how to display the total saving in cart page in case of applied coupon. Please help me. Bye bye from Italy!
Ciao Fabrizio, I hear it’s crazy warm over there ๐ 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..
After wc 3.0, you can edit code for regular and sale price.
Thank you so much! Snippet updated ๐
hi . thanks for share
how add this after price in single product page?
thanks
Ehsan, 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 again
I think that I solved the problem. I share the code with you:
Nice, thanks Fernando!
Thank you! You are a genius!
Fernando,
the only problem with this code is that when no bulk discounts are selected via Dynamic Pricing, the code shows a saving or the subtotal. Is there a way you can fix that – if it is not too much trouble? I will really appreciate it!!
Let me know – thanks Fernando!
Regards
Charles.
I would also want to know about that.
High Rodolfo
I am trying to apply this snippet with woocommerce dynamic pricing. The problem is that the discounted price is inside a private array:
Thanks in advance
Hi Rodolfo,
This works exactly as it should for the items with a sale price. But, not only are the products on my site on sale, if a customer purchases 6 or more bottles of wine, I have pricing rules setup to further discount the wine 10%.
When I use your code, the cart page displays the sale price crossed out and then the 10% off price, but obviously the “You Saved” total is only based on the sale savings and not the 6 bottle discount.
Do you know how I can show only the 10% discount and disregard the sale discount?
Thanks!
Dan
Dan, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom to your bulk pricing plugin and I cannot provide a complementary solution here on the blog. Thanks a lot for your understanding! ~R
Hey, sometimes I get a 500 error when I view this site. I figured you would like to know.
Hey Shanelle thanks for your comment! I don’t get these 500 errors, so would you able to let me know when you get them and on what page if possible? Thanks so much!
Awesome code! Thank you, it works great!
Any chance to get this onto a product page where an item is on sale and below the price it says: “You save $x”
Thanks
Hey Red, thanks for your comment! Yes, you can calculate the discount on the product page with something similar to this: https://businessbloomer.com/woocommerce-display-discount-shop-loop-pages/ ๐
I have a simple question. How do I implement the code? do I copy and paste it on my function.php or how?
thanks
Indeed Edgar! You can place this in your child theme’s functions.php file – if you need more guidance, please take a look at this video tutorial: https://businessbloomer.com/woocommerce-customization-hangout/. Hope this helps!
Awesome Rodolfo! Glad I found your blog.
In the wp-admin orders it shows “Discount: 0 (zero)”, because as you know wordpress doesn’t consider sale vs regular price a discount (only considers coupons). Can you let us know how we can tweak the “Discount” to consider it or simply add the “you saved: (value)” there?
Thanks!
Hey Pedro, thanks for your comment! Would you be able to send me a screenshot please, I’m not entirely sure what you mean here I’m afraid ๐ Thanks!
Hi Rodolfo,
I used this snippet, but there is a small issue.. When i am having only a product with no sale price in the cart, is is showing the regular price as you saved amount..I used the latest updated snippet. Can you pls help me resolve this..
Hey Samrat thanks for your comment! Could you send me a screenshot to show me the error? Thanks!
Great code. Thank you. I am having a similar issue.
When there are non-discounted products in the cart, It looks like it is adding the savings on the discounted products to the full prices of the non-discounted products.
See screenshot here: https://dl.dropboxusercontent.com/u/17516976/screenshot-savings.png
Thank you Kyle for this! I will need a screenshot of the product settings as well, as this should work fine. Sure that no product has the sale price set, even if it’s the same as the regular price?
It behaves the same way at my shop. Might it be related to a woocommerce update? Did you manage to find a solution?
Samrat, Kyle, Sanya, I revised the code snippet. It should work now – let me know ๐
Any idea how to make this code work when the sale price is $0 or Free?
Hey Brad, thanks for your comment! Could you send me a screenshot please? Cheers!
Hi Rodolfo,
Thanks for the quick reply! I have attached screenshots.
Code does work when regular price is set to $20 and sale price to $0
https://freesunshields.com/wp-content/uploads/2016/11/Screen-Shot-2016-11-02-at-9.40.23-AM.png
It does work when sale price is set to $1
https://freesunshields.com/wp-content/uploads/2016/11/Screen-Shot-2016-11-02-at-9.41.00-AM.png
Awesome, super helpful. You’re right, and here’s the fix (I’m also going to update the snippet in a few minutes); substitute the line:
with:
Hope this helps ๐
That worked. Thank you so much!!
Awesome!!!
That worked. Thank you so much!!
Brilliant, thanks so much for your feedback Keyvan!
Hello thank you for your snipnet,
However It seems doesn’t work on my site
Screenshot here: https://prntscr.com/d1wf01 – sorry for Czech language but I hope you will understand. The total saved price is somehow even higher than total price…
I used your latest code as well.
Thank you so much for sharing and helping people!
Weird ๐ Can you completely empty the cart and re-do a test please? Cheers!
Hi , Thank you very much !
How can i change this snippets for woocommerce orders ( for Invoices )?
I think I would change The Foreacj part code , but i hav not idea !
Thanks alot sir
Hey Mani, thanks for your comment and feedback! Are you suggesting I should add the “Savings” to the WooCommerce order email notifications as well? If yes, that’s a very good idea – let me know ๐
Hi Rodolfo , Thanks a lot for your response .
Yes , I suggest to adding the ” Total Saving ” to E-mail Notifications and ” invoices ” .
I have purchased ” Woocommerce pip” Plugin for Order Invoices and this plugin have many Hooks To add custom functions and displays their value in the invoice Html page .
If we have a custom function to add in the functions.php file and that returns the ” Total Saving ” based on the order content ( not the cart content ) , then we can add that function to each other hooks Like E-mails and Html Invoice of woocommerce invoice plugins .
Thank You very much !
Hello Mani, I just researched the task a bit and it’s not as easy as I thought.
First thing, the “savings” need to be saved together with the order if we want to retrieve them later in the thank you page & email notifications.
Second, we need to work on the “woocommerce_get_order_item_totals” filter, which is responsible for displaying the Order Totals table (we would need to add one row with the value of the savings).
I will add this to my to-write list but I’m afraid I cannot provide this fix right now – thanks for your understanding ๐
~R
hi rodolfo, this snippet is awesome, thanks a lot, you’re rock!
i tried it already. i found that this snippet is not responsive for mobile view. “you saved” is missing when checkout on mobile gadget.
how to resolve this situation? what am i need to change on this snippet.
thanks again! regards.
Thank you Thomas, you’re totally right! I just updated the snippet, and added the “data-title” part to the
Let me know ๐
work like a charm, thankyou.
anyway, based on your screencapture above. can we re-arrange the position?
original :
1.subtotal $150
2.total $150
3.you saved $17.2
into :
1. subtotal $150
2. you saved $17.2
3. total $150
thanks
Thank you Thomas!
Try using “woocommerce_cart_totals_before_order_total” instead of “woocommerce_cart_totals_after_order_total” and let me know ๐
would be amazing if we can get a snippet like this to show same message at the checkout page summary as we don’t use the cart total in the cart page ๐ Thank you!
Thanks for your comment Rodrigo! Actually, this snippet already does that. The following add_action shows that same result on the checkout page:
Thanks yes it worked, didn’t realize it :). Anyway we can change the color of the “You Saved” to RED? Thank you again!
Cool ๐ For changing the color, there is a class “.cart-discount” you can use in your CSS. Cheers!
Also, is it possible to make the saved amount aligned in the same column as the subtotal and Total? It’s a big to the right for some reasons. Thanks again!
Hey Vuster,
Thanks for your comments! All you’re asking is possible via CSS. If you note, we have:
and
…so you can use those classes to edit the look, alignment and widths!
Let me know if you manage to do so.
R
Thanks so much!
I was able to change the color of the “saving” text. But I’m not really sure how to make the dollar amount aligned properly (on the same vertical line as the Subtotal and Total line). Could you help? Thanks.
Hey, thanks for following up! I’ve slightly modified the snippet where is says “if ( $discount_total > 0 ) { echo…”. The HTML that was being generated there was creating a new table instead of “nesting” that content into the existing cart table.
If you use the new edit, the alignment and styles will follow the ones from the cart table now. Let me know!
Changelog:
Works like a charm. Thanks!!
Awesome, thanks for letting me know ๐
Hi again!
But this change doesn’t carry the formatting over to the Checkout page?
You’re right lol! Use this instead:
Awesome code! Thank you.
Is it possible to highlight or add some formatting to the “You Saved” and the amount saved?
Like making it Red or put a box around it. Thanks!!
Hey! Nice piece of Code. Very useful. Thanks
You’re welcome Waqas ๐ Thanks for your feedback!
I added this to my functions.php but nothing displays on the page. Any ideas?
April, thanks for your comment ๐ Just wondering if any product you added to cart is on Sale? Otherwise nothing will show ๐
Okay, thanks. That would be why. I was using a coupon code instead of actually placing the products on sale.
I’ll play around with some code to see if I can get this to work when coupons are used as well.
Thanks!
๐ Awesome! It’s a good idea to add coupon codes as well… I’ll see what I can do ๐
I Agree Rodolfo, I looking for this exactly!
Done! Just change:
with:
In this way you will also add the coupon discounts ๐
You are awesome!! ๐
I’m attending the live class now and didn’t realize you already added the code for coupon until you mentioned it. Thanks Rodolfo ๐
You’re always welcome ๐
Wow! Thank you very much Rodolfo. Codemaster!!
Awesome, thanks for your comment Jose! Credits go to Jamie ๐
Thanks for the snippet Jamie, cheers
Thank you Leo for letting Jamie know ๐
Excellent snippet!
I agree – thank you Jamie! ๐
Thanks for the info – this will come in handy!
Awesome – thank you Jean ๐