The package provides a set of React utilities to help you manage your ecommerce frontend. These include context providers, hooks, and components to handle carts, products, and payments.
The following hooks and components are available:
Hook / Component | Description |
|---|---|
| A context provider to wrap your application and provide the ecommerce context. |
| A hook to manage the cart state and actions. |
| A hook to fetch and manage addresses. |
| A hook to manage the checkout process. |
| A hook to format prices based on the selected currency. |
| A hook to access the ecommerce configuration (collection slugs, API settings). |
| A hook that encompasses all of the above in one. |
The EcommerceProvider component is used to wrap your application and provide the ecommerce context. It takes the following props:
Prop | Type | Description |
|---|---|---|
| | The slug of the addresses collection. Defaults to |
| | API configuration for the internal fetches of the provider. More |
| | The slug of the carts collection. Defaults to |
| | The child components that will have access to the ecommerce context. |
| | Configuration for supported currencies. See Currencies. |
| | The slug of the customers collection. Defaults to |
| | Enable or disable debug mode. This will send more information to the console. |
| | Enable or disable product variants support. Defaults to |
| | An array of payment method adapters for the client side. See Payment adapters. |
| | Whether to sync the cart ID to local storage. Defaults to |
Example usage:
The api prop is used to configure the API settings for the internal fetches of the provider. It takes an object with the following properties:
Property | Type | Description |
|---|---|---|
| | The base route for accessing the Payload API. Defaults to |
| | The full URL of your Payload server. |
| | Additional query parameters to include when fetching the cart. |
The cartsFetchQuery property allows you to specify additional query parameters to include when fetching the cart. This can be useful for including related data or customizing the response. This accepts:
Property | Type | Description |
|---|---|---|
| | Defaults to 0. See Depth |
| | Select parameters. See Select |
| | Populate parameters. See Populate |
Example usage:
The syncLocalStorage prop is used to enable or disable syncing the cart ID to local storage. This allows the cart to persist across page reloads and sessions. It defaults to true.
You can also provide an object with the following properties for more configuration:
Property | Type | Description |
|---|---|---|
| | The key to use for storing the cart ID in local storage. Defaults to |
The useCart hook is used to manage the cart state and actions. It provides methods to add, remove, and update items in the cart, as well as to fetch the current cart state. It has the following properties:
Property | Type | Description |
|---|---|---|
| | Method to add an item to the cart, optionally accepts a quantity to add multiple at once. |
| | The current cart state. Null or undefined if it doesn't exist. |
| | Method to clear the cart. |
| | Method to decrement the quantity of an item. Will remove it entirely if it reaches 0. |
| | Method to increment the quantity of an item. |
| | Boolean indicating if any async operation is in progress (e.g., adding/removing items). |
| | Method to remove an item from the cart. |
Example usage:
The useAddresses hook is used to fetch and manage addresses. It provides methods to create, update, and delete addresses, as well as to fetch the list of addresses. It has the following properties:
Property | Type | Description |
|---|---|---|
| | The list of addresses, if any are available for the current user. |
| | Method to create a new address. |
| | Boolean indicating if any async operation is in progress (e.g., creating/updating address). |
| | Method to update an existing address by ID. |
Example usage:
The usePayments hook is used to manage the checkout process. It provides methods to initiate payments, confirm orders, and handle payment status. It has the following properties:
Property | Type | Description |
|---|---|---|
| | Method to confirm an order by ID. More |
| | Method to initiate a payment for an order. More |
| | Boolean indicating if any async operation is in progress (e.g., initiating/confirming order). |
| | The list of available payment methods. |
| | The currently selected payment method, if any. |
Example usage:
Use this method to confirm an order by its ID. It requires the payment method ID and will return the order ID.
If the payment gateway requires additional confirmations offsite then you will need another landing page to handle that. For example with Stripe you may need to use a callback URL, just make sure the relevant information is routed back.
Use this method to initiate a payment for an order. It requires the cart ID and the payment method ID. Depending on the payment method, additional data may be required. Depending on the payment method used you may need to provide additional data, for example with Stripe:
This function will hit the Payload API endpoint for /stripe/initiate and return the payment data required to complete the payment on the client side, which by default will include a client_secret to complete the payment with Stripe.js. The next step is to call the confirmOrder once payment is confirmed on the client side by Stripe.
The useCurrency hook is used to format prices based on the selected currency. It provides methods to format prices and to get the current currency. It has the following properties:
Property | Type | Description |
|---|---|---|
| | The configuration for supported currencies. Directly matching the config provided to the Context Provider. More |
| | The currently selected currency. |
| | Method to format a price based on the selected currency. |
| | Method to set the current currency by code. It will update all price formats when used in conjunction with the |
formatPrice in particular is very helpful as all prices are stored as integers to avoid any potential issues with decimal calculations, therefore on the frontend you can use this utility to format your price accounting for the currency and decimals. Example usage:
The useEcommerceConfig hook provides access to the ecommerce configuration. This is useful when you need to build custom API calls or queries using the correct collection slugs and API settings.
Property | Type | Description |
|---|---|---|
| | The slug for the addresses collection. |
| | The slug for the carts collection. |
| | The slug for the customers collection. |
| | The base API route (e.g., |
Example usage:
The useEcommerce hook encompasses all of the above hooks in one. It provides access to the cart, addresses, and payments hooks, along with a unified isLoading state that tracks any async operations across all these features. It also includes the config property for accessing collection slugs and API settings.
Example usage:
The ecommerce provider exposes several hooks for handling authentication events, session management, and cart operations. These hooks are accessible via useEcommerce().
Called after a successful login to properly set up cart state. This hook handles:
Called during logout to clear all ecommerce session data. Currently this is just an alias for clearSession() but named for semantic clarity when used in logout flows and it could change in the future.
Clears all ecommerce session data from state and localStorage. This includes:
Use this when you need to reset the ecommerce state, such as during logout or when switching users.
Merges items from a source cart into a target cart. This is useful when you need to manually merge a guest cart into an authenticated user's cart, or when implementing custom cart merge logic.
Parameter | Type | Description |
|---|---|---|
| | The ID of the cart to merge items into |
| | The ID of the cart to merge items from |
| | The secret for the source cart (required for guest carts) |
When items are merged:
Fetches the latest cart data from the server and updates the local state. Use this when you need to ensure the cart is in sync with the server, such as after external cart modifications.
The provider includes built-in session management for handling user authentication flows. This ensures cart data, addresses, and user state are properly managed when users log in or out.
When a user logs in, call onLogin() to set up the ecommerce state. This automatically:
When a user logs out, call onLogout() or clearSession() to clear all ecommerce data:
Both methods clear cart data, cart ID, cart secret, addresses, and user state from memory and localStorage.