# Discount specific SKUs for new clients

This rule discounts the price of the SKUs belonging to a given set for logged clients who are placing their first order. The SKU code and total number of orders already associated with the customer are checked. If the customer has no order associated yet and the SKU code matches one of the codes listed in the provided array, a percentage discount is applied to the related price. If you apply this rule to a price list that contains prices associated with one or more of the given SKU codes, those prices will be discounted for customers who have not made any previous orders.

The example below is based on the following reference JSONs. Once the rules are evaluated against the resource payload, the outcomes show if and how the conditions are matched and the related actions applied.

{% tabs %}
{% tab title="Rules" %}

```json
{
  "rules": [
    {
      "name": "Discount specific skus for new clients",
      "conditions": [
        {
          "field": "price.jwt_customer.total_orders_count",
          "matcher": "eq",
          "value": 0
        },
        {
          "field": "price.sku_code",
          "matcher": "is_in",
          "value": [
            "JERSEYBLACK",
            "JERSEYGREEN"
          ],
          "group": "jerseys"
        }
      ],
      "actions": [
        {
          "type": "percentage",
          "selector": "price",
          "groups": [
            "jerseys"
          ],
          "value": 0.2
        }
      ]
    }
  ]
} 
```

{% endtab %}

{% tab title="Price" %}

```json
[
  {
    "price": {
      "sku_code": "JERSEYBLACK",
      "id": "lpeNYUvMTE",
      "jwt_customer": {
        "total_orders_count": 0,
        "id": "klzEhmRAMM"
      }
    }
  },
  {
    "price": {
      "sku_code": "JERSEYGREEN",
      "id": "lpeNYUvMTO",
      "jwt_customer": {
        "total_orders_count": 0,
        "id": "klzEhmRAMM"
      }
    }
  },
  {
    "price": {
      "sku_code": "BIBGREEN",
      "id": "lpeNYUvMTA",
      "jwt_customer": {
        "total_orders_count": 0,
        "id": "klzEhmRAMM"
      }
    }
  }
]
```

{% endtab %}

{% tab title="Outcomes" %}

```json
[
  {
    "id": "817d5187-e390-475e-84d8-c7ab47acf835",
    "name": "Discount specific skus for new clients",
    "priority": 0,
    "match": true,
    "conditions_logic": "and",
    "conditions": [
      {
        "field": "price.jwt_customer.total_orders_count",
        "matcher": "eq",
        "value": 0,
        "group": "4a5284a1-ece7-411f-abba-cd590c40b1ae",
        "match": true,
        "matches": [
          {
            "price": "lpeNYUvMTE",
            "jwt_customer": "klzEhmRAMM",
            "group": "4a5284a1-ece7-411f-abba-cd590c40b1ae"
          },
          {
            "price": "lpeNYUvMTO",
            "jwt_customer": "klzEhmRAMM",
            "group": "4a5284a1-ece7-411f-abba-cd590c40b1ae"
          },
          {
            "price": "lpeNYUvMTA",
            "jwt_customer": "klzEhmRAMM",
            "group": "4a5284a1-ece7-411f-abba-cd590c40b1ae"
          }
        ],
        "scope": "any"
      },
      {
        "field": "price.sku_code",
        "matcher": "is_in",
        "value": [
          "JERSEYBLACK",
          "JERSEYGREEN"
        ],
        "group": "jerseys",
        "match": true,
        "matches": [
          {
            "price": "lpeNYUvMTE",
            "group": "jerseys"
          },
          {
            "price": "lpeNYUvMTO",
            "group": "jerseys"
          }
        ],
        "scope": "any"
      }
    ],
    "actions": [
      {
        "resources": [
          {
            "resource_type": "prices",
            "id": "lpeNYUvMTE",
            "group": "jerseys",
            "quantity": null,
            "value": 0.2,
            "action_type": "percentage"
          },
          {
            "resource_type": "prices",
            "id": "lpeNYUvMTO",
            "group": "jerseys",
            "quantity": null,
            "value": 0.2,
            "action_type": "percentage"
          }
        ]
      }
    ]
  }
]
```

{% endtab %}
{% endtabs %}

## Rules breakdown

The desired results can be achieved with a single rule.

#### Conditions

The rule contains two conditions:

1. The first condition checks if the total order count of the logged customer is `0`, ensuring that the customer is placing their first order.
2. The second condition checks if the SKU code associated with the price is one of `JERSEYBLACK` or `JERSEYGREEN`, to identify if the SKU is eligible for the discount. The prices that match this condition are grouped as `jerseys`.

Since no `conditions_logic` is specified, the default value `and` will be used.

#### Actions

The only action in the rule is triggered if both the conditions are met and applies a 20% discount to the prices that match the second condition only (i.e. the ones belonging to the `jerseys` group).

## Resource payload analysis

The provided payload contains 3 prices, all associated with a logged customer who placed no order before. One of those prices (`klzEhmRAMM`) is associated with an SKU whose code is `JERSEYBLACK`, another (`lpeNYUvMTO`) with an SKU whose code is `JERSEYGREEN`, the remaining one with a different SKU code.

## Result check

The outcomes reflect the evaluation of the rules against the resource payload. Since all the prices satisfy the first condition and 2 of them also satisfy the second condition, the rule matches successfully (`match` is `true`). The 3 price IDs are all listed in the `matches` array of the first condition, along with the ID of the logged customer. The IDs of the two prices associated with the SKUs eligible for the discount are listed in the `matches` array of the second condition. Action is applied to those 2 prices (i.e. to the group `jerseys`) with a decimal value of `0.2` corresponding to the specified percentage discount.

## Conclusion

The rule matches the provided price payload and successfully identifies two prices associated with a new customer and with the SKU codes provided. Consequently, a 20% discount is applied to both prices. The third price in the payload won't be discounted because, even if it is associated with a new customer, it doesn't belong to an SKU eligible for the discount.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.commercelayer.io/rules-engine/use-cases/price-lists/discount-specific-skus-for-new-clients.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
