【问题标题】:Woocommerce How to Display Sale Price of Products in CartWoocommerce 如何在购物车中显示产品的销售价格
【发布时间】:2022-01-01 22:38:23
【问题描述】:

我已将我的购物车设置为在折扣商品的售价旁边显示带有删除线的产品价格(请参阅照片中购物车中的第一个产品)。

checkout screenshot

这是使用此代码实现的

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 );

您在照片中看到的问题是,这仅适用于特价产品(售价 12.00 美元的产品售价 0.00)。但是,优惠券代码应用于其他两个项目。

我关注this thread,在结帐摘要中将总节省显示为“You Saved”,包括促销和优惠券折扣。

如何显示购物车中应用了优惠券的商品的折扣价?

【问题讨论】:

    标签: php wordpress woocommerce cart checkout


    【解决方案1】:

    因此,这将根据我对您的 cmets 的解释更新购物车项目总数和行项目总数。

    这将被添加到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 );
    

    【讨论】:

    • 非常有帮助。该代码有效,但唯一的问题是它没有显示正确的折扣金额。好像没有显示含税的折扣价。例如,具有 20% 折扣的 39.99 美元(含税)产品应为 31.99 美元,但显示为 29.08 美元(大约少 10% 的税额)。可以显示含税的折扣价吗?谢谢
    • @KyleSinko 更新以显示税收。也在$cart_item 数组中。如果这有帮助...请接受并投票,谢谢。
    • 我更新了代码,但仍然显示错误的折扣。如果我在 Woocommerce 中取消选中“启用税率和计算”,它会显示正确的折扣。有任何想法吗?干杯
    • 这实际上是从商店中的每个产品中减去税额。在购物车中,它会显示带有删除线的每件产品以及更新后的不含税价格,无论它是否在销售。
    • 这是我在测试页上看到的。 snipboard.io/h3021K.jpg优惠券仅适用于APC Smart UPS。
    猜你喜欢
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 2022-01-21
    • 2017-04-06
    • 2017-08-07
    • 2021-05-30
    • 2021-05-18
    • 2019-09-16
    相关资源
    最近更新 更多