# Discount all the SKU in an order based on the customer email domain

This rule applies a discount to all the products present in an order if the customer's email belongs to one of two provided domains. The email address of the customer who's placing the order is checked and a percentage discount is applied to all the line items associated with an SKU if the conditions are met.

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": "Customer email domain mybrand.com / example.com",
      "conditions_logic": "or",
      "conditions": [
        {
          "field": "order.customer_email",
          "matcher": "matches",
          "value": ".*@mybrand.com"
        },
        {
          "field": "order.customer_email",
          "matcher": "matches",
          "value": ".*@example.com"
        }
      ],
      "actions": [
        {
          "type": "percentage",
          "selector": "order.line_items.sku",
          "value": 0.15
        }
      ]
    }
  ]
} 
```

{% endtab %}

{% tab title="Order" %}

```json
{
  "order": {
    "customer_email": "john.doe@mybrand.com",
    "id": "oKkhYLlzgE",
    "line_items": [
      {
        "quantity": 1,
        "id": "gnYtPpKLeG"
      },
      {
        "quantity": 7,
        "id": "wLGtgmdLjJ",
        "sku": {
          "id": "ZXxPSkbpNP"
        }
      },
      {
        "quantity": 1,
        "id": "zbZtmarXKR1",
        "sku": {
          "id": "WVyPSgRwxd"
        }
      },
      {
        "quantity": 1,
        "id": "zbZtmarXKR2",
        "sku": {
          "id": "WVyPSgRwxZ"
        }
      },
      {
        "quantity": 1,
        "id": "aoMtJlQlxD",
        "sku": {
          "id": "WKjRSMmlxe"
        }
      }
    ]
  }
}
```

{% endtab %}

{% tab title="Outcomes" %}

```json
[
  {
    "id": "2a56c419-7877-419a-8890-84cd634ccb8b",
    "name": "Customer email domain mybrand.com / example.com",
    "priority": 0,
    "match": true,
    "conditions_logic": "or",
    "conditions": [
      {
        "field": "order.customer_email",
        "matcher": "matches",
        "value": ".*@mybrand.com",
        "group": "79b64b07-e8ec-4a2a-b49a-92c7cda1249f",
        "match": true,
        "matches": [
          {
            "order": "oKkhYLlzgE",
            "group": "79b64b07-e8ec-4a2a-b49a-92c7cda1249f"
          }
        ],
        "scope": "any"
      },
      {
        "field": "order.customer_email",
        "matcher": "matches",
        "value": ".*@example.com",
        "scope": "any",
        "group": "79b64b07-e8ec-4a2a-b49a-92c7cda1249f",
        "match": false,
        "matches": [

        ]
      }
    ],
    "actions": [
      {
        "resources": [
          {
            "action_type": "percentage",
            "resource_type": "line_items",
            "group": "79b64b07-e8ec-4a2a-b49a-92c7cda1249f",
            "quantity": 7,
            "id": "wLGtgmdLjJ",
            "value": 0.15
          },
          {
            "action_type": "percentage",
            "resource_type": "line_items",
            "group": "79b64b07-e8ec-4a2a-b49a-92c7cda1249f",
            "quantity": 1,
            "id": "zbZtmarXKR1",
            "value": 0.15
          },
          {
            "action_type": "percentage",
            "resource_type": "line_items",
            "group": "79b64b07-e8ec-4a2a-b49a-92c7cda1249f",
            "quantity": 1,
            "id": "zbZtmarXKR2",
            "value": 0.15
          },
          {
            "action_type": "percentage",
            "resource_type": "line_items",
            "group": "79b64b07-e8ec-4a2a-b49a-92c7cda1249f",
            "quantity": 1,
            "id": "aoMtJlQlxD",
            "value": 0.15
          }
        ]
      }
    ]
  }
]
```

{% 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 email address associated with the order ends with `@mybrand.com`.
2. The second condition checks if the email address associated with the order ends with `@example.com`.

The `conditions_logic` is set to `or`, meaning that if either condition is met, the rule will be applied.

#### Actions

The only action in the rule is triggered if any of the conditions are met and applies a 15% discount to all the line items associated with an SKU.

## Resource payload analysis

The payload contains an order placed by a customer whose email address is `john.doe@mybrand.com`. This matches the first condition specified in the rule, confirming that the order qualifies for the discount. The order contains 5 line items, 4 of which (`wLGtgmdLjJ`, `zbZtmarXKR1`, `zbZtmarXKR2`, and `aoMtJlQlxD`) are associated with an SKU (`ZXxPSkbpNP`, `WVyPSgRwxd`, `WVyPSgRwxZ`, and `WKjRSMmlxe` respectively).

## Result check

The outcomes reflect the evaluation of the rules against the resource payload. Since the order's customer email satisfies one of the two conditions the order ID is listed within the related `matches` array. This doesn't happen for the second condition, but being the conditions evaluated according to an `or` logic the rule matches successfully anyway (`match` is `true`). The action is applied to the line items associated with an SKU with the decimal value of `0.15` corresponding to the specified percentage discount.

## Conclusion

Once checked that the order's customer email address belongs to the desired domain, the rule successfully identifies the line items eligible for the discount and applies a 15% discount to each qualifying line item.


---

# 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/promotions/get-percentage-discount-by-email.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.
