i am trying to create a ecommerce website for my school project,but i am confused about when to update the quantities in the database.When i am going to add the product to cart or when i complete the order?If i decrease the quantity when i add the product in the cart and the customer doesn't complete the order then i must increase the quantity again.So the only way is to update the quantity when a customer completes the order,and if 2 customers go to the final page together then to put an if statement to check the quantity in order to finalize the order,and if one of the 2 customers took the last product then the other will get a message like"We are verry sorry,the last product sold 2 minutes before". Is this the right way of thinking?Any other idea?(Sorry for my english.)
This is whats called a race condition.
In a situation like this you will need to update the stock for the first customer to complete their transaction in a transaction and notify the other customer that the stock is gone , or use your first idea and decrement the stock but if the customer cancels reincrement. Either should work with planning.
You have basically two options, which are both valid:
1) If what you're selling is something in high demand (you have a limited amount and expect lots of users buying it, like a ticket for a concert or a ticket for a flight) you can "reserve" a product when the user chooses it (that's how ticketmaster handles it). This is always done with a timer associated with the reservation, so that if the user doesn't complete his purchase, the item is available again and some other user can buy it
2) If you have a large stock and don't think a lot of users will be interested in a particular item, you can just store the items the user chose in the session, and once everything is good, decrease the amount of items on the db. Here, you should also consider when to charge the credit card... b/c if you charge and then decrease, you may have to void it (with a cost for you). So, the ideal thing in this scenario would be decreasing the stock and charging to the db if you didn't run out of the things the user ordered. If you did, you should explain the situation to the user and let him find "similar items" or do whatever you think it's best... however, I wouldn't advise to still process the invoice with remaining items, it may not make sense to the user (ie, buying a pc and a bagpack, the pc is unavailable... should you still ship the bagpack?)
Anyway, hope it helps.
Well, the answer to this question is very closely tied to the policies of this particular store. There are a few possibilities here:
There are a few more, but this is the line of "policy decisions" you need to assess.
If they are unique or discontinued items, then you'll need to adjust accordingly (obviously the customer cannot wait until they return to stock).
I've also done it where customers can request to be notified when the item returns to stock.
I would recommend doing whatever maximizes your conversions, and limits frustration for your customers.
I faced similar issue some time back when i attempted to build my first ecommerce shop. One of things i did was to provide a temporary storage such current_stock in the db and use it as the pseudo stock. When one customer puts that item in cart , the quantity is deducted from the pseudo storage and the real stock is deducted upon ordering. The pseudo stock becomes the visible stock for users browsing the site.
hope it helps!