Shopping cart and stock management

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 ();
        else
            ReleaseShoppingCartReserve ();
    }
    else
    {
        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.


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.