useCart
The useCart
hook allows you to access cart data and perform mutations that will reflect on other entities within the Checkout data layer.
Usage
import { useCart } from '@vtex/checkout'
const TotalItems = () => { const cart = useCart()
return <div>Items: {cart.items.length}</div>}
Additionally, you can use useCart
to interact with the Checkout data layer by mutating the cart items list:
import { useCart } from '@vtex/checkout'
const TotalItems = () => { const cart = useCart() const addItem = () => cart.addItem({ quantity: 1, seller: '1', id: '8392' })
return <button onClick={addItem}>Add to cart</button>}
Parameters
The useCart
hook does not receive any parameters.
Returns
The useCart
hook returns an object with the following properties:
items
- type:
CartItem[]
addItem
- type:
addItem: (data: AddItemData) => Promise<void>
The AddItemData
type is an object with the following structure:
type AddItemData = { quantity: number; id: string; seller: string;};
};
Extension Points
This hook is available in the following extension points:
cart.cart-list.after
cart.cart-item.after
For a full list of available extension points, refer to the ExtensionPoints page.