I’m creating a travel agency website and the client needs two different prices for each product. One for children and other for adults.
For example, the product price for adults is $100 and for children it’s $90. So in the single product page I want two input fields: “Quantity of adults” and “Quantity of children”, and I want it to calculate the price based on the quantity the user inputs.
So for example if the user types “2 adults” and “1 child”, the cart total should be $290
Can anyone give me a hand on how to do it?
I know how to create the custom field for the children price in the product admin and the input fields in the single product page, but I don’t know how to make the calculation and show it in the cart.
Thanks in advance
2
Answers
I thought about using product variations but I want the user to be able to add both quantities into cart at once, without having to add first adults and after children.
I'm quite new to programming so I definitely don't know the best practices to achieve what I want, but here's what I tried so far was:
Created an additional field to receive the children discount in the product admin page. So the price for adults will be for example $100, and children get $10 discount.
Created input fields in the product page like so:
Now as I don't know how to send these quantity fields and calculate the value, here's what I did:
I have hidden the original Woocommerce quantity field using css (opacity: 0) and used jquery to calculate the qty of children + qty of adults, and then set the result in the hidden woocommerce quantity field. So let's say for example the user inputs 5 adults and 2 children, jquery will calculate 5 + 2 and set 7 to the woocommerce standard quantity field, so the cart will have a total quantity of 7.
Then what I want to do is, calculate the quantity of children times the children discount field set in the product, and set the value as a discount using the cart calculate fees action:
I really don't know the best practices to achieve this and I'm probably doing it in a very bad way, but I hope you guys can understand better what I want and point me in the right way.
Thanks again!
I think you can use product variations for the same result.
You can define 2 variations, Adult and Child, each one with his specific prices.
Look, this was made in test site.
https://i.stack.imgur.com/446c5.png Define variations.
https://i.stack.imgur.com/6Se8h.png Child
https://i.stack.imgur.com/hr2QE.png Adult
It would display as such.
If this doesn’t work for you, please provide more info about your problem and Wc Setup. Such as code and errors you found. That will make easier for us to help you.