1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452---
title: Advanced uses and examples
label: Advanced
order: 60
desc: Add ecommerce functionality to your Payload CMS application with this plugin.
keywords: plugins, ecommerce, stripe, plugin, payload, cms, shop, payments
---
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](#createAddressesCollection) |
| `createCartsCollection` | `carts` | Carts can be used by customers, guests and once purchased are kept for records and analytics. [More](#createCartsCollection) |
| `createOrdersCollection` | `orders` | Orders are used to store customer-side information and are related to at least one transaction. [More](#createOrdersCollection) |
| `createTransactionsCollection` | `transactions` | Handles payment information accessible by admins only, related to Orders. [More](#createTransactionsCollection) |
| `createProductsCollection` | `products` | All the product information lives here, contains prices, relations to Variant Types and joins to Variants. [More](#createProductsCollection) |
| `createVariantsCollection` | `variants` | Product variants, unique purchasable items that are linked to a product and Variant Options. [More](#createVariantsCollection) |
| `createVariantTypesCollection` | `variantTypes` | A taxonomy used by Products to relate Variant Options together. An example of a Variant Type is "size". [More](#createVariantTypesCollection) |
| `createVariantOptionsCollection` | `variantOptions` | Related to a Variant Type to handle a unique property of it. An example of a Variant Option is "small". [More](#createVariantOptionsCollection) |
### 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 |
| ----------------- | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isAdmin` | `Access` | Access control to check if the user has `admin` permissions. |
| `isAuthenticated` | `Access` | Access control to check if the user is authenticated. Use on the `create` access to allow any customer to create a new address. |
| `isCustomer` | `FieldAccess` | Checks if the user is a customer (authenticated but not admin). Used to auto-assign customer ID when creating addresses. |
| `isDocumentOwner` | `Access` | Access control to check if the user owns the document via the `customer` field. Used to limit read, update or delete to only the customers that own this address. |
See the [access control section](./plugin#access) for more details on each of these functions.
Example usage:
```ts
import { createAddressesCollection } from 'payload-plugin-ecommerce'
const Addresses = createAddressesCollection({
access: {
isAdmin,
isAuthenticated,
isCustomer,
isDocumentOwner,
},
addressFields: [
{
name: 'company',
type: 'text',
label: 'Company',
},
],
})
```
### 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 |
| ----------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isAdmin` | `Access` | Access control to check if the user has `admin` permissions. |
| `isAuthenticated` | `Access` | Access control to check if the user is authenticated. |
| `isDocumentOwner` | `Access` | Access control to check if the user owns the document via the `customer` field. Used to limit read, update or delete to only the customers that own this cart. |
| `publicAccess` | `Access` | (Optional) Allow anyone to create a new cart, useful for guests. |
See the [access control section](./plugin#access) for more details on each of these functions.
Example usage:
```ts
import { createCartsCollection } from 'payload-plugin-ecommerce'
const Carts = createCartsCollection({
access: {
isAdmin,
isAuthenticated,
isDocumentOwner,
},
enableVariants: true,
currenciesConfig: {
defaultCurrency: 'usd',
currencies: [
{
code: 'usd',
symbol: '$',
},
{
code: 'eur',
symbol: '€',
},
],
},
})
```
### 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 |
| ---------------------- | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `isAdmin` | `Access` | Access control to check if the user has `admin` permissions. Used to limit create, update and delete access to only admins. |
| `isDocumentOwner` | `Access` | Access control to check if the user owns the document via the `customer` field. Used to limit read to only the customers that own this order. |
| `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](./plugin#access) for more details on each of these functions.
Example usage:
```ts
import { createOrdersCollection } from 'payload-plugin-ecommerce'
const Orders = createOrdersCollection({
access: {
isAdmin,
isDocumentOwner,
adminOnlyFieldAccess,
},
enableVariants: true,
currenciesConfig: {
defaultCurrency: 'usd',
currencies: [
{
code: 'usd',
symbol: '$',
},
{
code: 'eur',
symbol: '€',
},
],
},
addressFields: [
{
name: 'deliveryInstructions',
type: 'text',
label: 'Delivery Instructions',
},
],
})
```
### 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 |
| --------- | -------- | ----------------------------------------------------------------------------------------------------- |
| `isAdmin` | `Access` | Access control to check if the user has `admin` permissions. Used to limit all access to only admins. |
See the [access control section](./plugin#access) for more details on each of these functions.
Example usage:
```ts
import { createTransactionsCollection } from 'payload-plugin-ecommerce'
const Transactions = createTransactionsCollection({
access: {
isAdmin,
},
enableVariants: true,
currenciesConfig: {
defaultCurrency: 'usd',
currencies: [
{
code: 'usd',
symbol: '$',
},
{
code: 'eur',
symbol: '€',
},
],
},
addressFields: [
{
name: 'billingInstructions',
type: 'text',
label: 'Billing Instructions',
},
],
paymentMethods: [
// Add your payment adapters here
],
})
```
### 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 |
| ------------------------ | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isAdmin` | `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](./plugin#access) for more details on each of these functions.
Example usage:
```ts
import { createProductsCollection } from 'payload-plugin-ecommerce'
const Products = createProductsCollection({
access: {
isAdmin,
adminOrPublishedStatus,
},
enableVariants: true,
currenciesConfig: {
defaultCurrency: 'usd',
currencies: [
{
code: 'usd',
symbol: '$',
},
{
code: 'eur',
symbol: '€',
},
],
},
inventory: {
enabled: true,
trackByVariant: true,
lowStockThreshold: 5,
},
})
```
### 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 |
| ------------------------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isAdmin` | `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](./plugin#access) for more details on each of these functions.
Example usage:
```ts
import { createVariantsCollection } from 'payload-plugin-ecommerce'
const Variants = createVariantsCollection({
access: {
isAdmin,
adminOrPublishedStatus,
},
currenciesConfig: {
defaultCurrency: 'usd',
currencies: [
{
code: 'usd',
symbol: '$',
},
{
code: 'eur',
symbol: '€',
},
],
},
inventory: {
enabled: true,
lowStockThreshold: 5,
},
})
```
### 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 |
| -------------- | -------- | ----------------------------------------------------------------------------------------------------- |
| `isAdmin` | `Access` | Access control to check if the user has `admin` permissions. Used to limit all access to only admins. |
| `publicAccess` | `Access` | (Optional) Allow anyone to read variant types. |
See the [access control section](./plugin#access) for more details on each of these functions.
Example usage:
```ts
import { createVariantTypesCollection } from 'payload-plugin-ecommerce'
const VariantTypes = createVariantTypesCollection({
access: {
isAdmin,
publicAccess,
},
})
```
### 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 |
| -------------- | -------- | ----------------------------------------------------------------------------------------------------- |
| `isAdmin` | `Access` | Access control to check if the user has `admin` permissions. Used to limit all access to only admins. |
| `publicAccess` | `Access` | (Optional) Allow anyone to read variant options. |
See the [access control section](./plugin#access) for more details on each of these functions.
Example usage:
```ts
import { createVariantOptionsCollection } from 'payload-plugin-ecommerce'
const VariantOptions = createVariantOptionsCollection({
access: {
isAdmin,
publicAccess,
},
})
```
## 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`. |