Skip Navigation

[Resolved] Update woocommerce price based on Form submission

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to dynamically calculate price based on a custom fields

Solution:
Process the price calculation using cred_save_data action hook

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 4 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 7 replies, has 3 voices.

Last updated by Joe H. 4 years ago.

Assisted by: Raja Mohammed.

Author
Posts
#1831421

Tell us what you are trying to do?
I am building a Toolset Christmas Gift Catalog fundraiser website where Families submit a front end Form that asks how many children they have (numerical field). This Form creates a Pending woocommerce Product. I need this '# of children' form field to be used in a calculation that sets the woocommerce "price" of their "product" listing. Sponsors will then add a family to their shopping cart, checkout, and make a donation to my charity to buy christmas gifts for the family.

E.g. if the Family answers in the Form that they have 3 children, then upon approval of the form submission, a formula would multiply 3 x $50 AND populate the Woo product price with a suggested donation amount of $150. Sponsors can then choose to sponsor (woo checkout) this family for $150.

Is there any documentation that you are following?
https://toolset.com/course/custom-woocommerce-sites/

Is there a similar example that we can see?
Not that I am aware. This is similar except without the front end form submission: hidden link

What is the link to your site?
hidden link

#1831877

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You are using a front-end form to publish a product post, and you want to set the price of that product based upon a custom field submitted with the form.

For that you would use the Forms API, namely the cred_save_data hook which enables you to run code after the form has been submitted and the post created.

So your code would retrieve the value of the children custom field, calculate the correct price, and save it using add_post_meta.

Note that WooCommerce stores the price in a "hidden" custom field, i.e. the meta key has an underscore: "_price".

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/get_post_meta/
https://developer.wordpress.org/reference/functions/add_post_meta/

#1832515

Uh, this response points me in the right direction but without more help it requires knowing which functions to use and how to use them to calc the _price which in my case would be $25 x _family-size

_family-size is my custom field and is a select field in the form, its a value from 1 to 6.

I didnt get very far... any additional help here?

/**
* update woo product price based on family size
*/
add_action('cred_save_data_1553', 'save_data_for_form_with_id_1553',10,2);
function save_data_for_form_with_id_1553($post_id, $form_data)
{
//some code here

// add it to saved post meta
add_post_meta($post_id, '__my_custom_field', $_POST['my_custom_field'], true);
}

#1833107

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello there,

The calculation for the prices would go inside the function and the code will look something like the one below, In the code i am assuming the base price as static if that is set in a different way kindly update the code accordingly

add_action('cred_save_data_1553', 'save_data_for_form_with_id_1553',10,2);
function save_data_for_form_with_id_1553( $post_id,  $form_data ) {
  // Assuming a static base price 
  $price = 25;

     //Make sure family_size is not empty or 0
      if(  !empty( $_POST['_family-size'] )  ) {
       // calculate the price
       $price  = $price * intval( $_POST['_family-size'] );
      }
  // add family size to the post meta
  add_post_meta($post_id, '_family-size', intval($_POST['_family-size']), true);
  // Add price to the product
  add_post_meta($post_id, '_price', $price, true);
}

I hope this helps better, let me know if you still need further assistance.

Kind Regards
Raja

#1833693
Screen Shot 2020-11-03 at 11.14.17 AM.png

Thank you Raja for this reply. The code you provided (thank you) did not work as is . It does add a price to the Product but always $25 no matter what number is selected by the User front end form for _family-size.

Yes, the base price is static. It is always $25.

I have attached a screenshot of my _family-size Select field used in my front end form. It can not be zero. It is a required field and so is always integer 1 - 6.

Using your code I modified to this, which also does not work but perhaps demonstrates what I am trying to do. To troubleshoot the code you provided I attempted to simplify it.

I am not sure why I would need to 'add family size to the post meta' since the Family is submitting the Form and selecting a famil-size. This form feeds into the Product custom field _family-size

add_action('cred_save_data_1553', 'save_data_for_form_with_id_1553',10,2);
function save_data_for_form_with_id_1553( $post_id,  $form_data ) {
  // Assuming a static base price
  $price = 25;
  $quantity = get_post_meta($post->ID, '_family-size');
 
  // calculate the price
  $total = $price * $quantity;
	
  // add family size to the post meta
  //add_post_meta($post_id, '_family-size', intval($_POST['_family-size']), true);
  // 
  // Add price to the product
  add_post_meta($post_id, '_price', $total, true);
}

As I say, my version does NOT work either and may not be the best way/most elegant. But I am hoping it shows what I am trying to do. Seems like it ought to be simple but I cant figure it out.

#1833769

I was able to solve it using this code:

add_action('cred_save_data_1553', 'save_data_for_form_with_id_1553',10,2);
function save_data_for_form_with_id_1553( $post_id,  $form_data ) {
	// Base price per child
	$price = 25;

	//calculate price based on family size
	$price  = $price * $_POST['wpcf-_family-size'];
	
	// Add price to the product
	add_post_meta($post_id, '_regular_price', $price, true);
	add_post_meta($post_id, '_price', $price, true);
}

I went back to your code to troubleshoot it and determined that the main problem is that wpcf- is needed in order to correctly get the family size. Simply using '_family-size' does not work. But using 'wpcf-_family-size' does work.

Also, simply updating _price updates the front end but updating _regualar_price is needed to show the price in the back end admin columns.

#1833969

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad that you managed to get the issue resolved,

I misunderstood the family_size as a generic form field and hence provided the code to add the family_size to meta.
Since the field is from the types wpcf-_family-size is the correct way to handle the field in the code

Thanks for sharing the solution that works for you.

Let me know if there is anything else i can assist with.

#1835035

My issue is resolved now. Thank you!