Get a fast, conversion-focused WooCommerce theme in 2026. Switch to Shoptimizer.

Home Documentation Shoptimizer Code Snippets Display total savings within the mini cart

Display total savings within the mini cart

This snippet looks at including the total savings as a line item in the slide out mini cart. This isn’t standard WooCommerce functionality but inspired by this eCommerce conversions video I decided to make my own.

The final result should look like this.

Total savings within mini cart

Because of the way the mini cart hooks are setup I had to use some order CSS to arrange the “You save” bar at the top.

Add the following code within the Shoptimizer Child theme’s functions.php file or via the Code Snippets plugin.

if ( ! function_exists( 'shoptimizer_mini_cart_total_discounts' ) ) {
    /**
     *
     */
    function shoptimizer_mini_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) { ?>
            <p class="woocommerce-mini-cart__total total discounts-total">
                <strong><?php echo esc_html__( 'You save', 'shoptimizer' ); ?></strong>
                <span class="woocommerce-Price-amount amount">-<?php echo wc_price($discount_total + $woocommerce->cart->discount_cart); ?></span>
            </p>

			<style>
				.widget_shopping_cart p.total.discounts-total {
					color: green;
					font-size: 13px;
					margin-bottom: -1.2em;
					order: 1;
					border-top: 1px solid #e2e2e2;

				}
				.widget_shopping_cart p.total.discounts-total strong {
					font-weight: normal;
				}
				.shoptimizer-mini-cart-wrap .widget_shopping_cart .discounts-total .amount {
					margin: 0;
					font-weight: normal;
					color: green;
				}
				.shoptimizer-mini-cart-wrap .widget_shopping_cart .discounts-total .amount bdi {
					color: green;
				}
				.widget_shopping_cart p.total {
					order: 2;
					border: none;
				}
				.widget_shopping_cart p.buttons {
					order: 3;
				}
				.shoptimizer-mini-cart-wrap .cart-drawer-below {
					order: 4;
				}
			</style>
        <?php }
    }
}

add_action( 'woocommerce_widget_shopping_cart_before_buttons',  'shoptimizer_mini_cart_total_discounts', 10);

Success!

The discount has been applied. You will see it when you checkout.

There has been a problem

Unfortunately this discount cannot be applied to your cart.

$50
Off

Do you want a whopping $50 off Shoptimizer for WooCommerce?

Click the button below to get Shoptimizer for only $49. (Regular price: $99).
This is a special launch offer for today only.
Get it before it is gone!