I'm trying to access the getCurrentCart API in a backend file called "cart.web.js" This is the code in "cart.web.js":
import {Permissions, webMethod} from 'wix-web-module';
import { currentCart } from "wix-ecom-backend";
export const myGetCurrentCartFunction = webMethod(
Permissions.Anyone,
async () => {
try {
const myCurrentCart = await currentCart.getCurrentCart();
console.log("Success! Retrieved current cart:", currentCart);
return myCurrentCart;
} catch (error) {
console.error(error);
// Handle the error
}
},
);
As you can see, is the same code suggested in the VELO Docs.
so when I try to run the myGetCurrentCartFunction it returns me an error like the following:
I also added some code to the "CART PAGE" in order to access myGetCurrentCartFunction
import { myGetCurrentCartFunction } from 'backend/cart.web';
$w.onReady(function () {
const getCurrentCart = async() => {
const cart = await myGetCurrentCartFunction();
console.log(cart);
}
getCurrentCart();
});
The same error is returned even in the preview mode
Any ideas?
Hi Ricardo, As for the backend, it makes sense that there would be no current cart as the functional testing happens on the server and there is no frontend with a user and cart. As for preview mode, here I would hope for it to work but also wouldn't be too surprised as there are often things that don't work properly in preview mode (especially when it comes to simulation of different user permission). I would publish a test version of the site and test again on the live site. If you still encounter the same issue please attach the steps you took to test and the results and I'd be glad to take another look. Best, Eitan