我想更新使用这段简单的代码 update_post_meta( $product->id, '_regular_price', $frame_price_added);
在购物车中添加自定义价格的产品价格 .
Note: 我想要实现的是将此自定义价格添加到购物车中的每个产品 .
我试着以这种方式获得 $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);
,当页面刷新时,它会将自定义价格添加并存储到产品中并保存 .
关于如何正确实现这一点的任何想法?
谢谢 .
编辑:还有一件事......我搜索了一个可以在添加到购物车时被调用的功能,我没有找到任何东西,还有一个动作挂钩被调用woocommerce_template_single_add_to_cart,它有woocommerce_single_product_summary,但它没有找到任何功能 .
1 回答
您可以使用 woocommerce_before_calculate_totals hook来自定义购物车商品价格 .
您可以通过这种方式将 $framed_price 变量定义为函数中的全局变量 .
这是代码:
或者在挂钩功能中获取您的自定义价格(可选,取决于您获得自定义价格的方式):
This code is tested and working.
当然,这段代码会出现在活动子主题(或主题)的function.php文件中或任何插件文件中 .
参考:WooCommerce Cart - Dynamic Price variable pass into custom price hook