Functions that allow an Eitri-App to integrate with Shopify Checkout.

Shopify Checkout provides a native, optimized checkout experience using Shopify's Checkout Sheet Kit. This allows customers to complete their purchase without leaving the app, providing a seamless and secure checkout flow.

Important Notes:

  • The checkout URL must be obtained from the Shopify Storefront API
  • Only one checkout can be presented at a time
  • Use preload to improve checkout presentation speed

Methods

  • Presents the Shopify checkout sheet for the given checkout URL.

    Opens a native checkout sheet that handles the entire purchase flow, including payment processing, shipping selection, and order confirmation.

    Important:

    • Only one checkout can be active at a time
    • If a checkout is already in progress, throws shopify.checkout.alreadyInProgress
    • If the user cancels, throws shopify.checkout.abandoned
    • If checkout fails, throws shopify.checkout.failed

    Example:

    const modules = await Eitri.modules()
    const shopifyStartCheckout = modules.shopify?.startCheckout
    if (!shopifyStartCheckout) return

    try {
    const result = await shopifyStartCheckout({
    checkoutUrl: "https://your-store.myshopify.com/checkouts/cn/Z2NwLXVzLWVh...",
    colorScheme: "automatic" // optional: "automatic" | "light" | "dark"
    })

    // Access the order ID
    console.log("Order completed! Order ID:", result.orderDetails.id)

    // Access customer information
    console.log("Customer email:", result.orderDetails.email)

    // Access cart details
    const { cart } = result.orderDetails
    console.log("Total:", cart.price.total?.amount, cart.price.total?.currencyCode)

    // Access line items
    cart.lines.forEach(line => {
    console.log(`${line.title} x ${line.quantity}`)
    })

    // Navigate to order confirmation screen
    } catch (error) {
    if (error.message?.includes("abandoned")) {
    console.log("User cancelled checkout")
    } else {
    console.error("Checkout failed:", error)
    }
    }

    Parameters

    Returns Promise<ShopifyCheckoutResult>

    Promise resolving to ShopifyCheckoutResult with full order details

    exceptions when checkout cannot be completed

  • Preloads a Shopify checkout URL for faster presentation.

    Call this method when you anticipate the user will proceed to checkout soon (e.g., when viewing the cart). The checkout sheet will load in the background, resulting in a faster presentation when startCheckout is called.

    Best Practices:

    • Call when the user views their cart
    • Call when the checkout URL changes (e.g., cart items updated)
    • Don't preload too early as the checkout URL may become stale

    Example:

    const modules = await Eitri.modules()
    const shopifyPreload = modules.shopify?.preload
    if (!shopifyPreload) return

    // Preload when user views cart
    shopifyPreload({
    checkoutUrl: "https://your-store.myshopify.com/checkouts/cn/Z2NwLXVzLWVh..."
    })

    Parameters

    Returns Promise<void>

    shopify.checkout.invalidUrl if the checkout URL is invalid

  • Invalidates any preloaded checkout data.

    Call this method when the preloaded checkout is no longer valid, such as when the cart contents change significantly or when the user logs out.

    When to call:

    • User clears their cart
    • User logs out
    • Cart contents change and a new checkout URL is generated

    Example:

    const modules = await Eitri.modules()
    const shopifyInvalidate = modules.shopify?.invalidate
    if (!shopifyInvalidate) return

    // Invalidate when cart is cleared
    async function clearCart() {
    await cartService.clear()
    shopifyInvalidate()
    }

    Returns Promise<void>