@commercengine/checkout helpers (initCheckout, getCheckout, subscribeToCheckout) directly in layout scripts or client modules.
NPM Package
@commercengine/checkout
Root checkout helpers for Astro. Import from
@commercengine/checkout (not a framework-specific subpath).Installation
Setup
CallinitCheckout once in your root layout script. Listen for astro:page-load so checkout re-initializes after ClientRouter page swaps:
src/layouts/Layout.astro
Feature flags, login methods, drawer direction, and payment provider are managed in Checkout Studio (Config API), not as SDK init options. Use
theme only for per-session overrides.Imperative API
Since there is no framework-specific hook, usegetCheckout() and subscribeToCheckout() for imperative control:
src/components/CartButton.astro
Add to cart
src/components/AddToCartButton.astro
Framework islands
If you use React, Vue, Svelte, or Solid islands inside Astro, you can use the corresponding framework binding’s hook/composable/store inside the island. The checkout instance is shared —initCheckout only needs to be called once in the layout.
src/components/CartButton.tsx
src/pages/index.astro
Storefront SDK integration
If your Astro app also uses the@commercengine/storefront SDK, you must use authMode: "provided" with two-way token sync. Create a client module that owns both the storefront session and checkout init:
src/lib/storefront-client.ts
src/layouts/Layout.astro
Preserving the iframe across ClientRouter navigations
Astro’sClientRouter replaces document.body on each navigation, which remounts any body-mounted checkout iframe. If remounting is acceptable, no extra work is needed.
If you need to keep the iframe alive across page transitions, install a custom swap that only replaces your page content region instead of the full body:
1
Wrap page content in a data attribute
src/layouts/Layout.astro
2
Add a custom swap handler
src/layouts/Layout.astro
[data-page-content] region, leaving the checkout iframe (and any other body-level elements) untouched.
API reference
Since Astro uses the root helpers, the imperative API is slightly different from the framework-specific hooks:getCheckout() methods:
subscribeToCheckout state properties: