I'm using this code snippet to display the total order savings at WooCommerce checkout:
add_action( 'woocommerce_review_order_after_order_total', 'show_total_discount_cart_checkout', 9999 );
function show_total_discount_cart_checkout() {
$discount_total = 0;
foreach ( WC()->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="total-saved"><th>You Saved</th><td data-title="You Saved">' . wc_price( $discount_total + WC()->cart->get_discount_total() ) .'</td></tr>';
}
}
It should display the total amount of money a customer saved (sale prices plus coupon discounts). Screenshot: https://ibb.co/KXg2bDj
However, if there are no discounted products in the cart, the total order savings do not show up, even if there is a coupon code applied to the order. The total order savings show up only if there are discounted products in the cart. Screenshot: https://ibb.co/PCQPGZx
I would like the total order savings to show up if there is a coupon code applied to the order, if there are discounted products in the cart or if there's both. If there's neither 1 of those 2, the total order savings do not need to show up.
Could someone please help me achieve this?