Docs
STEP 2-5

Step 5: Replace the checkout button

Call prepareCheckout() at the start of the checkout button's handler, wait for it to complete, and then navigate to the current cart's checkoutUrl. Get checkoutUrl right before navigating, with your normal checkout start. Shopify also recommends requesting it when the shopper is ready to go to checkout, and requesting it again if it's stale (see Retrieve a checkout URL in Shopify's documentation). prepareCheckout() never throws and always completes (Exceptions and failures of prepareCheckout()), so you don't need to branch on success or failure in your checkout button handler.

components/CheckoutButton.tsx in 4-2. Examples that connect to your checkout is an example. You can replace the link to checkout (the link to the cart's checkoutUrl) with <CheckoutButton getCheckoutUrl={...} />. Pass your normal checkout start as getCheckoutUrl. The following is the core of what CheckoutButton does when it's pressed. It doesn't wait for initialization; if initialization hasn't finished, it proceeds without Checkout+.

// Example: the core of what `CheckoutButton` in the sample code (4-2) does when it's pressed
const checkoutPlus = useCheckoutPlus() // `null` before initialization finishes and when the SDK is unavailable (it doesn't wait)
async function handleClick() {
  await checkoutPlus?.prepareCheckout() // Never throws, and always completes
  const url = await getCheckoutUrl() // Your checkout start. `null` if it can't get the URL
  if (url !== null) window.location.assign(url)
}
  • If there's no cart, don't show the button.
  • If you place the checkout button inside a <form>, make it type="button" so that submitting the form doesn't navigate first.
  • From the moment the checkout button is pressed until navigation to checkout begins, block every checkout button and cart operations (including quantity changes and removals in the cart line list). When the shopper comes back from checkout with Back and the browser restores the previous page as is (when event.persisted of pageshow is true), restore the checkout buttons and cart operations you blocked.
  • If your code disables the checkout button for its own reasons (for example, while the cart is being updated), disable the button both while that condition applies and while checkout is being prepared.
  • If checkoutUrl can't be fetched, or navigation can't start, restore the checkout button so the shopper can press it again.
  • If the SDK user's server gets checkoutUrl, pass the shopper's IP in the Shopify-Storefront-Buyer-IP header so that a logged-in shopper stays logged in at checkout (see Retrieve a checkout URL in Shopify's documentation).

For what shoppers experience (for example, the purchase isn't blocked even if Checkout+ can't be added), see SDK states and transitions.

If you have multiple buttons that go to checkout (cart page, cart drawer, mini cart, and so on), call the same logic from all of them. Accelerated checkout buttons such as Shop Pay go to checkout without going through prepareCheckout(), so they don't add Checkout+.

Don't show the Checkout+ product in the cart line list

When the checkout button is pressed, the SDK adds the Checkout+ product to the cart. Identify this line with isCheckoutPlusLine(line), and don't show it in the cart line list. Don't include it in the item count either (including the cart count in the header). If you show cart.totalQuantity, subtract the quantity of the lines identified with isCheckoutPlusLine(). If the SDK is unavailable, treat every line as a regular line. The theme app embed doesn't show this line either.

  • The shapes of lines you can pass are in isCheckoutPlusLine(line).
  • Include merchandise.product.handle in the query you use to display the cart line list. This query is separate from CartConnector's query.

Use isCheckoutPlusLine() to identify the line. In the following example, checkoutPlus is the CheckoutPlusSession returned by init(), or null if the SDK is unavailable.

// Example: inside your cart line list component. With the sample code (4-1), you can get `checkoutPlus` from `useCheckoutPlus()`
// Don't show Checkout+ product lines in the cart line list, and exclude them from the item count (if the SDK is unavailable, treat them as regular lines)
const visibleLines = lines.filter((line) => !(checkoutPlus?.isCheckoutPlusLine(line) ?? false))
const count = visibleLines.reduce((sum, line) => sum + line.quantity, 0)

Next, check your Shopify store settings in Shopify store settings and before going live.

© Recustomer, Inc.