How to Show Total Savings on Cart in WooCommerce

show total savings on cart

If you run a WooCommerce store and want to show total savings on cart while checkout, then this article will help you.

Showing total savings on the cart creates a sense of confidence before the user proceeds to make a payment.

This makes them feel they got a good deal at a discounted price which makes them purchase faster.

Code to Show Total Savings on Cart in WooCommerce

Add the below code in your theme’s functions.php file.

Navigate to Dashboard >> Appearance >> Editor.

This code will show total savings below the total price.

function wc_total_savings() {
   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>'. __( 'Your Savings', 'woocommerce' ) .'</th>
    <td data-title=" '. __( 'You Saved', 'woocommerce' ) .' ">'
    . wc_price( $discount_total + $woocommerce->cart->discount_cart ) .'</td>
    </tr>';
    }
}
add_action( 'woocommerce_cart_totals_after_order_total', 'wc_total_savings', 99);
add_action( 'woocommerce_review_order_after_order_total', 'wc_total_savings', 99);

That’s it!

I hope this code helps you.

Recommended reading:

Leave a Comment

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