# Different discounts for different categories

> For the complete documentation index, see [llms.txt](https://docs.talon.one/llms.txt).

Let's create a [promotion rule](/docs/product/rules/overview.md#promotion-rules) that gives a discount only on products that are part of a specific category, for example, `Road Bikes` or `Helmets`.
This campaign relies on the [cart filter feature](/docs/product/rules/cart-item-filters/overview.md) to select the items that are part of the requested category.

## Create the discount promotion

### Cart item filters

Let's start by creating the cart item filters. We will discount road bikes by 40% if we
find one in the session and helmets by 10% if we find more than 2, so we create two filters:

1. Click **Add Filter**.
1. Click **Add Filter Step** and select **Filter items by conditions**.
1. Set the filter to <Attribute name="Item category (Item)" type="builtin" /> **is equal to** `Road Bikes`.
1. In **Save As**, type `RoadBikes` and click **Save Filter**.

This creates the <Attribute name="RoadBikes" type="filter"/> filter and we also get the <Attribute name="RoadBikes item count (RoadBikes)" type="filter"/>
filter created automatically, which
returns the amount of items in the cart item filter.

Repeat these steps for the `Helmets` category. This procedure also creates the <Attribute name="Helmets item count (Helmets)" type="filter"/> filter.
The category of each item is provided by your shop system via the [Integration API](/docs/dev/integration-api/overview.md).

### Rule 1: Checking the helmets

#### Condition

1. Name the rule `10% off Category Helmets`.
1. Click **Add condition** and select **Check attribute value** with the following
   expression: <Attribute type="filter" name="Helmets item count (Helmets)"/> `is greater than or equal to 2`.

#### Effect

Because we do not want to discount the whole session but rather specific items, we use
the [**Discount individual items**](/docs/product/rules/effects/use-effects.md#discount-individual-items) effect:

1. Set the **List of items** to `Helmets`.
1. Set the **Discount name** to `10% off helmets`.
1. Set the **Discount value** to <Attribute name="Price of item (Item)" />`* 10%`.

### Rule 2: Checking the bikes

Repeat the previous section for the road bikes:

1. Select the <Attribute type="filter" name="RoadBikes item count (RoadBikes)"/> for the condition.
1. Select <Attribute type="filter" name="RoadBikes"/> in the effect.
1. Set the discount value to: <Attribute name="Price of item (Item)" />`* 40%`.

These two effects are applied every time the condition is true.

## Run the campaign

To run the campaign, use the following payload sent to the Integration API's [Update customer session][cs] endpoint:

<Tabs
  defaultValue="req"
  values={[
    {label: '🚀 Request', value: 'req'},
    {label: '✅ Response', value: 'res'},
  ]}>

<TabItem value="req">

Let's trigger our campaign by inserting two cart items in the session and ensuring the session
is closed:

- Line 12: The item is part of the `Road Bikes` category.
- Line 22: The other item is part of the `Helmets` category.
- Line 20: This item has two units.

```json title="Sending a session update" {12,20,22} showLineNumbers
CURL -X PUT https://mycompany.europe-west1.talon.one/v2/customer_sessions/my_session_id -d
'{
  "customerSession": {
    "profileId": "CHP75036CF3",
    "state": "closed",
    "cartItems": [
      {
        "name": "ProductC",
        "sku": "sku-00003",
        "quantity": 1,
        "price": 200,
        "Category": "Road Bikes",
        "weight": 0,
        "position": 0,
        "attributes": {}
      },
      {
        "name": "ProductD",
        "sku": "sku-00004",
        "quantity": 2,
        "price": 30,
        "Category": "Helmets",
        "weight": 0,
        "position": 1,
        "attributes": {}
      }
    ],
    "couponCodes": [
      ""
    ],
    "referralCode": "",
    "attributes": {},
    "additionalCosts": {}
  },
  "responseContent": []
}'
```

</TabItem>
<TabItem value="res">

We receive this response, which shows the `setDiscountPerItem` effect for _each item_.

- Lines: 14, 27, and 40: The `position` property represents which item is impacted by the effect based
  on the item's position in the `cartItems` array sent in the update request.
- Lines 28 and 41: The `subPosition` property represents which unit of the given item
  is impacted in the effect. This only relevant when `quantity` is greater than 1
  in the session, like for our helmets.

```json title="Response" {14,27,40,28,41} showLineNumbers
{
  "createdCoupons": [],
  "createdReferrals": [],
  "effects": [
    {
      "campaignId": 250,
      "rulesetId": 502,
      "ruleIndex": 0,
      "ruleName": "10% off Category Helmets",
      "effectType": "setDiscountPerItem",
      "props": {
        "name": "10% off helmets#1",
        "value": 3.0,
        "position": 1,
        "subPosition": 0
      }
    },
    {
      "campaignId": 250,
      "rulesetId": 502,
      "ruleIndex": 0,
      "ruleName": "10% off Category Helmets",
      "effectType": "setDiscountPerItem",
      "props": {
        "name": "10% off helmets#1",
        "value": 3.0,
        "position": 1,
        "subPosition": 1
      }
    },
    {
      "campaignId": 250,
      "rulesetId": 502,
      "ruleIndex": 1,
      "ruleName": "40% off Category Road Bikes",
      "effectType": "setDiscountPerItem",
      "props": {
        "name": "40% off per item#0",
        "value": 80.0,
        "position": 0,
        "subPosition": 0
      }
    }
  ]
}
```

</TabItem>
</Tabs>

:::tip Key Takeaways

- You can have several rules in a campaign. They are all evaluated.
- You can select specific items from the session and save them in a list of items thanks to a **cart item filter**.
- Some effects can be applied to specific lists of cart items, such as the **Discount individual items** effect.
- The category of an item is passed via the Integration API's [Update customer session][cs] endpoint.
- A per-item effect always indicates what item is being affected wit `position` and `subPosition` properties.
:::

## Related pages

- [Cart item filters](/docs/product/rules/cart-item-filters/overview.md)
- [Use conditions](/docs/product/rules/conditions/overview.md) and [effects](/docs/product/rules/effects/overview.md)

[cs]: /integration-api#tag/Customer-sessions/operation/updateCustomerSessionV2
