Skip to main content
The useCheckoutSession hook provides access to the current checkout session data and functionality to update it. It gives you access to both the parsed, platform-agnostic checkout session data and the raw session data from the e-commerce platform.
Let your agent build it. The Ollie Shop skill ships with projects created by npx create-ollie-shop, so your assistant already knows the session shape and which fields are safe to read.
Prompt
Don’t have the skill yet? Install it.

Import

Usage

Return Value

Returns an object with:
  • session: CheckoutSession<Extensions> - The parsed, platform-agnostic checkout session conforming to your custom schema
  • rawSession: unknown - The original session data from the e-commerce platform (for example the VTEX orderForm) in raw/unparsed form
  • updateSession: (session: CheckoutSession<Extensions>, rawSession: unknown) => void - Function to update both the parsed and raw session data
  • sessionValidity?: ValidationSuccess | ValidationFailure - Results from the last validation check of the session data
  • sessionValidate: (session, options?: { ignorePayment?: boolean }) => ValidationSuccess | ValidationFailure - Validate any session object on demand, optionally skipping the payment rules
  • revalidate: () => void - Fetch the session again from the platform and update the context
  • revalidateAsync: () => Promise<...> - Same as revalidate, but returns a promise that settles when the fetch finishes
  • isFallback: boolean - true while the session is served from the last known copy; call revalidate to refresh
  • fallbackError?: string - The message that explains the fallback state
  • clearFallback: () => void - Clear the fallback state, for example after a successful revalidate
ValidationSuccess is { valid: true } and ValidationFailure is { valid: false, errors }, where errors follows the shape of the session (errors.shipping?.addresses, errors.customer?.email, and so on).

Session Validation

The sessionValidity object contains the results of validating the checkout session against a ZodSchema. This schema-based validation ensures that all required data is present and formatted correctly.

Understanding sessionValidity

  • sessionValidity uses a ZodSchema to validate the structure and content of your checkout session
  • It helps identify missing or invalid information in customer details, shipping addresses, payment methods, etc.
  • This validation is crucial for determining whether a user can proceed to checkout or needs to provide more information

Validating Address Information

A common use case is checking if some information is complete before redirecting the user to the next step
This pattern lets you detect missing required fields and direct users to the appropriate step in your checkout flow to provide that information.

Example

Notes

  • Use type parameters with useCheckoutSession<YourExtensions>() when you have custom properties in your checkout session
  • All amounts in session.totals and on cart items are in the currency’s minor units (cents). Format them with session.locale as in the example
  • To change the cart, use useCheckoutAction. updateSession replaces the context with a session you already have; it does not call the platform