在Woocommerce中设置购物车价格后重新计算总数 [英] Re-calculate totals after setting cart item prices in Woocommerce
本文介绍了在Woocommerce中设置购物车价格后重新计算总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在通过set_price()方法更改产品价格后,如何更改小计价格?现在,它可以按review-order.php中的旧价格计算总成本。
How i can change subtotal price after i changed the price of product by the set_price() method? Now it is calculate the total cost at old prices in review-order.php.
cart.php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
...
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
...
$_product->set_price( $price );
...
}
推荐答案
这需要在特定的专用挂钩函数中完成,而不要使用 cart.php
模板:
This need to be done in a specific dedicated hooked function instead of cart.php
template:
add_action( 'woocommerce_before_calculate_totals', 'changing_cart_item_prices', 20, 1 );
function changing_cart_item_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// The instance of the WC_Product Object
$product = $cart_item['data'];
$product_id = $product->get_id(); // The product ID
$price = $product->get_price(); // The product price
$new_price = 50;
// Set the new cart item price
$product->set_price( $new_price );
}
}
代码会出现在您活跃孩子的function.php文件中主题(或活动主题)。经过测试并可以正常工作。
Code goes in function.php file of your active child theme (or active theme). Tested and work.
由于钩子是由ajax支持的,因此一切都会正确更新
Everything will be updated correctly as the hook is ajax powered
这篇关于在Woocommerce中设置购物车价格后重新计算总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文