# Discount items with large stock availability

This rule applies a discount to specific line items in an order based on their quantity. The inventory of all the order's line item associated with an SKU is checked and a percentage discount is applied to those whose stock quantity is greater than or equal to a specific value.

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 5% on items that have a big stock",
      "conditions": [
        {
          "field": "order.line_items.sku.inventory.quantity",
          "matcher": "gteq",
          "value": 100,
          "group": "discountable-items"
        }
      ],
      "actions": [
        {
          "type": "percentage",
          "selector": "order.line_items",
          "groups": [
            "discountable-items"
          ],
          "value": 0.05
        }
      ]
    }
  ]
} 
```

{% endtab %}

{% tab title="Order" %}

```json
{
  "order": {
    "id": "oKkhYLlzgE",
    "line_items": [
      {
        "quantity": 1,
        "id": "gnYtPpKLeG"
      },
      {
        "quantity": 1,
        "id": "wLGtgmdLjJ",
        "total_amount_cents": 22000,
        "sku": {
          "id": "ZXxPSkbpNP",
          "inventory": {
            "quantity": 100
          }
        }
      },
      {
        "quantity": 1,
        "id": "qLGtgmdLjC",
        "total_amount_cents": 10000,
        "sku": {
          "id": "HXxPSkbpNA",
          "inventory": {
            "quantity": 200
          }
        }
      },
      {
        "quantity": 1,
        "id": "aLGtgmdLjB",
        "total_amount_cents": 9000,
        "sku": {
          "id": "gXxPSkbpNY",
          "inventory": {
            "quantity": 50
          }
        }
      }
    ]
  }
}
```

{% endtab %}

{% tab title="Outcomes" %}

```json
[
  {
    "id": "a6060404-4760-40bf-875e-2d263f215bbd",
    "name": "Discount 5% on items that have a big stock",
    "priority": 0,
    "match": true,
    "conditions_logic": "and",
    "conditions": [
      {
        "field": "order.line_items.sku.inventory.quantity",
        "matcher": "gteq",
        "value": 100,
        "group": "discountable-items",
        "match": true,
        "matches": [
          {
            "order": "oKkhYLlzgE",
            "line_item": "wLGtgmdLjJ",
            "sku": "ZXxPSkbpNP",
            "group": "discountable-items"
          },
          {
            "order": "oKkhYLlzgE",
            "line_item": "qLGtgmdLjC",
            "sku": "HXxPSkbpNA",
            "group": "discountable-items"
          }
        ],
        "scope": "any"
      }
    ],
    "actions": [
      {
        "resources": [
          {
            "resource_type": "line_items",
            "id": "wLGtgmdLjJ",
            "group": "discountable-items",
            "quantity": 1,
            "value": 0.05,
            "action_type": "percentage"
          },
          {
            "resource_type": "line_items",
            "id": "qLGtgmdLjC",
            "group": "discountable-items",
            "quantity": 1,
            "value": 0.05,
            "action_type": "percentage"
          }
        ]
      }
    ]
  }
]
```

{% endtab %}
{% endtabs %}

## Rules breakdown

The desired results can be achieved with a single rule.

#### Conditions

The only condition in the rule checks if the order contains line items associated with SKUs whose stock quantity is greater than or equal to `100`. The line items that match the condition are grouped as `discountable-items`.

#### Actions

The only action in the rule applies a 5% discount to the order's line items that match the condition (i.e. the ones belonging to the `discountable-items` group).

## Resource payload analysis

The provided payload contains an order with 4 line items 3 of which are associated with an SKU. 2 of those SKUs (`ZXxPSkbpNP` and `HXxPSkbpNA`, associated with the line items `wLGtgmdLjJ` and `qLGtgmdLjC`) have a `quantity` that matches or exceeds the desired threshold (`200` and `100` respectively), thus qualifying for the discount.

## Outcomes check

The outcomes reflect the evaluation of the rules against the resource payload. Since the order contains at least one line item that satisfies the condition, the rule matches successfully (`match` is `true`). The two line item IDs (together with the IDs of the related SKUs and the ID of the order) to which the discount is applied are listed within the `matches` array. The action is applied to those line items with the decimal value of `0.05` corresponding to the specified percentage discount.

## Conclusion

The rule successfully identifies the line items eligible for the discount and applies a 5% discount to each qualifying line item.
