JOKKER
JOKKER

Reputation: 542

Custom total savings amount display issue in WooCommerce Checkout

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?

Upvotes: 1

Views: 611

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254378

Try the following instead, that will solve your issue and handle displaying tax settings:

add_action( 'woocommerce_review_order_after_order_total', 'show_total_discount_cart_checkout', 1000 );
function show_total_discount_cart_checkout() {
    $discount_total = 0; // Initializing

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        $product = $item['data'];

        if ( $product->is_on_sale() ) {
            $regular_args = array( 'price' => $product->get_regular_price() );

            if ( WC()->cart->display_prices_including_tax() ) {
                $active_price    = wc_get_price_including_tax( $product );
                $regular_price   = wc_get_price_including_tax( $product, $regular_args );
            } else {
                $active_price    = wc_get_price_excluding_tax( $product );
                $regular_price   = wc_get_price_excluding_tax( $product, $regular_args );
            }
            $discount_total += ( $regular_price - $active_price ) * $item['quantity'];
        }
    }

    if ( WC()->cart->display_prices_including_tax() ) {
        $discount_total += WC()->cart->get_discount_tax();
    }

    $discount_total += WC()->cart->get_discount_total();

    if ( $discount_total > 0 ) {
        $text = __("You Saved", "woocommerce");

        printf( '<tr class="total-saved"><th>%s</th><td data-title="%s">%s</td></tr>', $text, $text, wc_price($discount_total) );
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Upvotes: 4

Related Questions