Skip to main content

๐Ÿงช 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 โ†’