Skip to main content
The 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.
Let your agent build it. The Ollie Shop skill ships with projects created by npx create-ollie-shop, so your assistant already knows every action type below and the input each one expects.
Prompt
Don’t have the skill yet? Install it.

Action Types

The hook supports various action types including:
  • ADD_ITEMS - Add items to the cart
  • REMOVE_ITEMS - Remove items from the cart
  • UPDATE_COUPONS - Update coupon codes
  • UPDATE_CUSTOMER_DETAILS - Update customer information
  • UPDATE_ITEMS_QUANTITY - Update quantities of items in the cart
  • UPDATE_SHIPPING_PACKAGES - Update shipping packages
  • UPDATE_SHIPPING_ADDRESSES - Update shipping addresses
  • UPDATE_PAYMENT_METHODS - Update payment methods
  • UPDATE_GIFT_CARDS - Apply or update gift cards
  • UPDATE_CUSTOMER_PREFERENCES - Update customer preferences (e.g. save data, locale)
  • SIMULATE_SESSION - Simulate a checkout session without mutating the real one
  • CREATE_ORDER - Create a new order
  • CREATE_NEW_SESSION - Discard the current cart and start a new session
  • REQUEST - 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 callbacks
    • onSuccess?: (data?: CheckoutSession, input?: Input) => void - Called when the action succeeds. Receives the updated session and the input that was executed. For CREATE_ORDER, data is the created order; cast it, since the generic type is CheckoutSession. For REQUEST it is the whole { data, raw } result of your request, typed as unknown
    • onError?: ({ serverError, validationErrors }) => void - Called when action fails
Keep callbacks stable. Treat onSuccess and onError as fixed for the component’s lifetime: props or state you read inside them may be the values from the first render. Read changing values through a ref, or react to the result where it is already fresh: the session from useCheckoutSession after a success, or the error return value after a failure.

Return Value

Returns an object with:
  • execute: (input: Input) => void - Function to dispatch the action with the appropriate payload
  • executeAsync: (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 errors
  • isPending: boolean - Boolean indicating if the action is currently processing
  • error?: object - Error information if the action failed. Check it right after a failed call; it is not reset by a later success
    • serverError?: ServerError - Error returned by the server
    • validationErrors?: 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:
Navigate the tabs to see details of each action

ADD_ITEMS

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.