Use these methods to manipulate the contents of the cart:
setItems(items[]): Overwrites the entire cart with a new array of items. Each item must have an id and price. If no quantity is provided, it defaults to 1.addItem(item, quantity?): Adds an item to the cart. The item must have an id and price. quantity defaults to 1.updateItem(itemId, data): Updates specific properties of an existing item using its id.updateItemQuantity(itemId, quantity): Directly updates the quantity of a specific item.removeItem(itemId): Removes an item from the cart entirely.emptyCart(): Removes all items and resets all totals to 0.
const {
setItems,
addItem,
updateItem,
updateItemQuantity,
removeItem,
emptyCart
} = useCart();
// Examples
setItems([{ id: '1', name: 'Product', price: 100 }]);
addItem({ id: '2', name: 'Product 2', price: 200 }, 2);
updateItem('1', { name: 'Updated Name' });
updateItemQuantity('1', 5);
removeItem('1');
emptyCart();