Shopify (headless): Sample code
This chapter contains the full text of example code for the integration. What the SDK needs is the method calls described in each step of the integration steps and in the Reference; you don't need to write the code or split the files the same way as this example. The examples are written in React (19 or later). With other frameworks, you call the same methods and follow the same rules.
The code is split into three pages. 4-1 and 4-2 are split by what the code connects to, and 4-3 has the type definitions.
- 4-1. Examples that connect to the SDK: examples that only call the SDK. They don't depend on your cart or checkout handling, so in some cases you can use them without changes
- 4-2. Examples that connect to your checkout: examples that include parts that depend on your checkout handling. Adapt them to your code
- 4-3. Type definitions (TypeScript): the TypeScript type definition file. Copy it into your project if you implement in TypeScript
Files
| File | Page | Step |
|---|---|---|
lib/checkout-plus.ts | 4-1 | Step 1: Load the SDK |
components/CheckoutPlusProvider.tsx | 4-1 | Step 3: Initialize the SDK |
components/CheckoutPlusBanner.tsx | 4-1 | Step 4: Show the banner |
components/CheckoutButton.tsx | 4-2 | Step 5: Replace the checkout button |
types/checkout-plus.d.ts | 4-3 | Step 1: Load the SDK (for TypeScript) |
The code you write for your cart is on the step pages: CartConnector is in Step 2: Implement CartConnector, StoreCheckoutPlus is in Step 3: Initialize the SDK, and leaving Checkout+ product lines out of the cart line list is in Don't show the Checkout+ product in the cart line list.