[英]Javascript shopping cart logic
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.我有一个 javascript 函数,它当前将商品价格(产品的价格,在本例中为比萨饼)添加到购物车中,然后将其乘以商品数量以返回总金额。 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.这部分工作正常,但是我现在需要能够将第一个附加项目的价格更改为 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.现在我似乎只能将它们全部更改为 5 美元,但不能像下面第二个代码块中看到的那样,在原始版本之后每增加一个。 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)我的解决方案(将每件商品的价格改为 5 美元,而不是第一件商品之后的每件附加商品)
getItemTotal(item) {
if(item.tags == "pizza"){
item.price = 5;
}
@user269867 was correct in assuring me that this code will work. @user269867 向我保证此代码将起作用是正确的。
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.我只需要根据条件多次调整 item.price 才能在我的用例中工作。
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 :)谢谢收听 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.