Pregunta

I'm building an ecommerce system and I was thinking about the best way to handle items that were added to an user cart.

For example, lets say that I'm about to buy product i've just added to my shopping cart, automaticaly if there were 10 items of this product there will be only 9 available, but for some reason now I dont want it and just closed my browser, how am I supposed to cancel that request?

I mean how am I gonna turn 9 available products into 10 again since there was no confirmation of paymant?

I was thinking maybe about using session to do so, for example when you add an item to your cart, you'll have 5 minutes to confirm otherwise it will be available again but it works only when that user is constantly browsing the site, só that I can verify that but not if he just closed you know?

Any help?

Yours, Diogo

¿Fue útil?

Solución

You update Your stock count after user proceed to checkout not when he adds it to cart. And put it back in stock if payment was not made in a week or some other time You choose.

Plus You check on each request or when he gets to cart page if all items are still available, maybe someone bought it in the meantime, and inform user about it.

But if You insist on updating stock when adding to cart then You will have to keep cart content in db and keep track of user activity by updating timestamp of last request. You do a cronjob that checks every x minutes for inactive users and puts back items in stock. You could add some automatic ajax request on page so if user didn't close browser but is afk to just keep him active.

Otros consejos

Short answer: Let everyone try to buy you out, until someone successfully pays you, at which point everyone else is told not enough stock.

Long answer: Suppose there are 2 people vying for all 10 items. Let's suppose you don't have AJAX. Both see 10. Both put 10 items into their basket. Both put their credit card info and both click submit to order.php. First thing in order.php: acquire a mutex. Check inventory. If enough inventory, charge card. If successfully charged, decrement inventory and release mutex and return success. If too little inventory or card fails, release mutex and return error.

you deduct the stock after the credit card transaction has completed. most people do not complete the purchase for what they put in their cart.

the other tangent on this is that people can put stuff in their cart and not buy it -- for a long time. so at the checkout stage - besides confirming stock on hand - you are also confirming the price in case its changed.

related issue is ONE customer who tries to order more then there is stock on hand. they put 5 in their cart and you only have 3. by checking inventory you can give them a polite message.

all you should store in the "cart" is the product id and the quantity. anytime something is added to the cart, or a quantity updated, and at the checkout stage - you confirm the inventory and get the prices for all the cart items.

this is also to prevent someone being able to hack your cart, change the prices and give themselves a discount. the very minimum is -- do not send the product price when the customer clicks the buy button. only send the product id and optionally qty, then do the pricing and totals based on the info you get back from the products table.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top