Display Total Discount Amount in Cart and Checkout Pages

Show total discount savings in WooCommerce cart and checkout using woocommerce_cart_totals_before_order_total and woocommerce_review_order_before_order_total action hooks. Retrieve discount amount with WC()->cart->get_cart_discount_total() method, check if discount exists with conditional greater than zero, format discount amount with wc_price function for proper currency display, output as table row with proper HTML structure including th for label and td for amount, add custom CSS class for styling, display discount transparency showing customers their savings from applied coupons or automatic discounts, enhance user experience by highlighting value received, and encourage purchases by making savings visible in cart totals section.

WooCommerce Display Total Discount @ Cart & Checkout

Hooks PHP PHP Snippets Plugins Theme Development Woocommerce Woocommerce Hooks Wordpress WordPress Development

WooCommerce Display Total Discount @ Cart & Checkout Tutorial/Guide

To display the total discount or savings amount in the cart and checkout pages of WooCommerce, you can use the `woocommerce_cart_totals_before_order_total` and `woocommerce_review_order_before_order_total` hooks. Here's an example of how you can achieve this: Step 1: Open your theme's `functions.php` file. Step 2: Add the following code to the file:  

// Function to display total discount
function display_total_discount() {
    $total_discount = WC()->cart->get_cart_discount_total();

    if ($total_discount > 0) {
        $formatted_discount = wc_price($total_discount);
        echo '<tr class="total-discount"><th>' . __('Total Discount', 'your-text-domain') . '</th><td data-title="' . __('Total Discount', 'your-text-domain') . '">' . $formatted_discount . '</td></tr>';
    }
}
add_action('woocommerce_cart_totals_before_order_total', 'display_total_discount');
add_action('woocommerce_review_order_before_order_total', 'display_total_discount');

  Step 3: Save the file and upload it to your theme directory. With these steps, the `display_total_discount()` function will be triggered before the order total is displayed on the cart and checkout pages. It retrieves the total discount amount using the `get_cart_discount_total()` method and formats it using `wc_price()`. The total discount will be displayed as a separate row in the cart and checkout tables, showing the discount amount under the "Total Discount" label. Please note that you may need to adjust the code and customize the CSS styles to match your theme's design. Additionally, replace `'your-text-domain'` with your actual text domain for translation purposes if needed. By displaying the total discount or savings, you provide transparency to your customers, allowing them to see the reduced amount and the benefits of any applied discounts. This can help enhance their shopping experience and encourage conversions.

๐Ÿ’ก Have a Coding Problem?

Search our archives or reach out to our team for solutions and expert advice.