Shopping cart and stock management

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 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.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

An alternative approach may be not to reserve a stock upon putting it in the shopping cart. Perform a check each time a page is reloaded, should the item be no more available, display a message like “The item you wish to buy has just been sold out. It will be available shortly”. And you remove the product from the shopping cart.

Now, you absolutely have to reserve the shopping cart contents right before you initiate the payment operation, then either remove it from the stock or remove the reserve depending on the success/failure of the payment. You do it better in one code run, so that the reserve lasts as briefly as possible.

ProcessOrder ()
    bool reserved = ReserveShoppingCartContents ();
    if (reserved)
        bool paymentStatus = ProcessPayment ();
        if (paymentStatus)
            RemoveShoppingCartContentsFromStock ();
            ReleaseShoppingCartReserve ();
        RefreshShoppingCartContents (); // Remove positions or adjust quantities
        MessageBox ("Could not reserve your shopping cart contents. Please check out your selection");

The briefer your reserve lasts, the higher the chance your item will be actually sold. You minimize the possibility of a conflict: CustomerA begins with the shopping cart, the item gets reserved, CustomerB comes, sees the item is not on stock and goes away, CustomerA decides he doesn’t like the price and cancels the operation. You had two potential customers but couldn’t sell to either.

Method 2

i do a check for the stock on every reload of the pages during the checkout proccess and redirect them to the cart page with an error message if during the process the items have been sold out.
The stock is reduced only on order confirmed
Also i restore the stock if the order is canceled.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x