Chord OMS
...
Next.js
SDK Reference
useCart
15min
the usecart hook returns information about the current shopping cart, and functions for creating and updating the shopping cart example import { usecart } from ' /hooks/actions' const { cart, addpromocode, addtocart, applygiftcard, checkout, loadcart, modifyquantity, removefromcart, removegiftcard, removepromocode } = usecart() returns the usecart hook returns an object with these properties property description cart an object representing the current order addpromocode a function that applies a promo code to the current order addtocart a function that adds a product variant to the current order applygiftcard a function that applies a gift card to the current order checkout a function that redirects the browser to shopify checkout loadcart a function that loads the current cart based on the current browser session modifyquantity a function that modifies the quantity of a line item in the cart removefromcart a function that removes a line item from the cart removegiftcard a function that removes a gift card from the current order removepromocode a function that removes an applied promo code from the cart functions addpromocode the addpromocode function applies a promo code to the current order arguments addpromocode(promocode) argument type description promocode string the promo code to apply to the current order returns promise\<void> a promise that resolves when the promo code has been applied to the order and the cart object has been updated to reflect the promo code example import { usecart } from ' /hooks/actions' const { addpromocode } = usecart() const promocode = '50off' await addpromocode(promocode) addtocart the addtocart function adds a product variant to the current order arguments addtocart(variantid, quantity, placement, position, customattributes) argument type description variantid string the product variant shopify id to add to the cart quantity number the quantity of the variant to add to the cart placement? string (optional) a string representing the placement of the product position? number (optional) a number representing the position of the product in the shopping cart customattributes? array (optional) an array of custom attributes ( { key '', value '' } ) returns promise\<void> a promise that resolves when the new line item has been added to the cart example import { usecart } from ' /hooks/actions' const { addtocart } = usecart() const variantid = '123' const quantity = 1 await addtocart(variantid, quantity) applygiftcard the applygiftcard function applies a gift card to the current order arguments applygiftcard(giftcardcode) argument type description giftcardcode string the gift card code to apply to the current order returns promise\<void> a promise that resolves when the gift card has been applied to the order and the cart object has been updated to reflect the gift card example import { usecart } from ' /hooks/actions' const { applygiftcard } = usecart() const giftcardcode = 'xxxyyyzzz' await applygiftcard(giftcardcode) checkout the checkout function redirects the browser to shopify checkout arguments none returns promise\<void> a promise that resolves when the browser has been redirected to shopify checkout example import { usecart } from ' /hooks/actions' const { checkout } = usecart() await checkout() loadcart the loadcart function loads the current cart based on the current browser session this is automatically called on every page load and with every other usecart method arguments none returns promise\<void> a promise that resolves when the cart object has been added to redux example import { usecart } from ' /hooks/actions' const { loadcart } = usecart() await loadcart() modifyquantity the modifyquantity function modifies the quantity of a line item in the cart arguments modifyquantity(lineitemid, quantity) argument type description lineitemid string the id of the line item to update quantity number the desired quantity of the line item returns promise\<void> a promise that resolves when the cart object has been updated in redux example import { usecart } from ' /hooks/actions' const { modifyquantity } = usecart() const lineitemid = '123' const quantity = 2 await modifyquantity(lineitemid, quantity) removefromcart the removefromcart function removes a line item from the cart arguments removefromcart(lineitemid) argument type description lineitemid string the id of the line item to update returns promise\<void> a promise that resolves when the cart object has been updated in redux example import { usecart } from ' /hooks/actions' const { removefromcart } = usecart() const lineitemid = '123' await removefromcart(lineitemid) removegiftcard the removegiftcard function removes a gift card from the current order arguments removegiftcart(appliedgiftcardid) argument type description appliedgiftcardid string the id of the gift card to remove returns promise\<void> a promise that resolves when the cart object has been updated in redux example import { usecart } from ' /hooks/actions' const { removegiftcard } = usecart() const giftcardid = 'xxxyyyzzz' await removegiftcard(giftcardid) removepromocode the removepromocode function removes an applied promo code from the order arguments removepromocode(promocode) argument type description promocode string the promo code to apply to the current order returns promise\<void> a promise that resolves when the promo code has been removed from the order and the cart object has been updated to reflect the promo code example import { usecart } from ' /hooks/actions' const { removepromocode } = usecart() const promocode = '50off' await removepromocode(promocode)