# commercetools parameters

> Use the available parameter types to incorporate content from commercetools into your components and compositions.

Source: https://docs.uniform.app/docs/integrations/commerce/commercetools/commercetools-parameters

Installing the commercetools integration will cover most use cases without the need for additional code. However, if you need to do any post-processing of any data, or have other specific needs for manipulating data, you can leverage the following
parameter types with enhancers.

| Parameter type | Description |
| --- | --- |
| [commercetools Product](#commercetools-product) | Select a single product from commercetools. |
| [commercetools Product List](#commercetools-product-list) | Select multiple products from commercetools. |
| [commercetools Product Query](#commercetools-product-query) | Specify search criteria that's used to select a single or multiple products from commercetools. |
| [commercetools Category](#commercetools-category) | Select a specific product category. |

## commercetools Product

This parameter type allows a Uniform user to select a single
commercetools product.

### Add parameter to component

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**.
3. The following values can be specified:

   | Name | Description |
   | --- | --- |
   | Category constraints | Select specific categories that are allowed to appear as results in the product query. Note: Parent categories will include all child categories. |
   | Display field | Used to update which field is used for titles when previewing the results from the query. |
   | Restrict search–Master/Variants | Allows you to restrict the result to only include the product master, variants, or both. This can help to reduce the resultant JSON payload and improve site performance. |
   | Restrict search–Locale | Allows you to remove non-configured locales from results, further reducing potential JSON payload size. |
   | Restrict search–Currency | Allows you to remove non-configured currencies from results, further reducing potential JSON payload size. |
   | Required | Indicates whether the value is required when the component is used. |

   ![commercetools Product parameter configuration.](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-product/parameter-config.png)

### 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**.

   ![](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-product/edit-value-default.png)
2. Click the product you want to select.

   ![](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-product/edit-value-filters.png)

   > **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.

   ![](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-product/edit-value-selected.png)
4. You will see details about the product you selected, including the name, category, and price.

   ![](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-product/edit-value-saved.png)

   > **About this step:**
   >
   > After your selection is saved, you are presented with the following options:
   >
   > - If you want to deselect 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.

```json
{
   "commercetoolsProduct": {
    "type": "commercetoolsProduct",
    "value": {
      "productIds": [
        "d4d087f8-d704-4c41-9d5a-52ca56177faa"
      ]
    }
  }
}
```

#### Add the enhancer

Uniform provides an enhancer so you don't need to write the API
calls to commercetools to retrieve data for products.

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

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

   ```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:

   ```javascript
   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:

   ```javascript
   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

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**.
3. The following values can be specified:

   | Name | Description |
   | --- | --- |
   | Category constraints | Select specific categories that are allowed to appear as results in the product query. Note: Parent categories will include all child categories. |
   | Display field | Used to update which field is used for titles when previewing the results from the query. |
   | Restrict search–Master/Variants | Allows you to restrict the result to only include the product master, variants, or both. This can help to reduce the resultant JSON payload and improve site performance. |
   | Restrict search–Locale | Allows you to remove non-configured locales from results, further reducing potential JSON payload size. |
   | Restrict search–Currency | Allows you to remove non-configured currencies from results, further reducing potential JSON payload size. |
   | Required | Indicates whether the value is required when the component is used. |

   ![commercetools Product List parameter configuration.](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-product-list/parameter-config.png)

### Edit parameter value

This parameter type works the same way as [commercetools Product](#commercetools-product),
adding the ability to select multiple products from the list
when the parameter value is edited.

![Example of the editing interface for an commercetools Product List parameter on a component.](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-product-list/edit-parameter-value.png)

### Configure an enhancer

This parameter type uses the same enhancer as the [commercetools Product](#configure-an-enhancer)
parameter type.

The following is an example of what Uniform stores for the parameter.

```json
{
  "commercetoolsProductList": {
    "type": "commercetoolsProductList",
    "value": {
      "productIds": [
        "d4d087f8-d704-4c41-9d5a-52ca56177faa",
        "422acfa6-69d1-43e2-b519-2f82517605eb"
      ]
    }
  }
}
```

## commercetools Product Query

This parameter type allows a Uniform user to configure a query
that will determine which commercetools products are retrieved.

### Add parameter to component

For a user to select categories, you must add the parameter
to a component. The parameter is used to store the selected categories from
the category selection UI.

1. In Uniform, navigate to your component.
2. Add a new parameter using the parameter type **commercetools Category**.
3. The following values can be specified:

   | Name | Description |
   | --- | --- |
   | Category constraints | Select specific categories that are allowed to appear as results in the product query. Note: Parent categories will include all child categories. |
   | Display field | Used to update which field is used for titles when previewing the results from the query. |
   | Restrict search–Master/Variants | Allows you to restrict the result to only include the product master, variants, or both. This can help to reduce the resultant JSON payload and improve site performance. |
   | Restrict search–Locale | Allows you to remove non-configured locales from results, further reducing potential JSON payload size. |
   | Restrict search–Currency | Allows you to remove non-configured currencies from results, further reducing potential JSON payload size. |

   ![commercetools Product Query parameter configuration.](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-product-query/add-parameter-to-component.png)

### Edit parameter value

Configuring this parameter type involves specifying the way
a product search should work. You are able to control the
following search settings:

| Field | Description |
| --- | --- |
| Categories | Filter the query based on the category a product is within. Multiple categories can be selected, and parent categories will include all child categories. |
| Count | The maximum number of products that are returned. |
| Keyword | Only products whose names match the specified keyword(s) are included. |
| Sort | The product property used to sort the products that are returned. |
| Sort Order | Whether the products should be sorted in ascending or descending order. |

![Example of the editing interface for a commercetools Product Query parameter on a component.](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-product-query/edit-parameter-value.png)

### Configure an enhancer

This parameter type uses the same enhancer as the [commercetools Product](#configure-an-enhancer)
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 stores the settings used to perform
> the search.
>
> ```json
> {
>   "commercetoolsProductQuery": {
>     "type": "commercetoolsProductQuery",
>     "value": {
>       "options": {
>         "count": 3,
>         "categories": [
>           "4e90d0fd-5344-4e3f-b85b-2cd4fabd746a",
>           "6efb29b3-a3c9-4d12-9eda-7d04f9d38cf3",
>           "bf3c0adc-0472-4439-b0e4-16389df009bd"
>         ],
>         "brand": "",
>         "keyword": "Blue",
>         "sort": "Name",
>         "sortOrder": "desc"
>       },
>       "searchLocale": "en"
>     }
>   }
> }
> ```

## commercetools Category

This parameter type allows a Uniform user to select one or multiple categories to be set as the
value of the parameter. This can be useful when composing pages where a global category filter needs
to be set, or to access the category description stored in commercetools through the enhancer.

### Add parameter to component

For a user to configure a query to determine the
products from commercetools to retrieve, you must add the parameter
to a component. The parameter is used to store the query settings
used to perform the search.

1. In Uniform, navigate to your component.
2. Add a new parameter using the parameter type **commercetools Product Query**.
3. The following values can be specified:

   | Name | Description |
   | --- | --- |
   | Allow multi select | Determines if a user is able to select one or many categories within the category selector. |
   | Required | Indicates whether the value is required when the component is used. |

   ![commercetools Category parameter configuration.](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-category/add-parameter-to-component.png)

### Edit parameter value

When you use a component with a commercetools Category parameter, by default no categories will be selected.
You are prompted to select a category.

1. Click **Select**.

   ![](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-category/edit-value-default.png)
2. Click the category or categories you would like to select.

   ![](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-category/edit-value-select.png)

   > **About this step:**
   >
   > Sub-categories can be accessed by expanding the parents category with the "+" button
   > on the left-hand side of the category.
3. Click **Save and Close** to save your selection.

   ![](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-category/edit-value-selected.png)
4. You will see details about the category or categories you have selected, including the name and slug.

   ![](https://docs.uniform.app/images/integrations/commerce/commercetools/use-parameters/commercetools-category/edit-value-saved.png)

   > **About this step:**
   >
   > After your selection is saved, you are presented with the following options:
   >
   > - If you want to deselect the product, click the **Unlink** button.
   > - If you want to add/edit the categories, click the **Select Category** button.

### Configure an enhancer

This parameter type uses the same enhancer as the [commercetools Product](#configure-an-enhancer)
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 stores the settings used to perform
> the search.
>
> ```json
> {
>   "type": "commercetoolsProductQuery",
>     "commercetoolsCategory": {
>       "type": "commercetoolsCategorySelector",
>       "value": {
>         "categoryIds": [
>           "27b3ca02-0a81-4afb-871e-050d214815ca",
>           "a9ca1554-55cc-4fe3-bfc3-8157c9368b17"
>         ],
>         "restrictLocale": "en"
>       }
>     }
> }
> ```
