# Every X discount Y

Actions of type `every_x_discount_y` enable you a fixed amount discount of value `y` on the selected items for each interval defined by the value `x` on the amount of a desired field. The total discount is proportionally distributed based on the quantity of the selected items.

{% hint style="warning" %}
If X is greater than the amount on which the intervals are supposed to be calculated, the action won't have any effect.
{% endhint %}

{% hint style="info" %}
The action applies even when the `attribute` value isn't an *exact* multiple of X but just greater than. The discount is proportionally distributed based on the targeted items' quantity.&#x20;
{% endhint %}

This way, if the value of the specified field is `n` (where `n` is greater than or equal to `m*x` and less than `(m+1)*x`, with `m` greater than zero) the total discount applied will be `m*y`.

When setting up a every X discount Y action, in addition to the action `type`, you need to specify at least `value` object as described [here](#value) (for more information on the parameters listed in the table below and how they work please refer to the main [actions page](https://docs.commercelayer.io/rules-engine/actions)):

<table><thead><tr><th>Key</th><th>Type<select multiple><option value="YGgpISUGMdPQ" label="Boolean" color="blue"></option><option value="QePOb3JPIkBd" label="Integer" color="blue"></option><option value="rSmUinI3meTE" label="Float" color="blue"></option><option value="ytg3YoHxHpB2" label="String" color="blue"></option><option value="rlTqes6yAIME" label="Array" color="blue"></option><option value="qhQqnuEfMsvz" label="Object" color="blue"></option></select></th><th data-type="checkbox">Required</th><th>Notes</th></tr></thead><tbody><tr><td><strong><code>type</code></strong></td><td><span data-option="ytg3YoHxHpB2">String</span></td><td>true</td><td>Must be <code>"every_x_discount_y"</code>.</td></tr><tr><td><strong><code>selector</code></strong></td><td><span data-option="ytg3YoHxHpB2">String</span></td><td>true</td><td></td></tr><tr><td><strong><code>identifier</code></strong></td><td><span data-option="ytg3YoHxHpB2">String</span></td><td>false</td><td>Can be set only if the <code>selector</code> key value is an attribute.</td></tr><tr><td><strong><code>groups</code></strong></td><td><span data-option="rlTqes6yAIME">Array</span></td><td>false</td><td></td></tr><tr><td><strong><code>aggregation</code></strong></td><td><span data-option="qhQqnuEfMsvz">Object</span></td><td>false</td><td></td></tr><tr><td><strong><code>value</code></strong></td><td><span data-option="qhQqnuEfMsvz">Object</span></td><td>true</td><td></td></tr></tbody></table>

### Example

The following action applies a 5000 cents discount every 30000 cents of the total amount of the order, proportionally distributing the total resulting discount on the line items associated with an SKU belonging to the related condition's matchers grouped as `discountable-items`:

<pre class="language-json"><code class="lang-json">"actions": [
  {
<strong>    "type": "every_x_discount_y",
</strong><strong>    "selector": "order.line_items.sku",
</strong>    "groups": [ "discountable-items" ],
<strong>    "value": {
</strong><strong>      "x": 30000,
</strong><strong>      "y": 5000,
</strong><strong>      "attribute": "total_amount_cents"
</strong><strong>    }  
</strong>  }
]
</code></pre>

Considering some real number:

* If the total amount of the order is 60000 cents and the order contains two products — both with quantity 1 for a total of 2 units — the total discount applied will be `(60000 / 30000) * 5000 = 10000` cents, equally distributed on the two line items (`10000 / 2 = 5000` cents each).
* If the total amount of the order is 90000 cents and the order contains two products — one with quantity 2 and the other with quantity 1 for a total of 3 units — the total discount applied will be `(90000 / 30000) * 5000 = 15000` cents, resulting in a `15000 / 3 = 5000` cents discount per units, proportionally distributed on the two line items (`5000 * 2 = 10000` cents on the one with quantity 2, `5000 * 1 = 5000` cents on the other).
* If the total amount of the order is 140000 cents and the order contains three products — one with quantity 5, another with quantity 3, and the other with quantity 2, for a total of 10 units — the total discount applied will be `(140000 / 30000) * 5000 = 20000` cents, resulting in a `20000 / 10 = 2000` cents discount per unit, proportionally distributed on the three line items (`2000 * 5 = 10000` cents on the one with quantity 5, `2000 * 3 = 6000` cents on the one with quantity 3, `2000 * 2 = 4000` cents on the one with quantity 2).

{% hint style="warning" %}
Please note that when calculating the number of intervals on which to apply the `y` discount, we always consider only the quotient of the division between the numeric value of the field specified in the `attribute` and `x` (skipping the remainder when the former isn't an exact multiple of the latter).
{% endhint %}

## Value

The `x` and `y` values of a buy X pay Y are defined within the `value` object where you can also optionally set a limit on the number of items to consider for the calculation:

<table><thead><tr><th width="238">Key</th><th width="145">Type<select><option value="Djzo1UhBKQfW" label="Boolean" color="blue"></option><option value="VAIMp1rdLBI0" label="Integer" color="blue"></option><option value="qhaoRE82Rs3C" label="Float" color="blue"></option><option value="sk9tHN8WC8KO" label="String" color="blue"></option><option value="B3M67zaVXhvQ" label="Array" color="blue"></option><option value="MFocCrV7v0L6" label="Object" color="blue"></option></select></th><th width="115" data-type="checkbox">Required</th><th>Description</th></tr></thead><tbody><tr><td><strong><code>x</code></strong></td><td><span data-option="VAIMp1rdLBI0">Integer</span></td><td>true</td><td>The number (usually an amount in cents) that defines the segmentation in intervals.</td></tr><tr><td><strong><code>y</code></strong></td><td><span data-option="VAIMp1rdLBI0">Integer</span></td><td>true</td><td>The amount in cents to be discounted for each interval.</td></tr><tr><td><strong><code>attribute</code></strong></td><td><span data-option="sk9tHN8WC8KO">String</span></td><td>true</td><td>The field of the main resource (at the moment, <code>orders</code>, since this action is available for <a href="../../core-api-integration/order-rules">order rules</a> only) on which to calculate the intervals, based on the <code>x</code> key (must be numeric).</td></tr></tbody></table>

## Restrictions

{% hint style="warning" %}
You cannot use [bundles](https://docs.commercelayer.io/rules-engine/actions/bundle), set a [limit](https://docs.commercelayer.io/rules-engine/actions/limit), or [target a specific attribute](https://docs.commercelayer.io/rules-engine/actions/..#apply-on) on which to apply the action when setting up every X discount Y actions.
{% endhint %}

## Resources

{% hint style="info" %}
As regards the Rule Engine [integration with Commerce Layer Core API](https://docs.commercelayer.io/rules-engine/readme#core-api-integration), the every X discount Y action type is available for [promotions](https://docs.commercelayer.io/rules-engine/resources/promotions) ([order rules](https://docs.commercelayer.io/rules-engine/core-api-integration/order-rules)) only.
{% endhint %}
