Chord OMS
...
Developer Tools
Gatsby

SDK Reference 3.x

72min

Version 4 is now available! Check out the Release Notes to read about its exciting new features, and v3.x to v4.x Migration Guide for details on how to upgrade.

Introduction

The Chord client-side Javascript SDK can be installed via a Gatsby theme. It's available as an npm package, @chordcommerce/gatsby-theme-autonomy, via GitHub Packages.

Despite the name, this package does not provide a visual theme. To quote from the Gatsby documentation:

Gatsby themes are plugins that include a gatsby-config.js file and add pre-configured functionality, data sourcing, and/or UI code to Gatsby sites. You can think of Gatsby themes as separate Gatsby sites that can be put together and allow you to split up a larger Gatsby project!

gatsby-theme-autonomy includes the Chord SDK, a set of methods you can import and use in your React components to do things like add items to a cart and check out. The theme also preconfigures your Gatsby site with a number of useful ecommerce features like product feeds and internationalization support.

useAnalytics()

JS


The useAnalytics hook returns a series of functions that fire a tracking event to Segment, our integrated analytics platform.

Many Segment events are fired automatically by the @chordcommerce/gatsby-theme-autonomy theme. However, some events need to be closely tied to the UI, like click tracking. These events are available via the useAnalytics hook and listed below.

trackClickCollection()

JS


The trackClickCollection function fires a Collection Clicked event to Segment. You should call this function when the user clicks on a link to a collection.

Example

JS


trackClickProduct()

JS


The trackClickProduct function fires a Product Clicked event to Segment. You should call this function when the user clicks on a link to a product.

Example

JS


trackCreateStockRequest()

JS


The trackCreateStockRequest function fires a Stock Request Created event to Segment. You should call this function when a product waitlist form is submitted.

Example

Text


trackViewCart()

JS


The trackViewCart function fires a Cart Viewed event to Segment. You should call this function when the cart page is viewed.

Example

Text


trackProductViewed()

Text


The trackProductViewed function fires a Product Viewed event to Segment. You should call this function when a product page is opened.

Example

JS


useAuth()

Text


The useAuth hook returns information about the current authentication session and functions for logging in and logging out. Chord supports passwordless authentication with Magic.

isLoggedIn

isLoggedIn is a boolean indicating whether the user is currently logged in.

Example

Text


isFetching

isFetching is a boolean indicating whether the user's authentication status is currently being fetched.

Example

Text


token

token is a string representing the user's authentication token. This token is automatically appended to Chord API requests when the user has an active session.

handleCallback()

JS


The handleCallback function completes the login process, when the user is redirected back to the site after clicking a link in their email. The SDK calls this function automatically when the Magic url parameter is detected on page load, so you are unlikely to need to call this function.

getToken()

Text


The getToken function retrieves an authentication token for the current user session. While you are unlikely to need the token directly, since the SDK automatically appends it to relevant Chord API requests, you can use this function to check whether the user's session is still active. If the user's session has expired, getToken will throw an error.

You may choose to call getToken on page load or when a user visits a protected route (like an account page) to check that their session has not expired since the last time you checked.

Example

Text


login()

The login function should be called when a user submits a login form. It will trigger an email to the user for verification.

The showUI option controls whether Magic's default UI will display from when login is called until the user clicks the link they've received in an email.

Example

Text


logout()

Text


The logout function terminates the user's current session.

Example

Text


useCart()

Text


cart

Example

Text


addPromoCode()

Text


Example

Text


addReferralIdentifier()

JS


Example

Text


addToCart()

Text


Example

Text


checkout()

Text


Example

Text


finalizeCheckout()

Text


This function should be called on the order confirmation page, when the user is redirected back to the site from checking out via Stripe.

Example

Text


forgetCart()

forgetCart()

This function can be called at any time to clear the current order from the session. It is called automatically by finalizeCheckout and logout, so no need to call it in those cases.

Example

JS


loadCart()

Text


modifyCart()

JS


modifyQuantity()

Text


modifyLineItem()

Updates a line item in the current order.

JS


modifyGiftCards()

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.

Text


removeFromCart()

Text


removePromoCode

Text


subscribeProduct()

Text


Note: endDate is optional. The subscription will renew indefinitely if endDate is not specified.

Example

Text


useProduct()

Text


createStockRequest()

The createStockRequest adds the customer to a product variant wait list in the OMS. When the variant is marked back in stock, customers on the wait list will automatically receive an email notification.

For kits, you will need to pass both sku and slug as options to the createStockRequest hook method.

Example

JS


useSubscription()

Text


The useSubscription hook returns the current loaded subscription and functions for managing the subscription.

subscription

The subscription object represents a subscription. You will need to explicitly call loadSubscription before this object will be populated.

Example

Text


loadSubscription()

Text


The loadSubscription function loads information about the subscription from the Chord API.

Example

Text


skipSubscription()

Text


The skipSubscription function calls the Chord API to skip the next subscription delivery.

Example

Text


cancelSubscription()

Text


The cancelSubscription function calls the Chord API to cancel a subscription.

Example

Text


pauseSubscription()

Text


ThepauseSubscription function calls the Chord API to pause a subscription.

Paused subscriptions will not be processed until manuallyresumedor theactionable_dateis reached.Subscriptions can be either paused until a specifiedactionable_dateby sending an optionalactionable_dateparam or paused indefinitely by not passing any params. The minimum date to whichactionable_datecan be set is the next day.

Text


resumeSubscription()

Text


TheresumeSubscriptionfunction calls the Chord API to resume a subscription.

Paused subscriptions will not be processed until manuallyresumedor theactionable_dateis reached.Subscriptions can be resumed at a specifiedactionable_dateby sending an optionalactionable_dateparam or resumed the next day (the minimum to which actionable_datecan be set) by not passing any params.

Text


updateSubscription()

Text


TheupdateSubscription function calls the Chord API to update a subscription.

To update the addresses, all fields need to be sent even if only a single field is updated. The two addresses that can be updated are shipAddressAttributes and billAddressAttributes.

In addition, the interval_unit('day'/'week'/'month'/'year'), interval_length, actionable_date and the end_date(null for no end date ) can be updated.

Text


useTranslate

Text


The useTranslate hook provides support for internationalization, powered by gatsby-plugin-intl.

To localize the text in your React components, use the translate function returned by the useTranslate hook. translate will load the specified string from your language resource file.

We recommend using translate even if your site currently only supports one language. It's easier to edit your website text if it's all in a single configuration file, and it makes it trivial to add multi-language support in the future.

Example

Text


The component above assumes there is a file at /src/intl/<language>.json that looks like this:

JS


useUser()

Text


The useUser hook returns the current user and functions for loading data about the user. At the moment, the only user information Chord supports is the user's referral identifier and personalized referral url.

user

The user object represents the current user. You will need to explicitly call loadUser or loadUserReferralIdentifier before this object will be populated.

Example

Text


loadUser()

Text


The loadUser function loads information about the current order's associated user from the Chord API.

Example

Text


loadUserReferralIdentifier()

Text


The loadUserReferralIdentifier function loads the referral identifier information for a user with the given email address from the Chord API.

Example

Text


modifyUser()

The modifyUser function is used to update the user's profile information.

Example

Text


useSquareCheckout

Text


This module allows to checkout using Square, the typical flow is:

updateOrderAddresses()

Text


The updateOrderAddresses() function is used to update the order's ship address. A valid ship address has the following format:

Text


To retrieve the available states and the country id, please use getStates().

Example

Text


getStates()

Text


The getStates() function is used to retrieve information about countries and states available in the system to be used when we need to update the order's address. It takes the ISO code of the country and returns the list of states along with other information related to the country itself.

Example

Text


updateOrderDelivery()

Text


The updateOrderDelivery() function allows choosing the shipping option for the current order. At the moment we do not support multiple delivery options, so this function is only needed to move the checkout from the delivery step to the payment step. During this process, shipping costs and taxes will be added to the order.

This function should be called only when the order is in the delivery state, meaning that a valid address has been added to the order previously. This way it will be able to correctly pick the right delivery option based on the shipping address and to correctly compute taxes based on the shipment route.

Example

Text


updateOrderPayment()

Text


The updateOrderPayment() function allows completing the payment step of the checkout. Before using this function, the client should retrieve a token using the Web Payments SDK provided by Square. When the order is successfully completed, the payment source will be added to the user's wallet (for authenticated users only), allowing to re-use the payment token later, without having to input the credit card information again.

This function should be called only when the order is in the payment state, meaning that a valid delivery option has been added to the order previously. At this stage the order is updated with shipping costs and taxes, allowing to complete the payment with the right amount.

For now, this endpoint doesn't support the ability to pay with an existing token. In the future we will provide this support, allowing to re-use a previously saved credit card from the user wallets.

Example

Text


finalizeCheckout()

Text


finalizeCheckout() is the last method that needs to be called to complete the order with Square. Call this method when you want to move the user to the thank you page and let them start a new order.

Example

Text


This function should be called only when the order is in the "confirm" state, meaning that a valid payment has been added to the order already.



Updated 28 Jul 2023
Did this page help you?