
Hello, Power Apps community,
I'm developing a shopping cart application in Power Apps with a peculiar issue related to how items are added and updated in the cart. The application consists of two galleries:
Each item in the cart gallery includes '+' and '-' buttons for adjusting item quantities.
Issue: When I first add an item to the cart, the '+' and '-' buttons do not update the TotalPrice correctly, sometimes setting it to zero. However, clearing the collection (the shopping cart) and adding an item again causes the buttons to work as expected.
Conversely, changing the reference from ItemPrice to Price in the button functionality corrects the issue on the first addition—where TotalPrice updates correctly—but subsequent additions after clearing the cart behave unexpectedly.
Code for Adding Items to Cart:
If(
LookUp('Inventory(VendingMachine)', Title = ThisItem.Title).'Quantity Available' > Coalesce(LookUp(ItemQuantities, Title = ThisItem.Title).QuantitySelected, 0),
If(
IsEmpty(Filter(ItemQuantities, Title = ThisItem.Title)),
Collect(
ItemQuantities,
{
Title: ThisItem.Title,
QuantitySelected: 1,
ItemPrice: ThisItem.Price,
TotalPrice: ThisItem.Price,
ItemImageURL: ThisItem.'Image URL'
}
),
Patch(
ItemQuantities,
LookUp(ItemQuantities, Title = ThisItem.Title),
{
QuantitySelected: LookUp(ItemQuantities, Title = ThisItem.Title).QuantitySelected + 1,
TotalPrice: (LookUp(ItemQuantities, Title = ThisItem.Title).QuantitySelected + 1) * ThisItem.Price
}
)
),
Notify("Not enough stock available", NotificationType.Error)
)
Code for '+' Button:
If(
LookUp('Inventory(VendingMachine)', Title = ThisItem.Title).'Quantity Available' > ThisItem.QuantitySelected,
Patch(
ItemQuantities,
LookUp(ItemQuantities, Title = ThisItem.Title),
{
QuantitySelected: ThisItem.QuantitySelected + 1,
TotalPrice: (ThisItem.QuantitySelected + 1) * ThisItem.ItemPrice
}
),
Notify("Not enough stock available", NotificationType.Error)
)
Question: What could be causing this inconsistency in how the TotalPrice is updated, depending on whether it's the first addition or after clearing the cart? How can I ensure consistent behavior regardless of the addition order?
Any insights or suggestions would be greatly appreciated!
I fixed the issue, the workaround I found is that I calculated the price looking up the price from the original data source rather than the one saved in the collection.
TotalPrice: (ThisItem.QuantitySelected + 1) * ThisItem.ItemPrice
TotalPrice: (ThisItem.QuantitySelected + 1) * LookUp('Inventory(VendingMachine)', Title = ThisItem.Title).Price