Skip to main content

removeCartItem

Description

Removes an item from the cart by its UUID. This function is a hookable service that allows extensions to hook into the cart item removal process.

Import

import { removeCartItem } from '@evershop/evershop/src/modules/checkout/services';

Usage

import { removeCartItem } from '@evershop/evershop/src/modules/checkout/services';

const removedItem = await removeCartItem(cart, itemUuid, context);

Parameters

NameTypeDescription
cartCartThe cart object from which the item will be removed
uuidstringThe UUID of the item to remove
contextRecord<string, unknown>Context object for hookable functions

Return Value

Returns a Promise that resolves to the removed Item object.

Example

import { removeCartItem } from '@evershop/evershop/src/modules/checkout/services';

async function handleRemoveItem(cart, itemUuid) {
try {
const removedItem = await removeCartItem(cart, itemUuid, {
userId: 123,
sessionId: 'abc123'
});

console.log('Removed item:', removedItem.getData('product_name'));
return removedItem;
} catch (error) {
console.error('Failed to remove item:', error.message);
throw error;
}
}

Hooks

This function is hookable. Extensions can register hooks to execute before or after the item removal:

import { hookAfter } from '@evershop/evershop/src/lib/util/hookable';
import { removeCartItem } from '@evershop/evershop/src/modules/checkout/services';

hookAfter(removeCartItem, async (removedItem, cart, uuid, context) => {
// Log the removal for analytics
await logCartItemRemoval(cart.getId(), removedItem.getData('product_sku'));
return removedItem;
});

See Also