WooCommerce - 为购物车中的每个产品添加自定义价格
WooCommerce - Adding a custom price to each product in cart
我想使用这段简单的代码更新在购物车中添加自定义价格的产品的价格 update_post_meta( $product->id, '_regular_price', $frame_price_added);
。
注意:我想要实现的是将此自定义价格添加到购物车中的每个产品。
我尝试通过这种方式获得 $frame_price_added
:
$frame_price = $res['_number_field'][0];
$frame_price_added = $product->price + $frame_price;
这里 $product->price
是来自 woocomerce 产品的价格,$frame_price
来自我新添加的价格。
我想知道如何将这个新价格关联到购物车,因为它不起作用。
我试过使用 update_post_meta( $product->id, '_price', $frame_price_added);
,当页面刷新时,它会添加和存储产品的自定义价格,并保存它。
关于如何正确实现此目标的任何想法?
谢谢。
Edit: 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 had woocommerce_single_product_summary
but it didn't find any function.
Update: For WooCommerce 3.0+
您可以使用 woocommerce_before_calculate_totals
挂钩来自定义您的购物车商品价格。
您可以通过这种方式在函数中将 $framed_price
变量定义为全局变量。
这是代码:
// 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 文件或任何插件文件中。
参考:
我想使用这段简单的代码更新在购物车中添加自定义价格的产品的价格 update_post_meta( $product->id, '_regular_price', $frame_price_added);
。
注意:我想要实现的是将此自定义价格添加到购物车中的每个产品。
我尝试通过这种方式获得 $frame_price_added
:
$frame_price = $res['_number_field'][0];
$frame_price_added = $product->price + $frame_price;
这里 $product->price
是来自 woocomerce 产品的价格,$frame_price
来自我新添加的价格。
我想知道如何将这个新价格关联到购物车,因为它不起作用。
我试过使用 update_post_meta( $product->id, '_price', $frame_price_added);
,当页面刷新时,它会添加和存储产品的自定义价格,并保存它。
关于如何正确实现此目标的任何想法?
谢谢。
Edit: 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+
您可以使用 woocommerce_before_calculate_totals
挂钩来自定义您的购物车商品价格。
您可以通过这种方式在函数中将 $framed_price
变量定义为全局变量。
这是代码:
// 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 文件或任何插件文件中。
参考: