Please see the screenshot, i have linked the Ticket Type dropdown to a dataset. I then want when a customer selects how many adults and children are going it multiplies the value of the Ticket Type. Then the value is displayed underneath?
How do i set the Values of each option under Ticket Type when linked to a dataset?
How do i calculate based on adult and child quantity?
How do i show the total balance?
Hey
Let me help you a bit here.
If you connect a Drop down to a dataset the title field will be used as both value and label in the drop down. So if you want the title as label and some other field like ticketPrice as value you will need to code the data into the drop down yourself.
First of all I will give you my function that populates a dropdown with one field as label and another as value.
export function populateDropdown(collectionName) {
let allItems = [];
return wixData.query(collectionName)
.find()
.then((results) => {
if (results.totalCount > 0) {
let items = results.items;
items.forEach((item) => {
let oneItem = {
label: item.title,
value: item.yourfield_with_ticket_type // MODIFY HERE
}
allItems.push(oneItem);
})
return allItems;
}
return null;
})
}
You use that this way
$w("#dropdown1").options = populateDropdown("Name of your data collection here");
Then the ticket type field will be as value in the #dropdown1 and we can start calculate.
let ticketType = $w("#dropdown1").value; // Ticket price here
let adults = Number($w("#adultsgoingDropdown").value); // Nr of adults
let children = Number($w("#childrengoingDropdown").value); // Nr of children
As you see in the snippet above I use Number(value) and that is to make sure all our values are converted to numbers before we start calculating. If there is a string in there calculations will break.
So I can’t see your pricing so I am just making things up here for you to try with
if (ticketType === "seating_area4") {
let price = 250;
let sum = (adults * price) + (children * price);
} else if (ticketType === "seating_parkett") {
let price = 450;
let sum = (adults * price) + (children * price);
}
$w("#addtocartprice").text = "€ " + sum.toString()
And when you set a number and want to use that in a .text property you need to use toString() on that to convert it into a string. If you miss this you will get an error in the developer console.
I hope that will get you going using Wix Code.
thank you that is great, i will crack on and use the above and see what happens. one question though, if the ticket price is different for an adult and child how would that be calculated?