Use parameters
After you install the Commercetools integration, the following parameter types are available. You can use these to incorporate content from Commercetools into your components and compositions.
Parameter type | Description |
---|---|
Commercetools Product | Select a single product from Commercetools. |
Commercetools Product List | Select multiple products from Commercetools. |
Commercetools Product
This parameter type allows a Uniform user to select a single Commercetools product.
Add parameter to component
In order to allow a user to select a product from Commercetools, you must add a parameter to a component. The parameter is used to store the identifier to the selected product when the user selects a product.
In Uniform, navigate to your component.
Add a new parameter using the parameter type Commercetools Product.
Commercetools Product parameter configuration.
Edit parameter value
When you use a component with a Commercetools Product parameter, by default no product will be selected. You are prompted to select a product.
Click Select.
Click the product you want to select.
About this stepA couple of filters are available. The dropdown allows you to filter by category. The text box allows you to filter by product name.
Click Accept to save your selection.
You will see details about the product you selected, including the name, category, and price.
About this stepAfter your selection is saved, you are presented with the following options:
- If you want to unselect the product, click the red X.
Configure an enhancer
When a product is selected, Uniform only stores the identifier for the product. Your front-end application must retrieve the details for the product. Uniform provides an enhancer to simplify this process.
Using the enhancer provided by Uniform saves you from having to write logic to interact directly with Commercetools.
How Uniform stores the selected product
The following is an example of what Uniform stores for the parameter.
{
"type": "commercetoolsProduct",
"value": "######################"
}
The product ID is stored as the value.
Add the enhancer
Uniform provides an enhancer so you don't need to write the API calls to Commercetools to retrieve data for products.
For more information about the enhancer, see the package documentation.
Add the following npm packages to your front-end application:
@uniformdev/canvas
@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 = [!!! YOUR CT AUTH URL !!!];
const commercetoolsProjectKey = [!!! YOUR CT PROJECT KEY !!!];
const commercetoolsClientId = [!!! YOUR CT CLIENT ID !!!];
const commercetoolsClientSecret = [!!! YOUR CT CLIENT SECRET !!!];
const commercetoolsApiUrl = [!!! YOUR CT API URL !!!];
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,
});About this stepWe recommend you moving the Commercetools credentials to environment variables rather than hard-coding them in the front-end app.
In a text editor, open the file where you retrieve the composition definition from Uniform.
About this stepYou are looking for the code calls the async function
getComposition
. The code below assumes the object returned is set in a variablecomposition
.Add the following import statements:
import { EnhancerBuilder, enhance } from '@uniformdev/canvas';
import {
CANVAS_COMMERCETOOLS_PARAMETER_TYPES,
} from '@uniformdev/canvas-commercetools';
import {
commercetoolsEnhancer
} from '../lib/commercetoolsEnhancer';Add the following code:
await enhance({
composition,
enhancers: new EnhancerBuilder().parameterType(
CANVAS_COMMERCETOOLS_PARAMETER_TYPES,
commercetoolsEnhancer,
),
context: {},
});About this stepThis registers the enhancer to be used for any occurrence of the Commercetools Product parameter.
Now, the parameter value in the composition is mutated to include the field values for the selected Commercetools product (instead of just being identifiers).
Commercetools Product List
This parameter type allows a Uniform user to select multiple Commercetools products.
Add parameter to component
In order to allow a user to select multiple products from Commercetools, you must add a parameter to a component. The parameter is used to store the identifiers to the selected products when the user selects products.
In Uniform, navigate to your component.
Add a new parameter using the parameter type Commercetools Product List.
Commercetools Product List parameter configuration.
Edit parameter value
This parameter type works the same way as Commercetools Product, adding the ability to select multiple products from the list when the parameter value is edited.

Configure an enhancer
This parameter type uses the same enhancer as the Commercetools Product parameter type.
The parameter settings are stored in Canvas in a slightly different format. Instead of a single product ID being stored, the value is stored as an array of product IDs.
{
"type": "commercetoolsProductList",
"value": [
"######################",
"######################"
]
}