Creating empty order for every visitor? #1027
-
Hi, Also received 30 error messages like these (which are maybe related to this?): |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
My guess would be one of the Simple Commerce tags used in your layout is creating new orders for each user. I've had this problem crop up before and managed to prevent it in most cases. If you have any custom tags/middleware/controllers which deal with Simple Commerce code, it could potentially be that too. I just had a look at my local version of your site I have from another issue and I can't seem to reproduce it (😞). A new order is only created for me when I add something to the cart.
Yes, I think that'll be related. You're probably hitting that error due to multiple orders being created at exactly the same time. Before an order is saved in the database, Simple Commerce generates an order number based on the last order + 1. If multiple orders are being created at exactly the same time, they'll all have the same order number, so when it tries to save more than 1 order, you'll hit that error since order numbers are intended to be unique. Ideally, I think it'd probably be better to have the database handle the order generation itself. However, doing that is probably a bit of a bigger job and might even need to be done in a major version (if it's a breaking change), so I've opened a feature request for it: #1030. In the meantime, to avoid that error from occurring, you could probably tweak the order number generation logic to make it slightly more random (like appending a random string to the very end of the order number, so it'll always be unique) or implement some kind of locking, so only one order can get an order number at once. You can extend the Sorry I couldn't provide any firm answers in this reply 😬 |
Beta Was this translation helpful? Give feedback.
Hi Duncan,
Thanks for your detailed explanation.
I investigated all possible tags/middleware/controllers linked to Simple Commerce and found that the
{{ sc:cart }}
tag looping over items in the header was the problem (This placement provides a mini cart on hover of shopping cart icon). On new sessions this tag was creating an empty cart, so put that within a{{ if sc:cart:count > 0 }}
and now it doesn't seem to happen anymore.Hopefully this will also solve the other error as a lot fewer carts are now being created.
Thanks again for your help en also fixing the other issue!