I am currently building an ecommerce site with PHP/MySQL. Recently, I have been working on the Shopping Cart integration. The client wanted to ensure that stock was available to potential buyers, so I created a stock management system. The shopping cart works as follows:
- Client adds a quantity of an item to his cart.
- Item quantity is reserved from available stock in the database.
- No one else can purchase reserved stock.
- Stock remains reserved until client processes order - where stock is then removed from database.
- If client abandons his cart, stock remains reserved.
- If another client wishes to buy an item, but only available stock is reserved by another client, then the client can steal the reserved stock if it has been inactive for 20 minutes.
My question is, what are best practices for this kind of scenario? Am I doing this correctly? The main thing is that the client does not want to sell stock that he does not have.
I am looking to have a discussion on how to improve the functionality, or what others are doing to accomplish this.