Displays a “You are saving” row in the WooCommerce cart and checkout totals table, combining coupon discounts and sale price savings into a single amount. Encourages customers to complete their purchase by showing the total value they’re getting.
add_action( 'woocommerce_cart_totals_after_order_total', function() {
if ( ! WC()->cart ) {
return;
}
$discount = (float) WC()->cart->get_discount_total();
$sale_savings = 0;
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product = $cart_item['data'];
$quantity = (int) $cart_item['quantity'];
if ( $product->is_on_sale() ) {
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_sale_price();
$sale_savings += ( $regular_price - $sale_price ) * $quantity;
}
}
$total_savings = $discount + $sale_savings;
if ( $total_savings > 0 ) {
echo '<tr class="order-total savings-total">';
echo '<th>' . esc_html__( 'You are saving', 'woocommerce' ) . '</th>';
echo '<td><strong>' . wp_kses_post( wc_price( $total_savings ) ) . '</strong></td>';
echo '</tr>';
}
} );