1

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Krystian
  • 887
  • 5
  • 21
  • 52

1 Answers1

1

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks thats now showing the right subtotal but a too big discount-price. That another problem with my code I just noticed. I think its because its creating the discounted price out of the original price without the 7% tax. But why? – Krystian Jan 29 '19 at 08:45
  • @KrystianManthey I have updated the code. It should solve this problem. – LoicTheAztec Jan 29 '19 at 09:04