Question:
I’ve implemented a custom product creator which creates a new product and then adds to the product to the cart. I have noticed that the cart.addProducts does not always update the cart and no errors are given.
Timeouts are included (5000 ms at the moment), as recommended in the documentation, and 1 out of every 10 cart.AddProducts fails to add the product.
Because it does work for the majority of the time, this points to it not being a coding issue but rather some issue with cart.AddProducts.
Anyone had a similar issue and been able to resolve? I am thinking this is somethin g Wix needs to be looking at.
If you’re comfortable sharing more details about your code implementation, the specific product creation process, and any error messages (even if none are displayed), I might be able to offer advice.
Hi.
Thanks for the offer. As mentioned my code is pretty much a straight implementation of the createProduct and cart.addProduct from the WIX documentation.
Just for anyone else who has this problem. The documentation says: If you create a product immediately before adding it to the cart, we suggest setting a timeout for "1000"
milliseconds (1 second) before calling wix-stores.cart.addProducts()
. While this slows down the operation slightly, it also ensures proper retrieval of the newly created product before adding it to the cart.
I put up to a 5000 ms timeout and still had issues occassinally when adding to the cart - this didn’t throw an error but simply didn’t add the product to the cart. As mentioned, this happened 1 or 2 times out every 10 efforts.
To solve, I wrote a WHILE loop to check if the product was in fact available in the products collection. The WHILE loop queries the collection every 1000 ms until it gets a result and then adds to cart. This has solved the problem.
What is interesting is that it sometimes takes up to the 8th try (8000 ms) for the product to actually be created in the product collection.
So the 1000 ms is greatly underexaggerated and may work for someone sitting right next to the Wix data centres but definitely not for someone sitting in South Africa.
1 Like