Hello,
I am using EditorX and have a Store but rather than displaying all items, I actually have just one product page for one product. Additionally, I have 2 step process similar to Patagonia.com when adding an item to the cart. In other words, I have a custom Button named “Add to Bag” which makes a call to cart.addProducts() on click and adds a product of quantity 1 with selected options to the cart. At least that is what it should do …
I am not using the default Add To Cart button because it doesn’t give me the flexibility to specify all of the product options I want.
I am following the API documentation here Cart - Velo API Reference - Wix.com
I am able to test it in Preview mode. However, once published, the same code throws an INTERNAL_SERVER_ERROR on the cart API call saying that it cannot resolve the cartId
because it is null.
The error I get in production from the cart.addProducts API call is:
FetchError: {"message":"Cannot read properties of null (reading 'cartId')","locations":[{"line":3,"column":5}],"path":["cart","addToCart"],"extensions":{"code":"INTERNAL_SERVER_ERROR","exception":{"name":"ERROR:addToCart:RESOLVER_ERROR"}}}
Any help is much appreciated! Here is my code:
// I understand that wix-stores.getCurrentCart() is deprecated but cart.getCurrentCart() isn't and I wanted to see if the cartID is actually missing. I am able to get the cart and the cartId is not missing!
cart.getCurrentCart()
.then((currentCart) => {
const cartId = currentCart._id;
const cartLineItems = currentCart.lineItems;
console.log(cartId); // this exists when I log it
console.log(cartLineItems); // this exists when I log it
})
.catch((error) => {
console.error(error);
});
// This is where the problem is on the live site. This call returns the error
cart.addProducts(productsToAdd).then((updatedCart) => {
// Products added to cart
console.log(updatedCart);
let dataObj = {
"cart": updatedCart,
"size": sizeSelection.label,
"color": colorSelection.label
}
wixWindow.openLightbox("AddedToBagLightbox", dataObj);
}
})
One more thing, I am able to navigate to the cart page with. There are no products added though because the server call fails…
import {navigate} from 'wix-stores';
...
navigate.toCart();
Thanks,