17 KiB
Payload Ecommerce Template
This template is in BETA.
This is the official Payload Ecommerce Template. This repo includes a fully-working backend, enterprise-grade admin panel, and a beautifully designed, production-ready ecommerce website.
This template is right for you if you are working on building an ecommerce project or shop with Payload.
Core features:
- Pre-configured Payload Config
- Authentication
- Access Control
- Layout Builder
- Draft Preview
- Live Preview
- On-demand Revalidation
- SEO
- Search & Filters
- Jobs and Scheduled Publishing
- Website
- Products & Variants
- User accounts
- Carts
- Guest checkout
- Orders & Transactions
- Stripe Payments
- Currencies
- Automated Tests
Quick Start
To spin up this example locally, follow these steps:
Clone
If you have not done so already, you need to have standalone copy of this repo on your machine. If you've already cloned this repo, skip to Development.
Method 1
Use the create-payload-app CLI to clone this template directly to your machine:
pnpx create-payload-app my-project -t ecommerce
Method 2
Use the git CLI to clone this template directly to your machine:
git clone -n --depth=1 --filter=tree:0 https://github.com/payloadcms/payload my-project && cd my-project && git sparse-checkout set --no-cone templates/ecommerce && git checkout && rm -rf .git && git init && git add . && git mv -f templates/ecommerce/{.,}* . && git add . && git commit -m "Initial commit"
Development
- First clone the repo if you have not done so already
cd my-project && cp .env.example .envto copy the example environment variablespnpm install && pnpm devto install dependencies and start the dev server- open
http://localhost:3000to open the app in your browser
That's it! Changes made in ./src will be reflected in your app. Follow the on-screen instructions to login and create your first admin user. Then check out Production once you're ready to build and serve your app, and Deployment when you're ready to go live.
How it works
The Payload config is tailored specifically to the needs of most websites. It is pre-configured in the following ways:
Collections
See the Collections docs for details on how to extend this functionality.
-
Users (Authentication)
Users are auth-enabled collections that have access to the admin panel and unpublished content. See Access Control for more details.
For additional help, see the official Auth Example or the Authentication docs.
-
Pages
All pages are layout builder enabled so you can generate unique layouts for each page using layout-building blocks, see Layout Builder for more details. Pages are also draft-enabled so you can preview them before publishing them to your website, see Draft Preview for more details.
-
Media
This is the uploads enabled collection used by pages, posts, and projects to contain media like images, videos, downloads, and other assets. It features pre-configured sizes, focal point and manual resizing to help you manage your pictures.
-
Categories
A taxonomy used to group products together.
-
Carts
Used to track user and guest carts within Payload. Added by the ecommerce plugin.
-
Addresses
Saves user's addresses for easier checkout. Added by the ecommerce plugin.
-
Orders
Tracks orders once a transaction successfully completes. Added by the ecommerce plugin.
-
Transactions
Tracks transactions from initiation to completion, once completed they will have a related Order item. Added by the ecommerce plugin.
-
Products and Variants
Primary collections for product details such as pricing per currency and optionally supports variants per product. Added by the ecommerce plugin.
Globals
See the Globals docs for details on how to extend this functionality.
-
HeaderThe data required by the header on your front-end like nav links.
-
FooterSame as above but for the footer of your site.
Access control
Basic access control is setup to limit access to various content based based on publishing status.
users: Users with theadminrole can access the admin panel and create or edit content, users with thecustomerrole can only access the frontend and the relevant collection items to themselves.pages: Everyone can access published pages, but only admin users can create, update, or delete them.productsvariants: Everyone can access published products, but only admin users can create, update, or delete them.carts: Customers can access their own saved cart, guest users can access any unclaimed cart by ID.addresses: Customers can access their own addresses for record keeping.transactions: Only admins can access these as they're meant for internal tracking.orders: Only admins and users who own the orders can access these.
For more details on how to extend this functionality, see the Payload Access Control docs.
User accounts
Guests
Layout Builder
Create unique page layouts for any type of content using a powerful layout builder. This template comes pre-configured with the following layout building blocks:
- Hero
- Content
- Media
- Call To Action
- Archive
Each block is fully designed and built into the front-end website that comes with this template. See Website for more details.
Lexical editor
A deep editorial experience that allows complete freedom to focus just on writing content without breaking out of the flow with support for Payload blocks, media, links and other features provided out of the box. See Lexical docs.
Draft Preview
All products and pages are draft-enabled so you can preview them before publishing them to your website. To do this, these collections use Versions with drafts set to true. This means that when you create a new product or page, it will be saved as a draft and will not be visible on your website until you publish it. This also means that you can preview your draft before publishing it to your website. To do this, we automatically format a custom URL which redirects to your front-end to securely fetch the draft version of your content.
Since the front-end of this template is statically generated, this also means that pages, products, and projects will need to be regenerated as changes are made to published documents. To do this, we use an afterChange hook to regenerate the front-end when a document has changed and its _status is published.
For more details on how to extend this functionality, see the official Draft Preview Example.
Live preview
In addition to draft previews you can also enable live preview to view your end resulting page as you're editing content with full support for SSR rendering. See Live preview docs for more details.
On-demand Revalidation
We've added hooks to collections and globals so that all of your pages, products, footer, or header changes will automatically be updated in the frontend via on-demand revalidation supported by Nextjs.
Note: if an image has been changed, for example it's been cropped, you will need to republish the page it's used on in order to be able to revalidate the Nextjs image cache.
SEO
This template comes pre-configured with the official Payload SEO Plugin for complete SEO control from the admin panel. All SEO data is fully integrated into the front-end website that comes with this template. See Website for more details.
Search
This template comes with SSR search features can easily be implemented into Next.js with Payload. See Website for more details.
Orders and Transactions
Transactions are intended for keeping a record of any payment made, as such it will contain information regarding an order or billing address used or the payment method used and amount. Only admins can access transactions.
An order is created only once a transaction is successfully completed. This is a record that the user who completed the transaction has access so they can keep track of their history. Guests can also access their own orders by providing an order ID and the email associated with that order.
Currencies
By default the template ships with support only for USD however you can change the supported currencies via the plugin configuration. You will need to ensure that the supported currencies in Payload are also configured in your Payment platforms.
Stripe
By default we ship with the Stripe adapter configured, so you'll need to setup the secretKey, publishableKey and webhookSecret from your Stripe dashboard. Follow Stripe's guide on how to set this up.
Tests
We provide automated tests out of the box for both E2E and Int tests along with this template. They are being run in our CI to ensure the stability of this template over time. You can integrate them into your CI or run them locally as well via:
To run Int tests wtih Vitest:
pnpm test:int
To run E2Es with Playwright:
pnpm test:e2e
or
pnpm test
To run both.
Jobs and Scheduled Publish
We have configured Scheduled Publish which uses the jobs queue in order to publish or unpublish your content on a scheduled time. The tasks are run on a cron schedule and can also be run as a separate instance if needed.
Note: When deployed on Vercel, depending on the plan tier, you may be limited to daily cron only.
Website
This template includes a beautifully designed, production-ready front-end built with the Next.js App Router, served right alongside your Payload app in a instance. This makes it so that you can deploy both your backend and website where you need it.
Core features:
- Next.js App Router
- TypeScript
- React Hook Form
- Payload Admin Bar
- TailwindCSS styling
- shadcn/ui components
- User Accounts and Authentication
- Fully featured blog
- Publication workflow
- Dark mode
- Pre-made layout building blocks
- SEO
- Search
- Live preview
- Stripe payments
Cache
Although Next.js includes a robust set of caching strategies out of the box, Payload Cloud proxies and caches all files through Cloudflare using the Official Cloud Plugin. This means that Next.js caching is not needed and is disabled by default. If you are hosting your app outside of Payload Cloud, you can easily reenable the Next.js caching mechanisms by removing the no-store directive from all fetch requests in ./src/app/_api and then removing all instances of export const dynamic = 'force-dynamic' from pages files, such as ./src/app/(pages)/[slug]/page.tsx. For more details, see the official Next.js Caching Docs.
Development
To spin up this example locally, follow the Quick Start. Then Seed the database with a few pages, posts, and projects.
Working with Postgres
Postgres and other SQL-based databases follow a strict schema for managing your data. In comparison to our MongoDB adapter, this means that there's a few extra steps to working with Postgres.
Note that often times when making big schema changes you can run the risk of losing data if you're not manually migrating it.
Local development
Ideally we recommend running a local copy of your database so that schema updates are as fast as possible. By default the Postgres adapter has push: true for development environments. This will let you add, modify and remove fields and collections without needing to run any data migrations.
If your database is pointed to production you will want to set push: false otherwise you will risk losing data or having your migrations out of sync.
Migrations
Migrations are essentially SQL code versions that keeps track of your schema. When deploy with Postgres you will need to make sure you create and then run your migrations.
Locally create a migration
pnpm payload migrate:create
This creates the migration files you will need to push alongside with your new configuration.
On the server after building and before running pnpm start you will want to run your migrations
pnpm payload migrate
This command will check for any migrations that have not yet been run and try to run them and it will keep a record of migrations that have been run in the database.
Docker
Alternatively, you can use Docker to spin up this template locally. To do so, follow these steps:
- Follow steps 1 and 2 from above, the docker-compose file will automatically use the
.envfile in your project root - Next run
docker-compose up - Follow steps 4 and 5 from above to login and create your first admin user
That's it! The Docker instance will help you get up and running quickly while also standardizing the development environment across your teams.
Seed
To seed the database with a few pages, products, and orders you can click the 'seed database' link from the admin panel.
The seed script will also create a demo user for demonstration purposes only:
- Demo Customer
- Email:
customer@example.com - Password:
password
- Email:
NOTICE: seeding the database is destructive because it drops your current database to populate a fresh one from the seed template. Only run this command if you are starting a new project or can afford to lose your current data.
Production
To run Payload in production, you need to build and start the Admin panel. To do so, follow these steps:
- Invoke the
next buildscript by runningpnpm buildornpm run buildin your project root. This creates a.nextdirectory with a production-ready admin bundle. - Finally run
pnpm startornpm run startto run Node in production and serve Payload from the.builddirectory. - When you're ready to go live, see Deployment below for more details.
Deploying to Vercel
This template can also be deployed to Vercel for free. You can get started by choosing the Vercel DB adapter during the setup of the template or by manually installing and configuring it:
pnpm add @payloadcms/db-vercel-postgres
// payload.config.ts
import { vercelPostgresAdapter } from '@payloadcms/db-vercel-postgres'
export default buildConfig({
// ...
db: vercelPostgresAdapter({
pool: {
connectionString: process.env.POSTGRES_URL || '',
},
}),
// ...
We also support Vercel's blob storage:
pnpm add @payloadcms/storage-vercel-blob
// payload.config.ts
import { vercelBlobStorage } from '@payloadcms/storage-vercel-blob'
export default buildConfig({
// ...
plugins: [
vercelBlobStorage({
collections: {
[Media.slug]: true,
},
token: process.env.BLOB_READ_WRITE_TOKEN || '',
}),
],
// ...
Self-hosting
Before deploying your app, you need to:
- Ensure your app builds and serves in production. See Production for more details.
- You can then deploy Payload as you would any other Node.js or Next.js application either directly on a VPS, DigitalOcean's Apps Platform, via Coolify or more. More guides coming soon.
You can also deploy your app manually, check out the deployment documentation for full details.
Questions
If you have any issues or questions, reach out to us on Discord or start a GitHub discussion.