I'm trying to decrease the price when the user presses the minus icon using the initial price, but then the new price after increasing is the initial price
quantityHandler: function (action, product) {
// Increase or decrease quantity of product
try {
const newItems = [...this.products]; // clone the array
let idx = newItems.indexOf(product);
let currentQty = newItems[idx].productQuantity;
let price = newItems[idx].unitPrice;
if (action == "more") {
newItems[idx].productQuantity = currentQty + 1;
newItems[idx].unitPrice = price + price;
} else if (action == "less") {
newItems[idx].productQuantity = currentQty > 1 ? currentQty - 1 : 1;
// Decrease current price using the initial price
}
this.products = newItems; // set new state
console.log(this.products);
} catch (error) {
alert(error);
}
},
quantityHandler: function (action, product) {
// Increase or decrease quantity of product
try {
const newItems = [...this.products]; // clone the array
let idx = newItems.findIndex((item) => { //compare with some unique identifier
return item.id === product.id
})
let currentQty = newItems[idx].productQuantity;
let price = newItems[idx].unitPrice;
let perQtyPrice = price / currentQty //per unit price
if (action == "more") {
newItems[idx].unitPrice += perQtyPrice;
newItems[idx].productQuantity = currentQty + 1;
} else if (action == "less") {
if(currentQty > 1) {
newItems[idx].unitPrice -= perQtyPrice;
newItems[idx].productQuantity = currentQty > 1 ? currentQty - 1 : 1;
}
}
this.products = newItems; // set new state
console.log(this.products);
} catch (error) {
alert(error);
}
},
You need to find the per item price then use that to subtract or add to the price