将自定义计算的产品价格传递到Woocommerce中的购物车 [英] Pass custom calculated product price to cart in Woocommerce
问题描述
我已经使用jquery在单个产品页面中计算了自定义价格,并将其输出为totalCost变量。我如何使用该值并将其覆盖或传递给结帐,以便能够将此新价格用作产品价格?
I have calculated a custom price within the single product page using jquery and have output it as a totalCost variable. How can I use this value and overwrite or pass it through to the checkout to be able to use this new price as the product price?
我会发布一些代码,但我真的不知道从哪里开始。我看到的示例只是在函数文件中设置了全局价格覆盖。
I would post some code but I really dont have a clue where to start. Examples ive seen just set a global price override in the functions file.
非常感谢。
推荐答案
第一步:
您需要添加一个自定义隐藏字段,您将在其中传递计算出的价格:
You should need to add a custom hidden field, where you will pass your calculated price:
// Adding a custom imput hidden field in add to cart form
add_action( 'woocommerce_before_add_to_cart_button', 'custom_hidden_product_field', 11, 0 );
function custom_hidden_product_field() {
echo '<input type="hidden" name="custom_price" class="custom_price" value="">';
}
代码包含在您活动的子主题的function.php文件中(
然后,您需要在此隐藏的输入字段中使用jQuery传递计算出的价格, (或在此函数中进行计算并直接在此隐藏的输入字段中设置值)。
这篇关于将自定义计算的产品价格传递到Woocommerce中的购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!