0

This question already has an answer here:

Context

I am creating a e-commerce website. We sell products in a really limited amount (+/- 10 available items per product).

Problem

How to deal with stock updates when a user makes a purchase?

Example

We have 5 items for a product.

The user checks out his cart with 4 items, fills in his details and goes through the purchase process. He arrives on the third party payment website. At this point, the payement has not been sent yet so we don't descrease the quantity purchased from the available stock.

Now let's assume that during that time, an other user had the 3 items for the same product and goes to the purchase process. We have a situation where, if they both pay for these items, we will sell more than available stock.

Questions

How could we prevent that? I am thinking of having a 'pending purchase' DB table, with the orders, that would timeout after 10 minutes(?), or the user could cancel it himself. But what would be the appropriate moment to insert the pending purchase into that table? When the user clicks 'checkout'? When he click 'confirm purchase' and goes to the payment process? And what is the appropriate moment to decrease the stock?

I can't really figure out a good workflow that would prevent users from actually being able to checkout if the quantity is not available anymore. We want to prevent as much as possible that the user would encounter an error during the checkout process because the quantity/product that he wants the purchase is not available anymore.

marked as duplicate by MetaFight, Community Jul 20 '15 at 11:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @MetaFight I've looked around, but finding the right keywords revealed to be a little tricky... Thanks for the link :) – skirato Jul 20 '15 at 10:56
  • No worries. Posting a duplicate isn't the end of the world :) – MetaFight Jul 20 '15 at 10:56
  • @skirato I was just basically writing a comment repeating my answer for the question MetaFight linked. Didn't even remember I ever answered it there. – thorsten müller Jul 20 '15 at 10:58
  • @thorstenmüller Your answer is very interesting. I would like to know, however, how do I block a quantity? I've mentioned a DB table in my question. I guess I would need a cron job to check every 10-20 minutes if the order is still valid? – skirato Jul 20 '15 at 11:03
  • You either have the basket in your database or the 'pending purchase' table you mentioned. In any case every record has a timestamp, so you can simply add all records for a given article that are not older than 10 minutes (or whatever) and subtract that amount from stock. A lot depends here on amount of customers on your site and how often this really happen (chances are less often then you may think). If this becomes a serious problem (you should log as much data about this as possible, especially if you show customers 'out of stock' messages) you should consider to stock higher volumes. – thorsten müller Jul 20 '15 at 11:09

Browse other questions tagged or ask your own question.