Question:
How can I remove an item from the current cart using Velo?
Product:
Wix Editor
What are you trying to achieve:
Hi,
I am building a custom cart page where I successfully list all the items in the current cart using a repeater. Each item has a delete button, and I need assistance in removing an item from the cart when this button is clicked.
What have you already tried:
Based on the updated Velo documentation, I understand that I should use the removeLineItemsFromCurrentCart()
API to remove items. However, I’m facing issues retrieving the current cart data. When attempting to use the API, I receive an error message saying that the cart owner could not be found.
Could you please guide me on how to resolve this issue? How can I properly remove an item from the current cart, especially when the user is not logged in?
“Error: message: ‘Cart not found: Cannot find cart by ownership’
details:
applicationError:
description: Cannot find cart by ownership
code: OWNED_CART_NOT_FOUND
data: {}”
Additional information:
This is the code I have.
–cart.web.js–
import wixData from ‘wix-data’;
import { currentUser } from ‘wix-users-backend’;
import { Permissions, webMethod } from “wix-web-module”;
import { currentCart } from “wix-ecom-backend”;
import { getCurrentCart } from ‘wix-stores-backend’;
export const myRemoveLineItemsFromCurrentCartFunction = webMethod(
Permissions.Anyone,
async (lineItemIds) => {
try {
const updatedCurrentCart =
await currentCart.removeLineItemsFromCurrentCart(lineItemIds);
console.log(“Success! Line items removed from cart:”, updatedCurrentCart);
return updatedCurrentCart;
} catch (error) {
console.error(error);
// Handle the error
}
},
);
—Front Page----
import { myRemoveLineItemsFromCurrentCartFunction } from “backend/cart.web.js”;
$item(“#deleteItemButton”).onClick(() => {
console.log("Butona tıklandı.");
try {
wixData.query('AddToCartInformation')
.eq('productID', itemData.productID)
.eq('timestamp', itemData.timestamp)
.eq('userID', wixUsers.currentUser.id)
.find().then((results) => {
if (results.items.length > 0) {
console.log("Veri bulundu:", results.items[0]);
} else {
throw new Error("Ürün bulunamadı");
}
const productIdToUpdateCart = results.items[0].productID;
const quantityToUpdateCart = results.items[0].quantity;
console.log("Product ID: " + productIdToUpdateCart + "Product Quantity: " + quantityToUpdateCart);
getCurrentCart()
.then((cart) => {
console.log("Sepet:", cart);
// Sepet boş mu dolu mu kontrol et
if (cart.lineItems.length > 0) {
// Sepet bilgilerini göster
cart.lineItems.forEach(item => {
console.log(`Ürün: ${item.name}, Adet: ${item.quantity}`);
console.log("İtem id: ", item.productId);
compareProductQuantities(productIdToUpdateCart, quantityToUpdateCart,
item.productId, item.quantity);
//removeProductFromCurrentCart(item.productId);
// Sample lineItemIds array:
const lineItemIds = [
results.items[0].productID,
];
console.log("Backend fonksiyonundan hemen önceye geldim.");
console.log(cart._id);
myRemoveLineItemsFromCurrentCartFunction(lineItemIds)
.then((updatedCurrentCart) => {
console.log("Success! Line items removed from cart:", updatedCurrentCart);
return updatedCurrentCart;
})
.catch((error) => {
console.error(error);
// Handle the error
});
});
} else {
console.log("Sepet boş");
}
})
.catch((error) => {
console.error("Sepet alınamadı:", error);
});
})
} catch (error) {
console.log(error);
}
});