简体   繁体   中英

Pass custom calculated product price to cart in Woocommerce

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM