在Woocommerce 3中以编程方式设置产品销售价格和购物车项目价格 [英] Set programmatically product sale price and cart item prices in Woocommerce 3

查看:65
本文介绍了在Woocommerce 3中以编程方式设置产品销售价格和购物车项目价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是以下内容的延续:在WooCommerce中以编程方式设置产品销售价格3

This is the continuation of : Set product sale price programmatically in WooCommerce 3

答案有效,但是,一旦用户将产品添加到购物车中,旧价格仍会在结帐时显示.

The answer works, however once a user adds the product to cart, the old price still shows up on checkout.

如何在购物车和购物车结帐页面上获得正确的销售价格?

How to get the correct sale price on cart and checkout pages for cart items?

感谢您的帮助.

推荐答案

缺少可用于购物车和结帐页面(以及订单和电子邮件通知)的部分是一个非常简单的技巧:

The missing part to get it work for for cart and checkout pages (and also Orders and email notifications too) is a very simple trick:

add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
function set_cart_item_sale_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Iterate through each cart item
    foreach( $cart->get_cart() as $cart_item ) {
        $price = $cart_item['data']->get_sale_price(); // get sale price
        $cart_item['data']->set_price( $price ); // Set the sale price

    }
}

代码进入您的活动子主题(活动主题)的function.php文件中.

经过测试,可以正常工作.

Tested and works.

因此,代码只需将产品销售价格设置为购物车中的产品价格即可.

So the code just set the product sale price as the product price in cart items and it works.

这篇关于在Woocommerce 3中以编程方式设置产品销售价格和购物车项目价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆