Ecommerce Frontend
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 products. |
| A hook to manage the checkout process. |
| A hook to format prices based on the selected currency. |
| A hook that encompasses all of the above in one. |
EcommerceProvider
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:
api
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. |
cartsFetchQuery
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:
syncLocalStorage
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 |
useCart
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. |
| | Method to remove an item from the cart. |
Example usage:
useAddresses
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. |
| | Method to update an existing address by ID. |
Example usage:
usePayments
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 |
| | The list of available payment methods. |
| | The currently selected payment method, if any. |
Example usage:
confirmOrder
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.
initiatePayment
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.
useCurrency
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:
useEcommerce
The useEcommerce
hook encompasses all of the above hooks in one. It provides access to the cart, addresses, and payments hooks.
Example usage: