Step 3: Initialize the SDK
Pass the store ID and CartConnector to init() to initialize the SDK. Initialization makes no network requests. You use the CheckoutPlusSession that init() returns in the following steps (such as mount() in Step 4 and prepareCheckout() in Step 5). For what the SDK does and when after initialization, see SDK states and transitions.
Follow these rules when you initialize and call refresh().
- Call
init()from a place that runs only once each time the page loads. If you call it from a place that can run again on the same page, such as inside a component, calldestroy()as described in Deciding whether to calldestroy(). Don't initialize inside a component that opens and closes, such as a cart drawer; initialize on the page, and call onlymount()andunmount()in the drawer (same section). - Call
refresh()once right after initialization. This removes a Checkout+ product left over from the previous screen, and it's needed even on screens without the banner. If you initialize again, call it again right after that. In a SPA, if you don't initialize again when the screen changes, you don't need to call it again (refresh: Call it once right after initialization). - When your code changes the cart (changing quantities, adding and removing products, applying discount codes, and so on), call
refresh()after the update finishes (refresh()). After the SDK adds or removes the Checkout+ product, you only fetch the cart for your UI again inonCheckoutPlusLinesChange; you don't need to callrefresh().
The following example uses CheckoutPlusProvider (components/CheckoutPlusProvider.tsx) in 4-1. Examples that connect to the SDK. CheckoutPlusProvider is an example that calls init(), refresh(), and destroy() according to the first two rules above, and you can also use it as is.
- It calls
init()when it is rendered. Whileenabledisfalse, it doesn't call it, and calls it whenenabledbecomestrue. - It calls
destroy()whenCheckoutPlusProvidergoes away and whenstoreId,locale, orenabledchanges. - It calls
refresh()once, right after it callsinit().
If you use CheckoutPlusProvider, don't call init() outside it. When your code changes the cart, call refresh() on the CheckoutPlusSession returned by useCheckoutPlus() after the code that changes the cart.
The following StoreCheckoutPlus is an example that passes CartConnector, the function that refetches the cart, and the store ID to CheckoutPlusProvider. Place it in the shared layout that every screen shows, inside your existing cart Provider, and wrap the whole page. This makes it call refresh() once even on screens without the banner. useCart() in the example is a hook that returns your cart state (the cart and a function to refetch it). Replace it with your existing cart React Context or state management library. In the Next.js App Router, use it inside a component marked with 'use client'. The files in 4. Sample code already include it.
cartConnector in the example is the CartConnector you implemented in step 2. Its name and file location (@/lib/checkout-plus-connector) are examples.
// `components/StoreCheckoutPlus.tsx` (React 19 or later example)
// Place it in the shared layout, inside your existing cart Provider, and wrap the whole page
'use client'
import type { ReactNode } from 'react'
import { CheckoutPlusProvider } from '@/components/CheckoutPlusProvider'
import { cartConnector } from '@/lib/checkout-plus-connector'
// Cart state (example; replace it with your existing cart React Context or state management library)
import { useCart } from '@/components/cart/cart-context'
export function StoreCheckoutPlus({
storeId,
children,
}: {
// This store's store ID
storeId: string
children: ReactNode
}) {
const { reloadCart } = useCart()
return (
<CheckoutPlusProvider
storeId={storeId}
// Your `CartConnector` (Step 2)
cart={cartConnector}
// Refetch the cart for the UI after the SDK adds or removes a Checkout+ product
reloadCart={reloadCart}
// Send to your monitoring (reportError is a placeholder for your logic)
onError={(error) => reportError(error)}
>
{children}
</CheckoutPlusProvider>
)
}Adapt the values passed to CheckoutPlusProvider (storeId, cart, reloadCart, and onError) to your settings and logic. reloadCart is called from onCheckoutPlusLinesChange of init(), and onError from onError of init().
- Which values passed to
init()are required, and what happens when they are omitted, is ininit(options). The example passesonErrorto send errors to your monitoring system. - How to pass the store ID is in Development Shopify store and store ID.
- The
kindnames delivered toonErrorare listed in the table inonError.
To use the display settings (displaySettings), add them to the props of CheckoutPlusProvider (for details, see displaySettings: Text and details dialog). cart, displaySettings, and initialSelection use the values passed when init() is called. CheckoutPlusProvider doesn't recreate the SDK when these values change, so if you load them from a CMS or similar, set enabled to false until loading finishes. The screens inside CheckoutPlusProvider render first, and init() is called once the values are ready.
<CheckoutPlusProvider
// … (`storeId`, `cart`, `reloadCart`, and `onError` from above)
displaySettings={{ title: 'あんしんプラス', description: '…' }} // Optional: banner text, etc. (`displaySettings` of `init`)
enabled={settingsLoaded} // Optional: if you load from a CMS or similar, false until loading finishes (settingsLoaded is a placeholder for your loading state)
>