Updates cart UI elements, like the Cart Icon and Side Cart with the most recent cart data.
The refreshCart()
function returns a Promise that resolves when the latest cart data is retrieved, and the cart UI elements are refreshed.
Note: Calling the refreshCart()
function will trigger the onCartChange()
event handler.
function refreshCart(): Promise<void>;
import wixEcomFrontend from "wix-ecom-frontend";
import wixEcomBackend from "wix-ecom-backend";
$w("#myAddAndRefreshCartButton").onClick(() => {
// example values for the options object
const options = {
lineItems: [
{
catalogReference: {
// Wix Stores appId
appId: "215238eb-22a5-4c36-9e7b-e7c08025e04e",
// Wix Stores productId
catalogItemId: "3fb6a3c8-988b-8755-04bd-5c59ae0b18ea",
},
quantity: 1,
},
],
};
// The wixEcomBackend.currentCart module is universal.
// You can call its methods in frontend and backend code.
wixEcomBackend.currentCart
.addToCurrentCart(options)
.then(() => {
console.log("item added to cart");
// refresh the cart after adding item
wixEcomFrontend.refreshCart();
})
.catch((error) => {
console.error(error);
// Handle the error
});
});
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.