WooCommerce-为购物车中的每个产品添加自定义价格 [英] WooCommerce - Adding a custom price to each product in cart
问题描述
我想使用这段简单的代码update_post_meta( $product->id, '_regular_price', $frame_price_added);
在购物车中添加自定义价格来更新产品价格.
I would like to update the price of products adding a custom price in cart using this simple piece of code update_post_meta( $product->id, '_regular_price', $frame_price_added);
.
注意:我要实现的目标是将此自定义价格添加到购物车中的每个产品.
Note: what I'm trying to achieve is to add this custom price to each product in cart.
我已经尝试通过这种方式获取$frame_price_added
:
I have try to get $frame_price_added
this way:
$frame_price = $res['_number_field'][0];
$frame_price_added = $product->price + $frame_price;
这里$product->price
是来自woocomerce产品的价格,$frame_price
是来自我新添加的价格.
Here $product->price
is price coming from woocomerce product and $frame_price
is coming from my newly added price.
我想知道如何将这个新价格与购物车相关联,因为它不起作用.
I was wondering how do I associate this new price to cart, because it doesn't work.
我尝试使用update_post_meta( $product->id, '_price', $frame_price_added);
,刷新页面后,它会将自定义价格添加并存储到产品中,并保存了.
I have tried using update_post_meta( $product->id, '_price', $frame_price_added);
and when page is refreshed it adds and stores the custom price to the product, and saved it.
关于如何正确实现此目标的任何想法吗?
Any idea on how I can achieve this properly?
谢谢.
还有一件事……我搜索了一个可以在添加到购物车中调用的函数,但是我没有找到任何东西,还有一个在
woocommerce_template_single_add_to_cart
上调用的动作挂钩有woocommerce_single_product_summary
,但找不到任何功能.
One more thing… I have searched a function that can being called on add to cart and i didn't find any thing, and also an action hook being called on
woocommerce_template_single_add_to_cart
which hadwoocommerce_single_product_summary
but it didn't find any function.
推荐答案
Update: For WooCommerce 3.0+ Change cart item prices in WooCommerce version 3.0
您可以使用 woocommerce_before_calculate_totals
挂钩自定义购物车项目的价格.
You can use woocommerce_before_calculate_totals
hook to customize your cart items prices.
您可以通过这种方式在函数中将 $framed_price
变量定义为全局变量.
You can define $framed_price
variables as global in your function, this way.
这是代码:
// getting your additional price outside the function (making any conditional calculations)
$framed_price = 20;
add_action( 'woocommerce_before_calculate_totals', 'add_custom_total_price', 10 );
function add_custom_total_price( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
global $framed_price;
foreach ( $cart_object->get_cart() as $key => $value ) {
$value['data']->price += $framed_price;
}
}
或者在挂钩函数中获取自定义价格(可选,具体取决于您如何获得自定义价格):
add_action( 'woocommerce_before_calculate_totals', 'add_custom_total_price', 10 );
function add_custom_total_price( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$framed_price = 20;
foreach ( $cart_object->get_cart() as $key => $value ) {
$value['data']->price += $framed_price;
}
}
此代码已经过测试并且可以正常工作.
自然,此代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.
参考: WooCommerce购物车-动态价格变量传递到自定义价格挂钩
这篇关于WooCommerce-为购物车中的每个产品添加自定义价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!