Pass custom calculated product price to cart in Woocommerce

前提是你 提交于 2019-12-22 00:18:00

问题


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.

Many thanks.


回答1:


First step:

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="">';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Then you will need to pass your calculated price with jQuery in this hidden input field, (or to make your calculation inside this function and set the value directly in this hidden input field).

The complete code: Set cart item price from a hidden input field custom price in Woocommerce 3



来源:https://stackoverflow.com/questions/47752907/pass-custom-calculated-product-price-to-cart-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!