๐งช Advanced Cart Usage
This page covers advanced operations available through the useCart()
hook โ useful for personalization, account-aware carts, and attribute customization.
๐งน Remove Discount Codeโ
const { removeDiscountCode } = useCart();
const clearDiscount = () => {
removeDiscountCode();
};
๐งบ Empty Cartโ
const { emptyCart } = useCart();
const clearAll = () => {
emptyCart();
};
๐ Merge Cartsโ
To merge another cart into the current one (e.g. after login):
const { mergeCarts } = useCart();
const handleMerge = async () => {
await mergeCarts("gid://shopify/Cart/OLD_CART_ID");
};
๐ค Update Buyer Identityโ
Used to associate a buyer to a cart:
const { updateBuyerIdentity } = useCart();
const handleUpdate = async () => {
await updateBuyerIdentity({
email: "john@example.com",
phone: "+33600000000",
countryCode: "FR",
});
};
๐งพ Update Cart Attributesโ
const { updateCartAttributes } = useCart();
const handleUpdate = async () => {
await updateCartAttributes([
{ key: "custom_message", value: "Deliver between 9-11am" },
{ key: "custom_flag", value: "b2b" },
]);
};
๐งฌ Set Individual Cart Attributeโ
const { setCartAttribute } = useCart();
const handleSet = async () => {
await setCartAttribute("gift_note", "Happy Birthday!");
};
โ Remove Individual Cart Attributeโ
const { removeCartAttribute } = useCart();
const handleRemove = async () => {
await removeCartAttribute("gift_note");
};
โ Next: Standalone API Methods โ
Description
Advanced cart operations using useCart, including cart merging, identity updates, and custom attributes.