0

I have set up my cart to display the product price with a strikethrough next to the sale price of the discounted item (see first product in cart in photo).

checkout screenshot

This was achieved using this code

function my_custom_show_sale_price_at_cart( $old_display, $cart_item, $cart_item_key ) {

$product = $cart_item['data'];

if ( $product ) {
    return $product->get_price_html();
}

return $old_display;

}
add_filter( 'woocommerce_cart_item_price', 'my_custom_show_sale_price_at_cart', 10, 3 );

The issue as you can see in the photo is that this only works for products on sale (the $12.00 product on sale for 0.00). However, a coupon code is applied to the other two items.

I followed this thread to display the total savings as "You Saved" in the checkout summary, including sale and coupon discounts.

How can I display the discounted price of the items in the cart that have the coupon applied to them?

1 Answers1

0

So this will update the cart item totals and the line item totals based on what I interpreted your comments to be.

This would be added to functions.php

function my_custom_show_sale_price_at_cart( $old_display, $cart_item, $cart_item_key ) {
    $product = $cart_item['data'];
    // If you just want this to show in checkout vs cart + checkout - add && is_checkout() below.
    if ( $product ) {
        // This item has a coupon applied.
        if ( floatval( $product->get_price() ) !== number_format( $cart_item['line_total'], 2 ) / $cart_item['quantity'] ) {
            // This updates the item total.
            $price_html = wc_format_sale_price( $product->get_regular_price(), number_format( $cart_item['line_total'], 2 ) ) . $product->get_price_suffix();
        } else {
            $price_html = $product->get_price_html();
        }
        // This updates the line item sub-total.
        add_filter(
            'woocommerce_cart_item_subtotal',
            function() use ( $cart_item ) {
                return wc_price( $cart_item['line_total'] + $cart_item['line_tax'] );
            }
        );
        return $price_html;
    }

    return $old_display;

}
add_filter( 'woocommerce_cart_item_price', 'my_custom_show_sale_price_at_cart', 10, 3 );
Howard E
  • 5,454
  • 3
  • 15
  • 24
  • Very helpful. That code works but the only problem is that it isn't displaying the correct discount amount. It seems it isn't showing the discount price with tax included. For example, the $39.99(including tax) product with a 20% discount should be $31.99 but is displaying as $29.08 (roughly 10% less which is the tax amount). Is it possible to show the discount price with tax included? Thanks – Kyle Sinko Jan 03 '22 at 00:18
  • @KyleSinko Updated to show tax. Also in the `$cart_item` array. If this helped... please accept and upvote thanks. – Howard E Jan 03 '22 at 10:51
  • I updated the code and it still displays the wrong discount. If I uncheck "Enable tax rates and calculations" in Woocommerce it displays the correct discount. Any ideas? Cheers – Kyle Sinko Jan 03 '22 at 12:42
  • This is actually subtracting the tax amount from every product in the store. In the cart it shows every product with a strikethrough and the updated price without tax, regardless of whether it's on sale or not. – Kyle Sinko Jan 04 '22 at 22:09
  • This is what I see on my test page. https://snipboard.io/h3021K.jpg The coupon is applied only to the APC Smart UPS. – Howard E Jan 04 '22 at 22:24
  • I think the issue is that I have hidden the subtotal column in my cart as seen in the original checkout screenshot. The "Price" and "Subtotal" columns were always the same value as you can only add 1 of each product to the cart, so I didn't see the need. Is there a way to change it so that it doesn't update the "Price" column by subtracting the tax. https://snipboard.io/M87kiN.jpg the "Photoshop Site Plan Course" is not on sale but is showing as because it's subtracted the tax. – Kyle Sinko Jan 05 '22 at 03:52