shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

Show Percentage Discount on Cart and Checkout Pages.

This article helps you customize the sale badge on the cart and checkout pages.

For a better understanding, please refer to the screenshot. This is how it looks:

Screenshot:

If this is your requirement for the cart and checkout page, please follow our instructions on how to achieve this requirement.

Please copy the code snippet and paste it into the functions.php file of your active theme.

Code Snippet:

//Show percentage discount on cart and checkout page.

add_filter('woocommerce_cart_item_subtotal', 'wdm_csp_show_discount_in_cart_checkout_78109',10, 2);

//Show percentage discount on the order details page and in the order confirmation email.

add_filter('woocommerce_order_formatted_line_subtotal', 'wdm_csp_show_discount_in_cart_checkout_78109', 10, 2);

function wdm_csp_show_discount_in_cart_checkout_78109($product_subtotal, $cart_item) {
    $product_id = $cart_item['variation_id'] ? $cart_item['variation_id'] : $cart_item['product_id'];

    $_product = wc_get_product( $product_id );

    $regular_price = $_product->get_regular_price();
    $regular_price = wc_get_price_to_display($_product, array('price' => $regular_price));

    $strikethrough_subtotal = $regular_price * $cart_item['quantity'];

    $applied_price = $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
    $applied_price = $cart_item['line_subtotal'];

    $discount_percentage = ($applied_price/$strikethrough_subtotal) * 100;
    $discount_percentage = 100 - round($discount_percentage, 2);

    if ( $applied_price === $strikethrough_subtotal ) {
        $product_subtotal = wc_price($strikethrough_subtotal) . ' (' . $discount_percentage . '%)';
        return $product_subtotal;
    }
    $product_subtotal = '<del>' . wc_price($strikethrough_subtotal) . '</del> ' . $product_subtotal . ' (' . $discount_percentage . '%)';

    return $product_subtotal;
}

The above code snippet allows you to apply a percentage discount on the cart and checkout pages.

Updated on June 16, 2024
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

Was this article helpful?

shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

Related Articles