Commercetools
Commercetools allows you to create powerful, highly customized commerce experiences while building a profitable, sustainable brand.
This integration allows business users to build personalized landing pages and storefronts by repurposing product content from your Commercetools product catalog. It also enables developers to use their preferred front-end tools to build these applications.
This section covers how to configure and use this integration.
Create API client
In order to configure a connection to Commercetools, you need to create an API client. This gives you the settings that will enable Uniform to use the Commercetools API:
Log into the Commercetools Merchant Center.
Navigate to Settings > Developer Settings.
Click Create new API client.
Provide a name.
Under Scopes, make the following selections:
- Categories > View
- Products (all) > View
- Products (published) > View
At the bottom of the screen, in the download your credentials section, switch to Environment Variables (.env).
About this step
Copy the values that are displayed.
Configure integration
This section describes how to add the Commercetools integration to your Uniform project.
Log into Uniform.
Navigate to Settings > Integrations.
Add the integration for Commercetools.
Enter the required fields:
Field Commercetools environment variable API Url CTP_API_URLAuth URL CTP_AUTH_URLClient ID CTP_CLIENT_IDClient Secret CTP_CLIENT_SECRETProject Key CTP_PROJECT_KEYEnter the optional fields, if applicable:
Field Description Locale Sets the locale for product titles Canvas users see in product fields. Currency Code Sets the currency displayed to Canvas users on products. Click Save.
Add parameter to Canvas component
This integration adds custom parameter types for Canvas components. The following parameter types are available:
Commercetools Product
This parameter type is used when the user wants to select a single product for a parameter.
Commercetools Product List
This parameter type is used when the user wants to select multiple products for a parameter.
Add parameter support to front-end component
When a user sets a value on a Commercetools parameter, Uniform doesn't store product details. It only stores settings that the front-end code can use to retrieve the relevant product details.
You don't have to write the code to retrieve the product details yourself. Uniform provides a component called an enhancer that handles this logic. You just need to add the enhancer to your application. Each parameter type has its own enhancer.
Add the following environment variables to your
.envfile:Variable name Commerce tools value CTP_PROJECT_KEYProject key CTP_CLIENT_SECRETClient secret CTP_CLIENT_IDClient ID CTP_AUTH_URLAuth URL CTP_API_URLAPI URL Add the following npm package to your application:
- Yarn
- pnpm
- npm
yarn add @uniformdev/canvas-commercetoolspnpm add @uniformdev/canvas-commercetoolsnpm install @uniformdev/canvas-commercetoolsAdd the following file:
/lib/commercetoolsEnhancer.jsimport getConfig from 'next/config';
import { ApiRoot, createApiBuilderFromCtpClient } from '@commercetools/platform-sdk';
import { createClient } from '@commercetools/sdk-client';
import { createAuthMiddlewareForClientCredentialsFlow } from '@commercetools/sdk-middleware-auth';
import { createHttpMiddleware } from '@commercetools/sdk-middleware-http';
import { createCommercetoolsEnhancer } from '@uniformdev/canvas-commercetools';
import fetch from 'node-fetch';
const commercetoolsAuthUrl = process.env.CTP_AUTH_URL;
const commercetoolsProjectKey = process.env.CTP_PROJECT_KEY;
const commercetoolsClientId = process.env.CTP_CLIENT_ID;
const commercetoolsClientSecret = process.env.CTP_CLIENT_SECRET;
const commercetoolsApiUrl = process.env.CTP_API_URL;
if (!commercetoolsAuthUrl) {
throw new Error('CTP_AUTH_URL env not set.');
}
if (!commercetoolsProjectKey) {
throw new Error('CTP_PROJECT_KEY env not set.');
}
if (!commercetoolsClientId) {
throw new Error('CTP_CLIENT_ID env not set.');
}
if (!commercetoolsClientSecret) {
throw new Error('CTP_CLIENT_SECRET env not set.');
}
if (!commercetoolsApiUrl) {
throw new Error('CTP_API_URL env not set.');
}
const authMiddleware = createAuthMiddlewareForClientCredentialsFlow({
host: commercetoolsAuthUrl,
projectKey: commercetoolsProjectKey,
credentials: {
clientId: commercetoolsClientId,
clientSecret: commercetoolsClientSecret,
},
});
const httpMiddleware = createHttpMiddleware({
host: commercetoolsApiUrl,
fetch,
});
const ctpClient = createClient({
middlewares: [authMiddleware, httpMiddleware],
});
const apiRoot: ApiRoot = createApiBuilderFromCtpClient(ctpClient);
export const commercetoolsEnhancer = createCommercetoolsEnhancer({
apiRoot,
projectKey: commercetoolsProjectKey,
});Find the file where the
EnhancerBuilderis used.About this step
If your application is built using Next.js, this is probably in
getStaticProps.Add the following import statement:
import { canvas_COMMERCETOOLS_PARAMETER_TYPES } from '@uniformdev/canvas-commercetools';
import { commercetoolsEnhancer } from '../lib/commercetoolsEnhancer';About this step
Be sure that the path to the enhancer file matches your environment.
Add your enhancer:
const enhancers = new EnhancerBuilder()
// ... other enhancers here ...
.parameterType(CANVAS_COMMERCETOOLS_PARAMETER_TYPES, commercetoolsEnhancer);
await enhance({ composition, enhancers, context: {});About this step
This tells Uniform to apply the enhancer any time one of the Commercetools parameters is found in a composition.