Chord OMS
...
Next.js
SDK Reference
useCart
20min
this page's documentation only applies to chord's next js starter prior to the release of the react docid\ ewxkrz0jhh0zpphikkcri the next js starter now uses the react sdk and does not use the methods documented on this page check out the react sdk reference for complete documentation, and the react sdk migration guide docid\ qmilhjfpz9wbjyzbe50ku for details on how to upgrade 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' // inside a function component const { cart, addtocart } = usecart() returns the usecart hook returns an object with these properties property description cart an object representing the current shopping cart addpromocode a function that applies a promo code to the cart addreferralidentifier a function that associates a referral identifier with the cart, so the referrer can get credit addtocart a function that adds a product variant to the cart checkout a function that prepares the cart for checkout by attaching a checkoutsessionid to be passed to stripe's checkout finalizecheckout a function that loads the finalized order upon checkout completion loadcart a function that loads the current cart based on the current browser session modifycart a function that modifies the current cart modifygiftcards a function that applies or removes gift cards from the current cart 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 removepromocode a function that removes an applied promo code from the cart subscribeproduct a function that adds a product variant subscription to the cart crosssell a function that adds an array of line items to the order post checkout functions addpromocode the addpromocode function applies a promo code to the current shopping cart arguments addpromocode(promocode) argument type description promocode string the promo code to apply to the current shopping cart returns promise\<void> a promise that resolves when the promo code has been applied to the cart and the cart object has been updated to reflect the promo code example import { usecart } from ' /hooks/actions' const promocodeform = () => { const { cart, addpromocode } = usecart() const onsubmit = async ({ promocode }) => { try { await addpromocode(promocode) } catch(error) { console error('error applying promo code ') } } } addreferralidentifier the addreferralidentifier function associates a referral identifier with the cart, so the referrer can get credit this would likely be called on initial site load, based on a referral identifier in the url arguments addreferralidentifier(identifier) argument type description referralidentifier string the referral identifier to associate with the current shopping cart returns promise\<void> a promise that resolves when the referral identifier has been associated with the cart example import { usecart } from ' /hooks/actions' export default () => { const { addreferralidentifier } = usecart() const onsubmit = ({ referralidentifier }) => { addreferralidentifier(referralidentifier) } } addtocart the addtocart function adds a product variant to the current shopping cart (creating a line item) arguments addtocart(sku, quantity) argument type description sku string the product variant sku to add to the cart quantity number the quantity of the variant to add to the cart returns promise\<void> a promise that resolves when the new line item has been added to the cart example import { usecart } from ' /hooks/actions' export default () => { const { addtocart } = usecart() const onsubmit = ({ sku, quantity }) => { addtocart(sku, quantity) } } checkout the checkout function prepares the cart for checkout by attaching a checkoutsessionid to be passed to stripe's checkout it should be called just before redirecting to stripe's checkout arguments none returns promise\<cart> a promise that resolves with the updated cart object when the cart has a checkoutsessionid attached example import { usecart } from ' /hooks/actions' import { loadstripe } from '@stripe/stripe js' const stripepromise = loadstripe(process env stripe pk key, { stripeaccount process env stripe connected account }) export default () => { const { checkout } = usecart() const onsubmit = async () => { const cart = await checkout() const stripe = await stripepromise stripe redirecttocheckout({ sessionid cart checkoutsessionid }) } } finalizecheckout the finalizecheckout function should be called when the user is redirected back to the site from stripe checkout it retrieves the finalized order for display on the order confirmation page this function expects two url parameters to be present, and uses them to finalize the checkout checkout session id and number stripe will attach these url parameters when redirecting back to the site arguments none returns promise\<cart> a promise that resolves with the finalized cart object this method is the only way to access the finalized cart example import { setstate } from 'react' import { usecart } from ' /hooks/actions' export default () => { const { finalizecheckout } = usecart() const \[cart, setcart] = usestate(null) useeffect(() => { finalizecheckout() then(cart => { setcart(cart) }) }, \[finalizecheckout]) } loadcart the loadcart function loads the current cart based on the current browser session and populates the cart in redux loadcart is automatically called on page load arguments none returns promise\<void> a promise that resolves when the cart object has been added to redux example import { usecart } from ' /hooks/actions' export default () => { const { cart, loadcart } = usecart() useeffect(() => { loadcart() catch(error => { console error(error message) }) }, \[loadcart]) } modifycart the modifycart function modifies the current cart arguments modifycart(options) argument type description options object an object to send to the chord oms update order endpoint https //chord stoplight io/docs/chord oms/b3a6mte3nzk0mjk update order returns promise\<void> a promise that resolves when the cart object has been updated in redux modifygiftcards the modifygiftcards function applies or removes gift cards from the current cart the giftcardsdetails object is returned in the lineitem object if the lineitem is a gift card additionally, newly redeemed gift cards are available in the user object for indicating to the user that they have received a gift the gift cards can be updated by including the gift card id along with any of the following params recipientname, recipientemail, giftmessage, purchasername, sendemailat the recipient email is a required field as that will be the account used to redeem the gift cards arguments modifygiftcards(options) argument type description options object an object to send to the chord oms bulk gift card endpoint returns promise\<void> a promise that resolves when the cart object has been updated in redux example modifygiftcards({ "giftcardsdetails" \[ { "recipientemail" "test\@chord co", "giftmessage" "hi!", "sendemailat" "2021 11 15", "id" "49" }, { "recipientemail" "admin\@chord co", "giftmessage" "", "sendemailat" "2021 11 16", "id" "50" } ] }) modifyquantity the modifyquantity function modifies the quantity of a line item in the current cart arguments modifyquantity(lineitemid, quantity) argument type description lineitemid number 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 removefromcart the removefromcart function removes a line item from the cart arguments removefromcart(lineitemid) argument type description lineitemid number the id of the line item to update returns promise\<void> a promise that resolves when the cart object has been updated in redux removepromocode the removepromocode function removes an applied promo code from the cart arguments removepromocode(promocode) argument type description promocode string the promo code to remove from the current shopping cart returns promise\<void> a promise that resolves when the cart object has been updated in redux subscribeproduct the subscribeproduct function adds a product variant subscription to the cart arguments note enddate is optional the subscription will renew indefinitely if enddate is not specified subscribeproduct(options) argument type description options sku string the product variant sku for the subscription options quantity number the quantity for the subscription options interval object the interval for the subscription options interval length number the number of units example if the subscription should be delivered every three months, this would be 3 options interval unit "day" | "week" | "month" | "year" the interval unit example if the subscription should be delivered every three months, this would be month options enddate? string (optional) a string representing the end date of the subscription if not provided, the subscription will renew indefinitely returns promise\<void> a promise that resolves when the cart object has been updated in redux example import { usecart } from ' /hooks/actions' export default () => { const { subscribeproduct } = usecart() const onsubmit = ({ sku, quantity, interval, enddate }) => { subscribeproduct({ sku, quantity, interval, enddate }) } } crosssell the crosssell function adds an array of line items to the order post checkout and charges the credit card again arguments crosssell(cart, lineitemsattributes) argument type description cart string the number of the order (cart) lineitemsattributes array of objects an array of line item objects that can be both subscription and standard line items returns promise\<void> a promise that resolves when the cross sell has been completed example import { usecart } from ' /hooks/actions' const { crosssell } = usecart() const cart = "order 1234567" const lineitems = \[ { "sku" "product 1", "quantity" 2, "subscription line items attributes" \[ { "interval length" 3, "interval units" "month" } ] }, { "sku" "product 2", "quantity" 4 } ] await crosssell(cart, lineitems)