useCheckoutAction hook enables interactions with checkout actions in a type-safe manner, automatically updating the checkout session upon successful execution. The exceptions are REQUEST, SIMULATE_SESSION and CREATE_ORDER, whose results are not a session and leave it untouched.
Action Types
The hook supports various action types including:ADD_ITEMS- Add items to the cartREMOVE_ITEMS- Remove items from the cartUPDATE_COUPONS- Update coupon codesUPDATE_CUSTOMER_DETAILS- Update customer informationUPDATE_ITEMS_QUANTITY- Update quantities of items in the cartUPDATE_SHIPPING_PACKAGES- Update shipping packagesUPDATE_SHIPPING_ADDRESSES- Update shipping addressesUPDATE_PAYMENT_METHODS- Update payment methodsUPDATE_GIFT_CARDS- Apply or update gift cardsUPDATE_CUSTOMER_PREFERENCES- Update customer preferences (e.g. save data, locale)SIMULATE_SESSION- Simulate a checkout session without mutating the real oneCREATE_ORDER- Create a new orderCREATE_NEW_SESSION- Discard the current cart and start a new sessionREQUEST- Make custom API requests to the backend
Parameters
actionType: ActionType- The type of checkout action to perform (e.g.,ADD_ITEMS,REMOVE_ITEMS, etc.)callback?: object- Optional configuration callbacksonSuccess?: (data?: CheckoutSession, input?: Input) => void- Called when the action succeeds. Receives the updated session and the input that was executed. ForCREATE_ORDER,datais the created order; cast it, since the generic type isCheckoutSession. ForREQUESTit is the whole{ data, raw }result of your request, typed asunknownonError?: ({ serverError, validationErrors }) => void- Called when action fails
Return Value
Returns an object with:execute: (input: Input) => void- Function to dispatch the action with the appropriate payloadexecuteAsync: (input: Input) => Promise<Result | undefined>- Async version of execute. The promise settles when the request finishes and resolves with the raw action result (see Using executeAsync). It resolves, rather than rejects, on server errorsisPending: boolean- Boolean indicating if the action is currently processingerror?: object- Error information if the action failed. Check it right after a failed call; it is not reset by a later successserverError?: ServerError- Error returned by the servervalidationErrors?: ValidatorErrors<Input>- Input Validation errors
Actions other than
CREATE_ORDER are retried on transient network errors before onError fires.Input Parameters by Action Type
Each action type expects specific input parameters. Below are the details for each:- ADD_ITEMS
- REMOVE_ITEMS
- UPDATE_COUPONS
- UPDATE_CUSTOMER_DETAILS
- UPDATE_ITEMS_QUANTITY
- UPDATE_SHIPPING_PACKAGES
- UPDATE_SHIPPING_ADDRESSES
- UPDATE_PAYMENT_METHODS
- UPDATE_GIFT_CARDS
- UPDATE_CUSTOMER_PREFERENCES
- SIMULATE_SESSION
- CREATE_ORDER
- CREATE_NEW_SESSION
- REQUEST
Example
Import
Usage
Action
Using executeAsync
executeAsync is useful when you need to wait for an action before doing something else. It resolves with the raw action result, typed as unknown, so narrow it before reading it. The result is { data?: { data, raw }, serverError?, validationErrors? }, where data.data is the updated session and data.raw is the platform’s response.
The promise resolves as soon as the request finishes. The session in
useCheckoutSession is updated right after, so prefer onSuccess or the session itself when you only need the new state.