Search code examples
phpwordpresswoocommercecartsubtotal

Display striked cart item subtotal when coupon is applied in Woocommerce


My goal looks like this: enter image description here

I tried this code to achieve this:

add_filter( 'woocommerce_cart_item_subtotal', 'show_coupon_item_subtotal_discount', 100, 3 );
function show_coupon_item_subtotal_discount( $subtotal, $cart_item, $cart_item_key ){
    if( $cart_item['line_subtotal'] !== $cart_item['line_total'] ) {
        $subtotal = sprintf( '<del>%s</del> <ins>%s<ins>',  wc_price($cart_item['line_subtotal']), wc_price($cart_item['line_total']) );
    }
    return $subtotal;
}

But unfortunately it shows a wrong striked price as you can see here. Whats wrong with this code?

enter image description here

Its regarding this page: keimster.de/kasse


Solution

  • Update 2 - Handling taxes on discounted cart item total.

    Try the following that should display the correct striked subtotal price when a coupon is applied:

    add_filter( 'woocommerce_cart_item_subtotal', 'show_coupon_item_subtotal_discount', 100, 3 );
    function show_coupon_item_subtotal_discount( $subtotal, $cart_item, $cart_item_key ){
        $line_subtotal = $cart_item['line_subtotal'];
        $line_total    = $cart_item['line_total'];
        if( $line_subtotal !== $line_total ) {
            $subtotal_tax  = $cart_item['line_subtotal_tax'];
            $total_tax     = $cart_item['line_tax'];
            $incl_taxes    = WC()->cart->display_prices_including_tax() && $cart_item['data']->is_taxable();
    
            $raw_subtotal = $incl_taxes ? $line_subtotal + $subtotal_tax : $line_subtotal;
            $raw_total    = $incl_taxes ? $line_total + $total_tax : $line_total;
            $subtotal     = sprintf( '<del>%s</del> <ins>%s<ins>',  wc_price($raw_subtotal), wc_price($raw_total) );
        }
        return $subtotal;
    }
    

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