Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

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

createAddressesCollection

addresses

Used for customer addresses (like shipping and billing). More

createCartsCollection

carts

Carts can be used by customers, guests and once purchased are kept for records and analytics. More

createOrdersCollection

orders

Orders are used to store customer-side information and are related to at least one transaction. More

createTransactionsCollection

transactions

Handles payment information accessable by admins only, related to Orders. More

createProductsCollection

products

All the product information lives here, contains prices, relations to Variant Types and joins to Variants. More

createVariantsCollection

variants

Product variants, unique purchasable items that are linked to a product and Variant Options. More

createVariantTypesCollection

variantTypes

A taxonomy used by Products to relate Variant Options together. An example of a Variant Type is "size". More

createVariantOptionsCollection

variantOptions

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

object

Access control for the collection.

addressFields

Field[]

Custom fields to add to the address.

customersSlug

string

(Optional) Slug of the customers collection. Defaults to customers.

supportedCountries

CountryType[]

(Optional) List of supported countries. Defaults to all countries.

The access object can contain the following properties:

Property

Type

Description

adminOrCustomerOwner

Access

Access control to check if the user has admin permissions or is the owner of the document via the customer field. Used to limit read, update or delete to only the customers that own this address.

authenticatedOnly

Access

Access control to check if the user is authenticated. Use on the create access to allow any customer to create a new address.

customerOnlyFieldAccess

FieldAccess

Field level access control to check if the user has customer permissions.

See the access control section for more details on each of these functions.

Example usage:

1
import { createAddressesCollection } from 'payload-plugin-ecommerce'
2
3
const Addresses = createAddressesCollection({
4
access: {
5
adminOrCustomerOwner,
6
authenticatedOnly,
7
customerOnlyFieldAccess,
8
},
9
addressFields: [
10
{
11
name: 'company',
12
type: 'text',
13
label: 'Company',
14
},
15
],
16
})

createCartsCollection

Use this to create the carts collection to store customer carts. It takes the following properties:

Property

Type

Description

access

object

Access control for the collection.

customersSlug

string

(Optional) Slug of the customers collection. Defaults to customers.

productsSlug

string

(Optional) Slug of the products collection. Defaults to products.

variantsSlug

string

(Optional) Slug of the variants collection. Defaults to variants.

enableVariants

boolean

(Optional) Whether to enable variants in the cart. Defaults to true.

currenciesConfig

CurrenciesConfig

(Optional) Currencies configuration to enable a subtotal to be tracked.

The access object can contain the following properties:

Property

Type

Description

adminOrCustomerOwner

Access

Access control to check if the user has admin permissions or is the owner of the document via the customer field. Used to limit read, update or delete to only the customers that own this cart.

publicAccess

Access

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:

1
import { createCartsCollection } from 'payload-plugin-ecommerce'
2
3
const Carts = createCartsCollection({
4
access: {
5
adminOrCustomerOwner,
6
publicAccess,
7
},
8
enableVariants: true,
9
currenciesConfig: {
10
defaultCurrency: 'usd',
11
currencies: [
12
{
13
code: 'usd',
14
symbol: '$',
15
},
16
{
17
code: 'eur',
18
symbol: '€',
19
},
20
],
21
},
22
})

createOrdersCollection

Use this to create the orders collection to store customer orders. It takes the following properties:

Property

Type

Description

access

object

Access control for the collection.

customersSlug

string

(Optional) Slug of the customers collection. Defaults to customers.

transactionsSlug

string

(Optional) Slug of the transactions collection. Defaults to transactions.

productsSlug

string

(Optional) Slug of the products collection. Defaults to products.

variantsSlug

string

(Optional) Slug of the variants collection. Defaults to variants.

enableVariants

boolean

(Optional) Whether to enable variants in the order. Defaults to true.

currenciesConfig

CurrenciesConfig

(Optional) Currencies configuration to enable the amount to be tracked.

addressFields

Field[]

(Optional) The fields to be used for the shipping address.

The access object can contain the following properties:

Property

Type

Description

adminOrCustomerOwner

Access

Access control to check if the user has admin permissions or is the owner of the document via the customer field. Used to limit read to only the customers that own this order.

adminOnly

Access

Access control to check if the user has admin permissions. Used to limit create, update and delete access to only admins.

adminOnlyFieldAccess

FieldAccess

Field level access control to check if the user has admin permissions. Limits the transaction ID field to admins only.

See the access control section for more details on each of these functions.

Example usage:

1
import { createOrdersCollection } from 'payload-plugin-ecommerce'
2
3
const Orders = createOrdersCollection({
4
access: {
5
adminOrCustomerOwner,
6
adminOnly,
7
adminOnlyFieldAccess,
8
},
9
enableVariants: true,
10
currenciesConfig: {
11
defaultCurrency: 'usd',
12
currencies: [
13
{
14
code: 'usd',
15
symbol: '$',
16
},
17
{
18
code: 'eur',
19
symbol: '€',
20
},
21
],
22
},
23
addressFields: [
24
{
25
name: 'deliveryInstructions',
26
type: 'text',
27
label: 'Delivery Instructions',
28
},
29
],
30
})

createTransactionsCollection

Use this to create the transactions collection to store payment transactions. It takes the following properties:

Property

Type

Description

access

object

Access control for the collection.

customersSlug

string

(Optional) Slug of the customers collection. Defaults to customers.

cartsSlug

string

(Optional) Slug of the carts collection. Defaults to carts.

ordersSlug

string

(Optional) Slug of the orders collection. Defaults to orders.

productsSlug

string

(Optional) Slug of the products collection. Defaults to products.

variantsSlug

string

(Optional) Slug of the variants collection. Defaults to variants.

enableVariants

boolean

(Optional) Whether to enable variants in the transaction. Defaults to true.

currenciesConfig

CurrenciesConfig

(Optional) Currencies configuration to enable the amount to be tracked.

addressFields

Field[]

(Optional) The fields to be used for the billing address.

paymentMethods

PaymentAdapter[]

(Optional) The payment methods to be used for the transaction.

The access object can contain the following properties:

Property

Type

Description

adminOnly

Access

Access control to check if the user has admin permissions. Used to limit all access to only admins.

See the access control section for more details on each of these functions.

Example usage:

1
import { createTransactionsCollection } from 'payload-plugin-ecommerce'
2
3
const Transactions = createTransactionsCollection({
4
access: {
5
adminOnly,
6
},
7
enableVariants: true,
8
currenciesConfig: {
9
defaultCurrency: 'usd',
10
currencies: [
11
{
12
code: 'usd',
13
symbol: '$',
14
},
15
{
16
code: 'eur',
17
symbol: '€',
18
},
19
],
20
},
21
addressFields: [
22
{
23
name: 'billingInstructions',
24
type: 'text',
25
label: 'Billing Instructions',
26
},
27
],
28
paymentMethods: [
29
// Add your payment adapters here
30
],
31
})

createProductsCollection

Use this to create the products collection to store products. It takes the following properties:

Property

Type

Description

access

object

Access control for the collection.

variantsSlug

string

(Optional) Slug of the variants collection. Defaults to variants.

variantTypesSlug

string

(Optional) Slug of the variant types collection. Defaults to variantTypes.

enableVariants

boolean

(Optional) Whether to enable variants on products. Defaults to true.

currenciesConfig

CurrenciesConfig

(Optional) Currencies configuration to enable price fields.

inventory

boolean InventoryConfig

(Optional) Inventory configuration to enable stock tracking. Defaults to true.

The access object can contain the following properties:

Property

Type

Description

adminOnly

Access

Access control to check if the user has admin permissions. Used to limit create, update or delete to only admins.

adminOrPublishedStatus

Access

Access control to check if the user has admin permissions or if the product has a published status. Used to limit read access to published products for non-admins.

See the access control section for more details on each of these functions.

Example usage:

1
import { createProductsCollection } from 'payload-plugin-ecommerce'
2
3
const Products = createProductsCollection({
4
access: {
5
adminOnly,
6
adminOrPublishedStatus,
7
},
8
enableVariants: true,
9
currenciesConfig: {
10
defaultCurrency: 'usd',
11
currencies: [
12
{
13
code: 'usd',
14
symbol: '$',
15
},
16
{
17
code: 'eur',
18
symbol: '€',
19
},
20
],
21
},
22
inventory: {
23
enabled: true,
24
trackByVariant: true,
25
lowStockThreshold: 5,
26
},
27
})

createVariantsCollection

Use this to create the variants collection to store product variants. It takes the following properties:

Property

Type

Description

access

object

Access control for the collection.

productsSlug

string

(Optional) Slug of the products collection. Defaults to products.

variantOptionsSlug

string

(Optional) Slug of the variant options collection. Defaults to variantOptions.

currenciesConfig

CurrenciesConfig

(Optional) Currencies configuration to enable price fields.

inventory

boolean InventoryConfig

(Optional) Inventory configuration to enable stock tracking. Defaults to true.

The access object can contain the following properties:

Property

Type

Description

adminOnly

Access

Access control to check if the user has admin permissions. Used to limit all access to only admins.

adminOrPublishedStatus

Access

Access control to check if the user has admin permissions or if the related product has a published status. Used to limit read access to variants of published products for non-admins.

See the access control section for more details on each of these functions.

Example usage:

1
import { createVariantsCollection } from 'payload-plugin-ecommerce'
2
3
const Variants = createVariantsCollection({
4
access: {
5
adminOnly,
6
adminOrPublishedStatus,
7
},
8
currenciesConfig: {
9
defaultCurrency: 'usd',
10
currencies: [
11
{
12
code: 'usd',
13
symbol: '$',
14
},
15
{
16
code: 'eur',
17
symbol: '€',
18
},
19
],
20
},
21
inventory: {
22
enabled: true,
23
lowStockThreshold: 5,
24
},
25
})

createVariantTypesCollection

Use this to create the variantTypes collection to store variant types. It takes the following properties:

Property

Type

Description

access

object

Access control for the collection.

variantOptionsSlug

string

(Optional) Slug of the variant options collection. Defaults to variantOptions.

The access object can contain the following properties:

Property

Type

Description

adminOnly

Access

Access control to check if the user has admin permissions. Used to limit all access to only admins.

publicAccess

Access

Allow anyone to read variant types.

See the access control section for more details on each of these functions.

Example usage:

1
import { createVariantTypesCollection } from 'payload-plugin-ecommerce'
2
3
const VariantTypes = createVariantTypesCollection({
4
access: {
5
adminOnly,
6
publicAccess,
7
},
8
})

createVariantOptionsCollection

Use this to create the variantOptions collection to store variant options. It takes the following properties:

Property

Type

Description

access

object

Access control for the collection.

variantTypesSlug

string

(Optional) Slug of the variant types collection. Defaults to variantTypes.

The access object can contain the following properties:

Property

Type

Description

adminOnly

Access

Access control to check if the user has admin permissions. Used to limit all access to only admins.

publicAccess

Access

Allow anyone to read variant options.

See the access control section for more details on each of these functions.

Example usage:

1
import { createVariantOptionsCollection } from 'payload-plugin-ecommerce'
2
3
const VariantOptions = createVariantOptionsCollection({
4
access: {
5
adminOnly,
6
publicAccess,
7
},
8
})

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

defaultCurrency

string

The default currency code. Must match one of the codes in the currencies array.

currencies

CurrencyType[]

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

code

string

The ISO 4217 currency code. Example 'usd'.

symbol

string

The symbol of the currency. Example '$'

label

string

The name of the currency. Example 'USD'

decimals

number

The number of decimal places to use. Example 2

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

value

string

The ISO 3166-1 alpha-2 code.

label

string

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

fieldName

string

(Optional) The name of the field to use for tracking stock. Defaults to inventory.

Next

Examples