gpt4 book ai didi

php - 在 Woocommerce 3 中以编程方式设置产品销售价格和购物车商品价格

转载 作者:行者123 更新时间:2023-12-04 13:22:26 25 4
gpt4 key购买 nike

这是继续:Set product sale price programmatically in WooCommerce 3

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

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

感谢任何帮助。

最佳答案

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

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 文件。

经过测试并有效。

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

关于php - 在 Woocommerce 3 中以编程方式设置产品销售价格和购物车商品价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48771178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com