The best way to achieve this is to simply sell the item to the first person that purchases it, and then have different "states" and order can be in. States can include "ordered" "shipped" "complete" "back ordered" "refunded"
Step 1 of the checkout procedure is to check if there is an item left, The final step of the checkout is reduce the quantity by 1.
If there is 1 item left and both users enter the checkout form and submit seconds apart the system will process the first request, the second request will check the quantity which will now be 0 because the first checkout took it.
All you need is a special check in code that checks to see if the user completes a transaction and if the quantity is zero, then you set the state of the order "back ordered" You can display a special message to the user and give them the option to cancel.
In e-commerce you never want to not take someone's money. If it later turns out that you can't order more of that product you do a refund or exchange for a similar products.
Having states on the orders is a great way of tracking orders in the backend. Shippers need to look at orders that are "ready to ship" then they can change it to "shipped", users can track the "state" of their orders... etc.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community