Subtract item count based on cart count

So I have a shopping app, and the store has a list of items: [A, B, C, D, E]
When a user adds items to their cart (for eg say they added these): [A, B, C]
Say a 2nd user logs in and adds these items to their cart: [B, B, D, E] (that’s not a typo, I allow them to add 2 items individually - customer’s request)
Each time a user adds an item to their cart, I add 1 to the item’s “Added in cart” and subtract 1 from that item’s “In stock”.
When a user removes that item, the reverse happens, and I’m able to do that.
However, I’ve given the option for each user to completely exit the app and leave.
Say the 2nd user Exits the app.
I would like the workflow to see that they had [B, B, D, E] in their cart, and then from the item’s “Added in cart” automatically subtract the correct number – in this case 2xB, 1xD, 1xE.

How do I go about doing that?

1 Like