WooCommerce: Display Total Discount / Savings @ Cart

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.

WooCommerce: Show Total Savings / Total Discount Amount @ Cart & Checkout Pages
WooCommerce: Show Total Savings / Total Discount Amount @ Cart & Checkout Pages

WooCommerce PHP Snippet: Display Total Discount Amount / Total Savings @ Cart & Checkout


/**
* @snippet Display Total Discount / Savings @ WooCommerce Cart/Checkout
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=20362
* @author Rodolfo Melogli, BΓΌlent Sakarya
* @testedwith WooCommerce 3.0
*/

function bbloomer_wc_discount_total_30() {

    global $woocommerce;
     
    $discount_total = 0;
     
    foreach ( $woocommerce->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 = ($regular_price - $sale_price) * $values['quantity'];
        $discount_total += $discount;
        }
 
    }
    	    
    if ( $discount_total > 0 ) {
    echo '<tr class="cart-discount">
    <th>'. __( 'You Saved', 'woocommerce' ) .'</th>
    <td data-title=" '. __( 'You Saved', 'woocommerce' ) .' ">'
    . wc_price( $discount_total + $woocommerce->cart->discount_cart ) .'</td>
    </tr>';
    }

}

// Hook our values to the Basket and Checkout pages

add_action( 'woocommerce_cart_totals_after_order_total', 'bbloomer_wc_discount_total_30', 99);
add_action( 'woocommerce_review_order_after_order_total', 'bbloomer_wc_discount_total_30', 99);

Where to add this snippet?

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

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 Customization?

Check out these free video tutorials!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Jamie Gill

I am a Web Developer at Jamiegill.com and love HTML, CSS, JS, PHP, MySQL. I spend most days coding, playing with the WordPress core. A WooCommerce Enthusiast, in my spare time I enjoy learning more about PHP and spending time with my wife and 3 girls.

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

98 thoughts on “WooCommerce: Display Total Discount / Savings @ Cart

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

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

  2. I think you forget to calculate variable products:

     
    if ( ! function_exists( 'tipikala_cart_total_discounts' ) ) {
        /**
         *
         */
        function tipikala_cart_total_discounts()  {
            global $woocommerce;
            $discount_total = 0;
    
            foreach ($woocommerce->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();
    
                    if (empty($regular_price)){ //then this is a variable product
                        $available_variations = $_product->get_available_variations();
                        $variation_id=$available_variations[0]['variation_id'];
                        $variation= new WC_Product_Variation( $variation_id );
                        $regular_price = $variation ->regular_price;
                        $sale_price = $variation ->sale_price;
                    }
    
                    $discount = ceil(($regular_price - $sale_price) * $values['quantity'] );
                    $discount_total += $discount;
                }
    
            }
            if ($discount_total > 0) { ?>
                <tr class="discounts-total">
                    <th><?php echo esc_html__( 'Discount total', 'tipikala' ); ?></th>
                    <td><?php echo wc_price($discount_total + $woocommerce->cart->discount_cart); ?></td>
                </tr>
            <?php }
        }
    }
    
    
    add_action( 'woocommerce_cart_totals_before_shipping',	    'tipikala_cart_total_discounts', 10 );
    add_action( 'woocommerce_review_order_before_order_total',  'tipikala_cart_total_discounts', 10);
    
    1. Thank you, I haven’t tested so I trust you πŸ™‚

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

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

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

    function bbloomer_wc_discount_total_30() {
     
        global $woocommerce;
          
        $discount_total = 0;
        $regular_price_total = 0;
        $shipping_cost = $woocommerce->cart->shipping_total ;
        $cart_total = $woocommerce->cart->total;
    
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
              
            $_product = wc_get_product( $values['data'] );
                if ( $_product->is_on_sale() ) {
                    $regular_price = $_product->get_regular_price();
                    $regular_price_total += $regular_price* $values['quantity'];
                }
    
    
            }
            $discount_total = $regular_price_total - $cart_total + $shipping_cost;
         
        if ( $discount_total > 0 ) {
        echo '<tr class="cart-discount">
        <th>'. __( 'You Saved', 'woocommerce' ) .'</th>
        <td data-title=" '. __( 'You Saved', 'woocommerce' ) .' ">'
        . wc_price( $discount_total ) .'</td>
        </tr>';
        }
     
    }
    
    1. Thanks Yuriy πŸ™‚

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

  5. Works like a charm. Thank you!

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

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

  7. Hi Rodolfo,
    I don’t know why but the code does not work for my site. Any help, please? πŸ™‚

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

  8. Hey guys, probably a stupid question, but how do I get the discount amount to show above the total, not below it?

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

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

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

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

  11. Hello..

    After wc 3.0, you can edit code for regular and sale price.

    
    function bbloomer_wc_discount_total() {
     
        global $woocommerce;
          
        $discount_total = 0;
          
        foreach ( $woocommerce-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $values) {
              
    		$_product = $values['data'];
    		
    		//var_dump($_product);
      
            if ( $_product-&gt;is_on_sale() ) {
    			$regular_price = $_product-&gt;get_regular_price();
    			$sale_price = $_product-&gt;get_sale_price();
    			$discount = ($regular_price - $sale_price) * $values['quantity'];
    			$discount_total += $discount;
            }
      
        }
                 
        if ( $discount_total &gt; 0 ) {
    		echo '
    		'. __( 'Δ°ndirimler ToplamΔ±', 'nivothemes' ) .'
    		'
    		. wc_price( $discount_total + $woocommerce-&gt;cart-&gt;discount_cart ) .'
    		';
        }
     
    }
    
    
    1. Thank you so much! Snippet updated πŸ™‚

  12. hi . thanks for share
    how add this after price in single product page?
    thanks

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

  13. hello again

    I think that I solved the problem. I share the code with you:

     foreach ( $woocommerce-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $values) {
       $_product = $values['data'];
        $_quantity = $values['quantity'];
    
       $array = (array) $_product;
       $valor_publico = $array[chr(0).'*'.chr(0).'data']['price'] * $_quantity;
      $valor_privado = $array[chr(0).'*'.chr(0).'changes']['price'] * $_quantity;
      $diferencia = $valor_publico - $valor_privado;
    
    	if ( $diferencia &gt; 0 ) {
               $discount = ($diferencia);
               $discount_total += $discount;
            }
    
    1. Nice, thanks Fernando!

    2. Thank you! You are a genius!

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

      1. I would also want to know about that.

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

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

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

  16. Hey, sometimes I get a 500 error when I view this site. I figured you would like to know.

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

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

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

  18. I have a simple question. How do I implement the code? do I copy and paste it on my function.php or how?
    thanks

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

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

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

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

    1. Hey Samrat thanks for your comment! Could you send me a screenshot to show me the error? Thanks!

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

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

          1. It behaves the same way at my shop. Might it be related to a woocommerce update? Did you manage to find a solution?

            1. Samrat, Kyle, Sanya, I revised the code snippet. It should work now – let me know πŸ™‚

  21. Any idea how to make this code work when the sale price is $0 or Free?

    1. Hey Brad, thanks for your comment! Could you send me a screenshot please? Cheers!

      1. 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
        http://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
        http://freesunshields.com/wp-content/uploads/2016/11/Screen-Shot-2016-11-02-at-9.41.00-AM.png

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

          if ( !empty($product->sale_price) ) {
          

          with:

          if ( isset($product->sale_price) ) {
          

          Hope this helps πŸ™‚

          1. That worked. Thank you so much!!

          2. That worked. Thank you so much!!

            1. Brilliant, thanks so much for your feedback Keyvan!

  22. Hello thank you for your snipnet,

    However It seems doesn’t work on my site

    Screenshot here: http://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!

    1. Weird πŸ™‚ Can you completely empty the cart and re-do a test please? Cheers!

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

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

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

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

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

    1. Thank you Thomas, you’re totally right! I just updated the snippet, and added the “data-title” part to the

      , which will show on mobile view:

       <td data-title= ....
      

      Let me know πŸ™‚

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

        1. Thank you Thomas!

          Try using “woocommerce_cart_totals_before_order_total” instead of “woocommerce_cart_totals_after_order_total” and let me know πŸ™‚

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

    1. Thanks for your comment Rodrigo! Actually, this snippet already does that. The following add_action shows that same result on the checkout page:

      add_action( 'woocommerce_review_order_before_payment', 'bbloomer_wc_discount_total');
      
      1. Thanks yes it worked, didn’t realize it :). Anyway we can change the color of the “You Saved” to RED? Thank you again!

        1. Cool πŸ™‚ For changing the color, there is a class “.cart-discount” you can use in your CSS. Cheers!

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

    1. Hey Vuster,

      Thanks for your comments! All you’re asking is possible via CSS. If you note, we have:

      
      <table class="discount-total">
      
      

      and

      
      <tr class="cart-discount">
      
      

      …so you can use those classes to edit the look, alignment and widths!

      Let me know if you manage to do so.

      R

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

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

          
          // leave above as is
          
          if ( $discount_total > 0 ) {
              echo '<tr class="cart-discount">
              <th>'. __( 'You Saved', 'woocommerce' ) .'</th>
              <td>'. wc_price($discount_total+$woocommerce->cart->discount_cart) .'</td>
              </tr>';
          
          // leave below as is
          
          
          1. Works like a charm. Thanks!!

            1. Awesome, thanks for letting me know πŸ™‚

          2. Hi again!
            But this change doesn’t carry the formatting over to the Checkout page?

            1. You’re right lol! Use this instead:

              add_action( 'woocommerce_review_order_after_order_total', 'bbloomer_wc_discount_total');
              
  27. 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!!

  28. Hey! Nice piece of Code. Very useful. Thanks

    1. You’re welcome Waqas πŸ™‚ Thanks for your feedback!

  29. I added this to my functions.php but nothing displays on the page. Any ideas?

    1. April, thanks for your comment πŸ™‚ Just wondering if any product you added to cart is on Sale? Otherwise nothing will show πŸ™‚

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

        1. πŸ™‚ Awesome! It’s a good idea to add coupon codes as well… I’ll see what I can do πŸ™‚

          1. I Agree Rodolfo, I looking for this exactly!

            1. Done! Just change:

               wc_price($discount_total)
              

              with:

               wc_price($discount_total+$woocommerce->cart->discount_cart)
              

              In this way you will also add the coupon discounts πŸ™‚

          2. 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 πŸ˜€

            1. You’re always welcome πŸ™‚

          3. Wow! Thank you very much Rodolfo. Codemaster!!

            1. Awesome, thanks for your comment Jose! Credits go to Jamie πŸ™‚

  30. Thanks for the snippet Jamie, cheers

    1. Thank you Leo for letting Jamie know πŸ™‚

    1. I agree – thank you Jamie! πŸ™‚

  31. Thanks for the info – this will come in handy!

    1. Awesome – thank you Jean πŸ™‚

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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.