Delete Item from Shoppingcart on Button click

Hey Guys,

quick question, is it possible to create a button, wich onClick deletes an Item form the Mini Shopping Cart?
Like the opposite of the addtocart function.

Thank you in advance

Yes possible if you made your online store fully with Wix Code. I don’t know if its possible with the Wix Stores API or not.

See Demonstration in video below (Delete Function at 2:11 )

Your Delete Button Code shall look like the below given that you want to delete a particular item entered by the user. For example: The Cart Database can hold hundreds of items but I can only modify or delete the item entered by me which contains my userId.

export function deleteItem_click(event, $w) {
$w("#loader").show(); //a loader to let the user know the deletion is in progress
 let user = wixUsers.currentUser;
 let userId = user.id;
 let currentItem = $w("#dataset1").getCurrentItem();
 const itemId = event.context.itemId; // this is the item in the repeater assuming that the button is in the repeater.
    wixData.query("Cart") // get the item from the database collection.
    .eq("userId", userId)
    .find()
    .then( (results) => {
 let remove = results.items[0]._id; // instead of results.items[0]
        deleteCartItem(remove, itemId);
    });
}

async function deleteCartItem(remove, itemId){
 let user = wixUsers.currentUser; 
 let userId = user.id;
 await wixData.remove("Cart", itemId); //removes the item
 await $w("#dataset1").refresh(); //refresh your cart to remove the item from on screen
    wixData.query("Cart")
    .eq("userId",userId)
    .find()
    .then((results) => {
 let items = results.items;
        checkoutTotal(items); //if you have a total value displayed, update that
        $w("#loader").hide(); //hide your loader
    });
}

Thank you Mate! You are genious! This little snippet helped me to create a complete workarround on the default Shopping Cart from Wix!

Im having trouble implementing this in my array as well.

can you add some more info about exagtly what “Cart” refers to?

Hi shan, I have a platform where users can input information and the information populates a database collection, that database collection is displayed on a repeater that is connected to the database collection in the user’s profile . Please what is code that I can use so that the user can click a delete button on the repeater to delete an item from the repeater. Thanks.

Hi Folks!

It’s been a few years since this was answered, so for anyone who stumbles across this thread here’s an update before an admin comes along an arbitrarily closes this post.
Since then, the function to remove from the native WIX shopping cart has been exposed in Velo. It is a promise, so handle with care.

cart . removeProduct ( cartLineItemId ) → You’ll get this ID from the cart. It’s close to a position id.

https://www.wix.com/velo/reference/wix-stores/cart/removeproduct

1 Like

hello can u post your site as will as other code snippet here