1

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
jord49
  • 542
  • 2
  • 17
  • 37
  • @LoicTheAztec thank you this helps me start in the right direction. I'll take a look at that. – jord49 Dec 11 '17 at 12:31

1 Answers1

0

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399