简体   繁体   中英

Woocommerce How to Display Sale Price of Products in Cart

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?

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 line item sub-total.
            add_filter(
                'woocommerce_cart_item_subtotal',
                function() use ( $cart_item ) {
                    return wc_price( $cart_item['line_total'] );
                }
            );
            // This updates the item total.
            return wc_format_sale_price( $product->get_regular_price(), number_format( $cart_item['line_total'], 2 ) ) . $product->get_price_suffix();
        }
        return $product->get_price_html();
    }

    return $old_display;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM