Home » Item #795
I've noticed that one of my products has a stock quantity on -1. It continues to be available for purchase on my store. I've been using stock tracking for ages and haven't had any problems till now. Any ideas why?
There are actually two problems here - the fact that you've got a -1 for a stock quantity, and why a -1 quantity is being sold on the site.
The reason a -1 quantity occurred is most likely because the version had a quantity of 1, and then 2 people purchased the product 'at the same time'. The timing doesn't have to be exact - they just have to get to the final stage of payment at the same time. Basically, throughout the site the basket is checked to see if there is enough in stock to order what's in the basket. If there isn't, then the stock is removed from the basket and they are directed to basket.asp with an explanation for why. So lets say version X has a quantity of 1 left, and you and I are browsing the site and we both add 1 of version X our basket. Then you go through and make your purchase while I'm still browsing the site. The stock count gets reduced to 0, and I then find that the product I had in my basket has been removed. So far, so good.
The problem occurs when we both go through to payment at the same time. Your site is using worldpay, so we've both got to payonline.asp and we get forwarded off to worldpay. At this point there's still 1 stock left in the database. Once we both pay at worldpay, the callback is made and the stock count is reduced twice - first for your order, then for mine - hence the -1 value. Even though the callback page may already know that the stock is on 0, there's nothing it can do to refuse the payment because it's already been taken by worldpay.
You should be able to check the orders history to confirm that that's what happened.
And unfortunately there's not really a way around this. One suggestion is that stock reduction is done at the stage of adding to a basket - therefore stopping me adding version X to the basket if you already have. Baskets would eventually time out if they aren't purchased, 'freeing' the stock again. This would fix the problem, but it would create lots of others - the main one being that it would leave the site open to quite an easy attack (someone could just add everything to their basket every few minutes, stopping everyone else for purchasing).
By the nature of payment gatways there's not much that can be done. The only real way of getting round this is always to keep surplus in stock, so if you do get a -1 stock you can still sell to both. Because this occurs very rarely (and only on big sites), keeping an extra 1 or 2 in stock for popular products would do.
Regarding the second problem; there's a check to see if the stock count is 0 - if it is, it displays an out of stock message. Because the stock is set to -1 it passes this check and it's available on the site. This issue is fixed in v5.104 onwards.