在 Woocommerce 3 中更改购物车商品价格
Change cart item prices in Woocommerce 3
我正在尝试使用以下功能更改购物车中的产品价格:
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;
}
}
它在 WooCommerce 版本 2 中工作正常。6.x 但在版本 3.0+ 中不再工作
如何让它在 WooCommerce 3.0+ 版本中运行?
谢谢。
2021 年更新(处理迷你购物车定制商品价格)
使用 WooCommerce 版本 3.0+ 您需要:
- 改为使用
woocommerce_before_calculate_totals
挂钩。
- 改用WC_Cart
get_cart()
方法
- 改用WC_product
set_price()
方法
代码如下:
// 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 );
}
}
对于迷你购物车 (更新):
// 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;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。
此代码已经过测试并有效(仍然适用于 WooCommerce 5。1.x)。
Note: you can increase the hook priority from 20
to 1000
(or even 2000
) when using some few specific plugins or others customizations.
相关:
对于 WooCommerce 版本 3.2.6,如果我将优先级提高到 1000,@LoicTheAztec 的回答对我有用。
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
我尝试了 10
、99
和 999
的优先级值,但我购物车中的价格和总价没有改变(即使我能够通过 [=14 确认=] set_price()
实际上设定了商品的价格。
我有一个自定义挂钩,可以向我的购物车添加费用,并且我正在使用添加产品属性的第 3 方插件。我怀疑这些 WooCommerce "add-ons" 引入了延迟,需要我延迟我的自定义操作。
我正在尝试使用以下功能更改购物车中的产品价格:
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;
}
}
它在 WooCommerce 版本 2 中工作正常。6.x 但在版本 3.0+ 中不再工作
如何让它在 WooCommerce 3.0+ 版本中运行?
谢谢。
2021 年更新(处理迷你购物车定制商品价格)
使用 WooCommerce 版本 3.0+ 您需要:
- 改为使用
woocommerce_before_calculate_totals
挂钩。 - 改用WC_Cart
get_cart()
方法 - 改用WC_product
set_price()
方法
代码如下:
// 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 );
}
}
对于迷你购物车 (更新):
// 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;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。
此代码已经过测试并有效(仍然适用于 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.
相关:
对于 WooCommerce 版本 3.2.6,如果我将优先级提高到 1000,@LoicTheAztec 的回答对我有用。
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
我尝试了 10
、99
和 999
的优先级值,但我购物车中的价格和总价没有改变(即使我能够通过 [=14 确认=] set_price()
实际上设定了商品的价格。
我有一个自定义挂钩,可以向我的购物车添加费用,并且我正在使用添加产品属性的第 3 方插件。我怀疑这些 WooCommerce "add-ons" 引入了延迟,需要我延迟我的自定义操作。