Skip to main content

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 typeDescription
Commercetools ProductSelect a single product from Commercetools.
Commercetools Product ListSelect 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.

  1. In Uniform, navigate to your component.

  2. Add a new parameter using the parameter type Commercetools Product.

    parameter-config
    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.

  1. Click Select.

    edit-value-default
  2. Click the product you want to select.

    edit-value-filters
    About this step

    A couple of filters are available. The dropdown allows you to filter by category. The text box allows you to filter by product name.

  3. Click Accept to save your selection.

    edit-value-selected
  4. You will see details about the product you selected, including the name, category, and price.

    edit-value-saved
    About this step

    After your selected 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.

tip

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.

tip

For more information about the enhancer, see the package documentation.

  1. Add the following npm packages to your front-end application:

    @uniformdev/canvas
    @uniformdev/canvas-commercetools
  2. Add the following file:

    /lib/commercetoolsEnhancer.js
      import 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 step

    We recommend you moving the Commercetools credentials to environment variables rather than hard-coding them in the front-end app.

  3. In a text editor, open the file where you retrieve the composition definition from Uniform.

    About this step

    You are looking for the code calls the async function getComposition. The code below assumes the object returned is set in a variable composition.

  4. 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';
  5. Add the following code:

    await enhance({
    composition,
    enhancers: new EnhancerBuilder().parameterType(
    CANVAS_COMMERCETOOLS_PARAMETER_TYPES,
    commercetoolsEnhancer,
    ),
    context: {},
    });
    About this step

    This registers the enhancer to be used for any occurrence of the Commercetools Product parameter.

Next steps

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.

  1. In Uniform, navigate to your component.

  2. Add a new parameter using the parameter type Commercetools Product List.

    add-parameter-to-component
    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.

edit-parameter-value
Example of the editing interface for an Commercetools Product List parameter on a component.

Configure an enhancer

This parameter type uses the same enhancer as the Commercetools Product parameter type.

note

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": [
"######################",
"######################"
]
}