views:

273

answers:

2

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.

+6  A: 

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.

Developer Art
Thanks for the advice. I am going to rework the functionality. I knew it wasn't the best way to go about doing this. Freezing stock is probably not a good idea, unless your selling unique products, such as concert tickets.
Jon
Yes, as with source control and database transactions, default to optimistic locking unless you have a good reason to do otherwise.
Jerph
+1  A: 

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.

solomongaby