I have a javascript function that currently adds the item price (of a product, in this case pizza) into a shopping cart and then multiplies it by the quantity of items to return the total amount. This part works fine however I now need functionality to be able to change the price of each additional item after the first to $5. Right now I can only seem to change them all to $5 but not each additional one after the original as seen below in the second code block. How can I get this to work?
The original function (which works like a charm)
getItemTotal(item) {
let total = item.price * item.quantity;
item.options.forEach(option => (total += option.value * item.quantity));
return total;
}
My solution (which changes every item price to $5 and not each additional item after the first item)
getItemTotal(item) {
if(item.tags == "pizza"){
item.price = 5;
}
@user269867 was correct in assuring me that this code will work.
if (item.tags == "pizza"&&(item.quantity >2 )) { item.price = 5;}
I just had to adjust the item.price based upon the condition multiple times for it to work in my use case.
if(item.tags == "pizza" && item.quantity === 2){
item.price = 4.99;
}
else if (item.tags == "pizza" && item.quantity === 3){
item.price = 4.65;
}`
Thanks for listening :)
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.