1

I'm trying to implement a little shopping system. I have a single product which has a limited stock.

For example, my product can be bought 5 times.

My problem is, if a user adds this product to his cart, fill the form and pay.

Decrement the stock when he adds the product to his cart (problem: if he quits the site before the payment? The product stock is decremented, but not bought)

Decrement the stock when the payment is confirmed (problem, if 2 or more user pay at the same time)

Have you got any advice on the best way to solve this?

Thanks

Kilian Foth
  • 110,899
anubis
  • 111

1 Answers1

0

I would look at it as if it was a physical shop. Customer A goes to a shelve, takes the products needed from the shop into his or her cart and may or may not proceed to buy the products in the cart. Customer B can go to the shelve, however he or she can not pick up the same stock as Customer A as that stock has moved into Customers A cart.

The point is to not handle stock as a number, but as actual objects that move through your system, until they are checked out.

You should then have a time out mechanism, so that if someone leaves a cart with stock in your shop, stock is put back on the shelve at some point. Think of it as a shop clerk going through the store and cleaning up after the customers after the shop has closed for the night.