"You're Saving X" Summary Above the Cart
Adds up the discounts from sale prices and coupons in the cart and shows the total saving at the top of the cart page.
Opens WPIDE on
Code
if ( WC()->cart && ! WC()->cart->is_empty() ) {
$saving = 0;
foreach ( WC()->cart->get_cart() as $item ) {
$product = $item['data'];
if ( $product->is_on_sale() ) {
$regular = (float) wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
$current = (float) wc_get_price_to_display( $product );
$saving += max( 0, $regular - $current ) * (int) $item['quantity'];
}
}
$saving += (float) WC()->cart->get_discount_total() + ( WC()->cart->display_prices_including_tax() ? (float) WC()->cart->get_discount_tax() : 0 );
if ( $saving > 0 ) {
echo '<div class="wpide-cart-savings" style="margin:0 0 20px;padding:12px 16px;border-radius:6px;background:#edfaef;color:#1e4620;font-weight:600;">' . wp_kses_post( sprintf( 'You are saving %s on this order!', wc_price( $saving ) ) ) . '</div>';
}
}
Notes
Only appears when the shopper saves something. Sale savings use the regular price of each product.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.