[英]Change cart item prices in Woocommerce 3
I am trying to change product price in cart using the following function:我正在尝试使用以下功能更改购物车中的产品价格:
add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price'
);
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = 400;
}
}
It was working correctly in WooCommerce version 2.6.x but not working anymore in version 3.0+它在 WooCommerce 2.6.x 版中正常工作,但在 3.0+ 版中不再工作
How can I make it work in WooCommerce Version 3.0+?我怎样才能让它在 WooCommerce 3.0+ 版中工作?
Thanks.谢谢。
Update 2021 (Handling mini cart custom item price) 2021 年更新(处理迷你购物车自定义商品价格)
With WooCommerce version 3.0+ you need:使用WooCommerce 3.0+ 版,您需要:
woocommerce_before_calculate_totals
hook instead.改为使用woocommerce_before_calculate_totals
钩子。get_cart()
method instead改用WC_Cart get_cart()
方法set_price()
method instead改用WC_product set_price()
方法Here is the code:这是代码:
// Set custom cart item price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
function add_custom_price( $cart ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example | optional)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
$cart_item['data']->set_price( 40 );
}
}
And for mini cart (update) :对于迷你购物车(更新) :
// Mini cart: Display custom price
add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
if( isset( $cart_item['custom_price'] ) ) {
$args = array( 'price' => 40 );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price_html;
}
Code goes in functions.php file of your active child theme (or active theme).代码在您的活动子主题(或活动主题)的functions.php 文件中。
This code is tested and works (still works on WooCommerce 5.1.x) .此代码经过测试并且可以工作(在 WooCommerce 5.1.x 上仍然有效) 。
Note: you can increase the hook priority from
20
to1000
(or even2000
) when using some few specific plugins or others customizations.注意:在使用一些特定插件或其他自定义项时,您可以将挂钩优先级从20
增加到1000
(甚至2000
) 。
Related:有关的:
With WooCommerce version 3.2.6, @LoicTheAztec's answer works for me if I increase the priority to 1000.对于 WooCommerce 版本 3.2.6,如果我将优先级提高到 1000,@LoicTheAztec 的答案对我有用。
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
I tried priority values of 10
, 99
and 999
but the price and total in my cart did not change (even though I was able to confirm with get_price()
that set_price()
had actually set the price of the item.我尝试了10
、 99
和999
的优先级值,但我的购物车中的价格和总数没有改变(尽管我能够用get_price()
确认set_price()
确实设置了商品的价格。
I have a custom hook that adds a fee to my cart and I'm using a 3rd party plugin that adds product attributes.我有一个自定义挂钩,可以向我的购物车添加费用,并且我正在使用添加产品属性的 3rd 方插件。 I suspect that these WooCommerce "add-ons" introduce delays that require me to delay my custom action.我怀疑这些 WooCommerce“附加组件”会引入延迟,需要我延迟自定义操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.