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.
Import
Usage
Return Value
Returns an object with:session: CheckoutSession<Extensions>- The parsed, platform-agnostic checkout session conforming to your custom schemarawSession: unknown- The original session data from the e-commerce platform (for example the VTEX orderForm) in raw/unparsed formupdateSession: (session: CheckoutSession<Extensions>, rawSession: unknown) => void- Function to update both the parsed and raw session datasessionValidity?: ValidationSuccess | ValidationFailure- Results from the last validation check of the session datasessionValidate: (session, options?: { ignorePayment?: boolean }) => ValidationSuccess | ValidationFailure- Validate any session object on demand, optionally skipping the payment rulesrevalidate: () => void- Fetch the session again from the platform and update the contextrevalidateAsync: () => Promise<...>- Same asrevalidate, but returns a promise that settles when the fetch finishesisFallback: boolean-truewhile the session is served from the last known copy; callrevalidateto refreshfallbackError?: string- The message that explains the fallback stateclearFallback: () => void- Clear the fallback state, for example after a successfulrevalidate
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
ThesessionValidity 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
sessionValidityuses 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 stepExample
Notes
- Use type parameters with
useCheckoutSession<YourExtensions>()when you have custom properties in your checkout session - All amounts in
session.totalsand on cart items are in the currency’s minor units (cents). Format them withsession.localeas in the example - To change the cart, use
useCheckoutAction.updateSessionreplaces the context with a session you already have; it does not call the platform