E-Commerce Simplified: A Practical Guide to Building Your Store with Storyblok, Commercetools, and React.js
Storyblok is the first headless CMS that works for developers & marketers alike.
In this tutorial, we'll learn how to build a performant e-commerce store with Commercetools and React.js. You can explore the code for this demo on GitHub. You can also look at the live demo:storyblok-commercetools-react-storefront.vercel.app.
You can also jump into one of the specific chapters below
- Setup Storyblok Space
- Adding e-commerce integration
- Storefront in React.js
- Add Storyblok to the React.js starter
- Store deployment
Storyblok Space
First, sign up on Storyblok for a free account and create a new space. Next, add a name to your storefront and click Create space {3}
Once you've created a new space, you can check out our eCommerce storefront documentation to set up a storefront with Storyblok. Next, you will see some e-commerce components such as Product Feature {1}, Product Grid {2}, and Product Slider {3} components, as shown below.
Commercetools E-commerce Integration
Before navigating to our React application, we must first set up the Commercetools plugin from Storyblok. You can check out the commercetools guide on how to set up and retrieve your endpoint from commercetools.
Next, follow the integration guide on how to set it up. Once your integration works, you should be able to select a single anime product or even categories on Storyblok, as shown in the image below.
Connect your Storyblok Space to your React.js application
To connect to the Storyblok space you've created, navigate to the settings tab of your space, click on access tokens, and copy the preview token provided by Storyblok as shown below
Next, navigate to your visual editor tab and set up your Location (default environment)
, and your preview URLs with a localhost
environment and preview URLs, as shown in the image below.
It's important to note that with Storyblok V2, you must set up your development server with an HTTPS proxy. We will use port 3010
, so the URL to access the website will be https://localhost:3010/
NOTE: Read this guide if you don’t know how to set up an HTTPS proxy on macOS.
As shown below, create a .env
file and add your preview token to your .env
file.
You can start your development server
Create a Storefront on Storyblok
In this section, we'll create our storefront components in Storyblok. First, navigate to your Storyblok space and the Home Page. In our Next.js App.js
, we see several components: a hero, a product grid, a product feature, and a product slider.
Our storefront guide shows how to set up the product and product slider components. Let's create the Hero and product grid components below.
Navigate to the product field, and in the Plugin Options, select a custom type, sb-commercetools {1}, and for the Source, select Self {2}, and add your commercetools endpoints {3} and URLs as shown below:
Add Storyblok to React.js starter
In this section, we'd connect our Storyblok space to the React.js starter. First, let's install @storyblok-react
package.
The @storyblok/react
allows us to interact with content from Storyblok API and gives you a wrapper component that creates editable blocks in the Storyblok visual editor.
Create a Storyblok Config
In the src/App.js
file, add the following code to import the storefront components and load the Storyblok client along with the API key stored in the .env
file.
Creating Storefront Pages
In this section, we will create pages for our storefront application. To do this, navigate to your src
folder, and inside it, create the folder, pages
. In our Pages folder, create two files, Home.jsx
and ProductDetails.jsx
.
Below, we will use the Storyblok API to fetch stories
This section above will create two defined routes for a Homepage and a ProductDetails
page. To do this, navigate to your src folder; inside it, create a folder named Pages
, and in it, two files: Home.jsx
and ProductDetails.jsx
. Copy and paste the following to the respective file into your Home.jsx:
In the code above, the story home
is fetched from Storyblok, and the story response object is passed to the story state. If the state contains the content object, the defined Storyblok component will be displayed.
Next, we will create the ProductDetails page, which will contain the product name, an image of the product, a description, and the product price for a single product. Add the code block below to your ProductDetails.jsx
file
In the code above, we created a ProductDetails
component to fetch a product and display its details for a single product using the id.
The fetchHomeStory
function fetches the product story from the Storyblok API and updates the state variable with the fetched data.
It then displays the product image, name, description, price, and a button to order the product.
Adapting the Components to Storyblok
Earlier on, we created a set of components for the e-commerce storefront on Storyblok. To access the Storyblok content, we will create corresponding components in the React.js application.
Hero.jsx
In this component, we will add the blok
prop and use the corresponding Storyblok content (if it exists). The blok
prop will contain the application's headline, description, and hero image.
ProductFeature.jsx
This component will utilize the blok
property, and contain the title and image of the featured product in the e-commerce application.
The results can be shown in the visual editor as seen below:
ProductGrid.jsx
Here the grid component will display all products from the blok
prop. The content to be displayed are the product title, image, and description.
You can see the application as shown in the image below:
ProductSlider.jsx and Page.jsx
To add the following component to the React application, first, we'd create the product slider component by creating a new file ProductSider.jsx
and inside it, we'd add the following lines of code below:
Next, our Page.jsx
component will render all the data for our application page. To do this, add the following lines of code to your Page.jsx
as shown below
Retrieving data from Commercetools in a React app
This section will show you how to fetch products from Commercetools using the product ID
. First, in your pages
folder, create a new file called FetchProducts.jsx
.
Next, install commercetools SDK for clients, middleware-http
and middleware auth
using the command below:
The packages above allow us to create a middleware and connect to our commercetools project in our React application.
Next, initialize the middleware in our file using the Commercetools middleware package. We'd also initialize a client middleware as shown below
In the code above, we initialized a client
object to communicate with commercetoools for fetching products using their IDs
The createAuthMiddlewareForClientCredentialsFlow
function creates a middleware to handle authentication for our client credentials flow with our commercetools environment variables.
The createHttpMiddleware
function creates an HTTP middleware to handle requests in our React application, the host
parameter points to our commercetools project endpoint.
Our createClient
function creates a client instance to execute our requests to our commercetools endpoint.
In the fetchProducts
function, we use React useState
to initialize the products
state to an empty array while we use the setProducts
function to update our state when products are fetched.
We then fetch products from the commercetools endpoint /product_key/products
.
You can learn how to retrieve data/products from commercetools from their documentation here.
Deployment
The created e-commerce storefront can be deployed on a hosting platform like Vercel or Netlify. For this demo, we will use Vercel for deployment. We must set up the CLI tool and set up a user account.
To install the Vercel tool, enter the following in the CLI environment:
To deploy the application to the production environment, enter the following command.
Conclusion
Using a headless approach to your eCommerce needs can improve your user experience. Storyblok lets you access a visual editor for your content editors and developers while storing your products on commercetools. The two combined can double your performance and reduce customer load times. You can learn more about Storyblok and eCommerce with the resources below
Resources | Link |
---|---|
Project Example Repo | https://github.com/iamfortune/Storyblok-Commercetools-React-Storefront |
Adding Storyblok to React.js in 5 minutes | https://www.storyblok.com/tp/headless-cms-react |
Storyblok Commercetools Integration | https://www.storyblok.com/docs/guide/integrations/ecommerce/commercetools |
Storyblok E-commerce integrations | https://www.storyblok.com/mp/headless-ecommerce-integrations |
Getting products from Commercetools | https://docs.commercetools.com/api/projects/products#get-product |