Search code examples
phpwordpresswoocommercecartcheckout

Calculating total saving amount on the WooCommerce cart and checkout


I want to put a code inside the functions.php file to check all the products in the shopping cart or checkout page and collect the discount percentage of the products that have a discount and show it to the customer as the profit of the purchase.

I tried code like this before which only works for single-product page and I want something like this for cart page or checkout page:

add_action( 'woocommerce_single_product_summary', 'simple_product_saving_amount', 11 );
function simple_product_saving_amount() {
    global $product;

    if( $product->is_type('simple') && $product->is_on_sale() ) {
        $args = array( 'price' => $product->get_regular_price() );
        if ( 'incl' === get_option('woocommerce_tax_display_shop') ) {
            $regular_price = wc_get_price_including_tax( $product, $args );
            $active_price  = wc_get_price_including_tax( $product );
        } else {
            $regular_price = wc_get_price_excluding_tax( $product, $args );
            $active_price  = wc_get_price_excluding_tax( $product );
        }
        $saved_amount  = $regular_price - $active_price;
        printf( '<p id="saving_total_price">%s %s</p>', __('our profit:'), wc_price( $saved_amount ) );
    }
}

Solution

  • Based on this accepted working answer, here is the adapted code version, to display total savings in cart and in checkout pages:

    add_action( 'woocommerce_cart_totals_before_shipping', 'display_cart_volume_total', 20 );
    add_action( 'woocommerce_review_order_before_shipping', 'display_cart_volume_total', 20 );
    function display_cart_volume_total() {
        $total_saving = 0;
    
        // Loop through cart items and calculate total volume
        foreach( WC()->cart->get_cart() as $cart_item ){
            $product = $cart_item['data'];
            $args = array( 'price' => floatval( $product->get_regular_price() ) );
    
            if ( 'incl' === get_option('woocommerce_tax_display_cart') ) {
                $regular_price = wc_get_price_including_tax( $product, $args );
                $active_price  = wc_get_price_including_tax( $product );
            } else {
                $regular_price = wc_get_price_excluding_tax( $product, $args );
                $active_price  = wc_get_price_excluding_tax( $product );
            }
            $total_saving += ( $regular_price - $active_price ) * $cart_item['quantity'];
        }
    
        if ( $total_saving > 0 ) {
            // The Output
            echo ' <tr class="cart-total-saving">
                <th>' . __( "Savings", "woocommerce" ) . '</th>
                <td data-title="total-saving">' . wc_price( $total_saving ) . '</td>
            </tr>';
        }
    }
    

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

    On cart page:

    enter image description here

    On checkout page:

    enter image description here

    Related: Insert a custom total row on cart and checkout totals in Woocommerce