Advanced uses and examples
The plugin also exposes its internal utilities so that you can use only the parts that you need without using the entire plugin. This is useful if you want to build your own ecommerce solution on top of Payload.
Using only the collections
You can import the collections directly from the plugin and add them to your Payload configuration. This way, you can use the collections without using the entire plugin:
Name | Collection | Description |
---|---|---|
| | Used for customer addresses (like shipping and billing). More |
| | Carts can be used by customers, guests and once purchased are kept for records and analytics. More |
| | Orders are used to store customer-side information and are related to at least one transaction. More |
| | Handles payment information accessable by admins only, related to Orders. More |
| | All the product information lives here, contains prices, relations to Variant Types and joins to Variants. More |
| | Product variants, unique purchasable items that are linked to a product and Variant Options. More |
| | A taxonomy used by Products to relate Variant Options together. An example of a Variant Type is "size". More |
| | Related to a Variant Type to handle a unique property of it. An example of a Variant Option is "small". More |
createAddressesCollection
Use this to create the addresses
collection. This collection is used to store customer addresses. It takes the following properties:
Property | Type | Description |
---|---|---|
| | Access control for the collection. |
| | Custom fields to add to the address. |
| | (Optional) Slug of the customers collection. Defaults to |
| | (Optional) List of supported countries. Defaults to all countries. |
The access object can contain the following properties:
Property | Type | Description |
---|---|---|
| | Access control to check if the user has |
| | Access control to check if the user is authenticated. Use on the |
| | Field level access control to check if the user has |
See the access control section for more details on each of these functions.
Example usage:
createCartsCollection
Use this to create the carts
collection to store customer carts. It takes the following properties:
Property | Type | Description |
---|---|---|
| | Access control for the collection. |
| | (Optional) Slug of the customers collection. Defaults to |
| | (Optional) Slug of the products collection. Defaults to |
| | (Optional) Slug of the variants collection. Defaults to |
| | (Optional) Whether to enable variants in the cart. Defaults to |
| | (Optional) Currencies configuration to enable a subtotal to be tracked. |
The access object can contain the following properties:
Property | Type | Description |
---|---|---|
| | Access control to check if the user has |
| | Allow anyone to create a new cart, useful for guests. |
See the access control section for more details on each of these functions.
Example usage:
createOrdersCollection
Use this to create the orders
collection to store customer orders. It takes the following properties:
Property | Type | Description |
---|---|---|
| | Access control for the collection. |
| | (Optional) Slug of the customers collection. Defaults to |
| | (Optional) Slug of the transactions collection. Defaults to |
| | (Optional) Slug of the products collection. Defaults to |
| | (Optional) Slug of the variants collection. Defaults to |
| | (Optional) Whether to enable variants in the order. Defaults to |
| | (Optional) Currencies configuration to enable the amount to be tracked. |
| | (Optional) The fields to be used for the shipping address. |
The access object can contain the following properties:
Property | Type | Description |
---|---|---|
| | Access control to check if the user has |
| | Access control to check if the user has |
| | Field level access control to check if the user has |
See the access control section for more details on each of these functions.
Example usage:
createTransactionsCollection
Use this to create the transactions
collection to store payment transactions. It takes the following properties:
Property | Type | Description |
---|---|---|
| | Access control for the collection. |
| | (Optional) Slug of the customers collection. Defaults to |
| | (Optional) Slug of the carts collection. Defaults to |
| | (Optional) Slug of the orders collection. Defaults to |
| | (Optional) Slug of the products collection. Defaults to |
| | (Optional) Slug of the variants collection. Defaults to |
| | (Optional) Whether to enable variants in the transaction. Defaults to |
| | (Optional) Currencies configuration to enable the amount to be tracked. |
| | (Optional) The fields to be used for the billing address. |
| | (Optional) The payment methods to be used for the transaction. |
The access object can contain the following properties:
Property | Type | Description |
---|---|---|
| | Access control to check if the user has |
See the access control section for more details on each of these functions.
Example usage:
createProductsCollection
Use this to create the products
collection to store products. It takes the following properties:
Property | Type | Description |
---|---|---|
| | Access control for the collection. |
| | (Optional) Slug of the variants collection. Defaults to |
| | (Optional) Slug of the variant types collection. Defaults to |
| | (Optional) Whether to enable variants on products. Defaults to |
| | (Optional) Currencies configuration to enable price fields. |
| | (Optional) Inventory configuration to enable stock tracking. Defaults to |
The access object can contain the following properties:
Property | Type | Description |
---|---|---|
| | Access control to check if the user has |
| | Access control to check if the user has |
See the access control section for more details on each of these functions.
Example usage:
createVariantsCollection
Use this to create the variants
collection to store product variants. It takes the following properties:
Property | Type | Description |
---|---|---|
| | Access control for the collection. |
| | (Optional) Slug of the products collection. Defaults to |
| | (Optional) Slug of the variant options collection. Defaults to |
| | (Optional) Currencies configuration to enable price fields. |
| | (Optional) Inventory configuration to enable stock tracking. Defaults to |
The access object can contain the following properties:
Property | Type | Description |
---|---|---|
| | Access control to check if the user has |
| | Access control to check if the user has |
See the access control section for more details on each of these functions.
Example usage:
createVariantTypesCollection
Use this to create the variantTypes
collection to store variant types. It takes the following properties:
Property | Type | Description |
---|---|---|
| | Access control for the collection. |
| | (Optional) Slug of the variant options collection. Defaults to |
The access object can contain the following properties:
Property | Type | Description |
---|---|---|
| | Access control to check if the user has |
| | Allow anyone to read variant types. |
See the access control section for more details on each of these functions.
Example usage:
createVariantOptionsCollection
Use this to create the variantOptions
collection to store variant options. It takes the following properties:
Property | Type | Description |
---|---|---|
| | Access control for the collection. |
| | (Optional) Slug of the variant types collection. Defaults to |
The access object can contain the following properties:
Property | Type | Description |
---|---|---|
| | Access control to check if the user has |
| | Allow anyone to read variant options. |
See the access control section for more details on each of these functions.
Example usage:
Typescript
There are several common types that you'll come across when working with this package. These are export from the package as well and are used across individual utilities as well.
CurrenciesConfig
Defines the supported currencies in Payload and the frontend. It has the following properties:
Property | Type | Description |
---|---|---|
| | The default currency code. Must match one of the codes in the |
| | An array of supported currencies. Each currency must have a unique code. |
Currency
Defines a currency to be used in the application. It has the following properties:
Property | Type | Description |
---|---|---|
| | The ISO 4217 currency code. Example |
| | The symbol of the currency. Example |
| | The name of the currency. Example |
| | The number of decimal places to use. Example |
The decimals is very important to provide as we store all prices as integers to avoid floating point issues. For example, if you're using USD, you would store a price of $10.00 as 1000
(10 * 10^2), so when formatting the price for display we need to know how many decimal places the currency supports.
CountryType
Used to define a country in address fields and supported countries lists. It has the following properties:
Property | Type | Description |
---|---|---|
| | The ISO 3166-1 alpha-2 code. |
| | The name of the country. |
InventoryConfig
It's used to customise the inventory tracking settings on products and variants. It has the following properties:
Property | Type | Description |
---|---|---|
| | (Optional) The name of the field to use for tracking stock. Defaults to |