I am having a problem with my power app, my app is simulating a store, where you can add items to a shopping cart, here are the parts of it, first when you click on the image of the item, this gets added to the cart, here is the code for the on select property for the button behind the image:
If(
LookUp('Inventory(VendingMachine)', Title = ThisItem.Title).'Quantity Available' > LookUp(ItemQuantities, Title = ThisItem.Title).QuantitySelected,
// If there is stock available, add or increment the item
If(
CountRows(Filter(ItemQuantities, Title = ThisItem.Title)) = 0,
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
}
)
),
// Else notify the user that no more stock is available
Notify("Not enough stock available", NotificationType.Error)
)
so this adds them to the shopping cart, fine: then when the items are on the cart, each item has a - and + button underneath it, where you can either add or decrease the amount of items in the shopping cart, there is also another button that works for clearing the cart: Clear(ItemQuantities), here is the issue though, the cart works perfectly, but when I clear the cart and add any item item, the + and - button do not work properly, they are not adding up the prices for the items, the price becomes 0.00, also the collection that serves as the shopping cart is called ItemQuantities, what is the issue here? here is the code for the + button:
If(
LookUp('Inventory(VendingMachine)', Title = ThisItem.Title).'Quantity Available' > ThisItem.QuantitySelected,
// If there is stock available, add or increment the item
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
}
)
,
// Else notify the user that no more stock is available
Notify("Not enough stock available", NotificationType.Error)
)
, here is the code for the - button: If(
LookUp(ItemQuantities, Title = ThisItem.Title).QuantitySelected > 1, // Check if more than one item is selected
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
}
),
// If only one item is selected, remove it from the collection
If(
LookUp(ItemQuantities, Title = ThisItem.Title).QuantitySelected = 1,
Remove(ItemQuantities, LookUp(ItemQuantities, Title = ThisItem.Title))
)
)
Sorry it took me so long to accept your response. It is working as expected, yeah, the inventory is on one gallery and the shopping cart is another gallery.
Hey @Eldor488,
Please correct me if I'm incorrect in how your gallery is set up, but from what I understand, you have 2 galleries:
- One containing the Inventory ('Inventory(VendingMachine)') and;
- Another containing the selected items (your shopping cart) (ItemQuantities) and the '-' and '+' buttons are inside this gallery containing that collection.
If this is the case, I believe it's due to the ItemQuantities collection not containing a 'Price' property/column, as your formula is calculating TotalPrice from ThisItem.Price. As the collection doesn't contain 'Price', it is multiplying quantity by 0. If you change it to the below, it should calculate the total using the item's price.
ThisItem.Price -> ThisItem.ItemPrice
// + Button:
If(
LookUp('Inventory(VendingMachine)', Title = ThisItem.Title).'Quantity Available' > ThisItem.QuantitySelected,
// If there is stock available, add or increment the item
Patch(
ItemQuantities,
LookUp(ItemQuantities, Title = ThisItem.Title),
{
QuantitySelected: LookUp(ItemQuantities, Title = ThisItem.Title).QuantitySelected + 1,
TotalPrice: (LookUp(ItemQuantities, Title = ThisItem.Title).QuantitySelected + 1) * ThisItem.ItemPrice
}
)
,
// Else notify the user that no more stock is available
Notify("Not enough stock available", NotificationType.Error)
)
WarrenBelz
146,537
Most Valuable Professional
RandyHayes
76,287
Super User 2024 Season 1
Pstork1
65,908
Most Valuable Professional