# Every

This alternative bundling strategy maximizes the number of items picked from the targeted group (listed according to the desired sorting logic) ensuring that it is equal to or a multiple of a specified number.

{% hint style="warning" %}
Bundles of type `every` can be created only from **one group** containing at least one item. If the related `actions.groups` array is made by more than one group, the Rule Engine will return an error. If the specified group is empty, the action won't be applied.
{% endhint %}

<table><thead><tr><th width="137">Key</th><th width="114">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="114" data-type="checkbox">Required</th><th width="338">Notes</th></tr></thead><tbody><tr><td><strong><code>type</code></strong></td><td><span data-option="sk9tHN8WC8KO">String</span></td><td>true</td><td>Must be <code>"every"</code>.</td></tr><tr><td><strong><code>sort</code></strong></td><td><span data-option="MFocCrV7v0L6">Object</span></td><td>true</td><td>Learn more <a href="/pages/GhfyTAymk4S8RYeOrEeX#sort">here</a>.</td></tr><tr><td><strong><code>value</code></strong></td><td><span data-option="VAIMp1rdLBI0">Integer</span></td><td>true</td><td></td></tr></tbody></table>

## How the `every` algorithm works

1. The items of the targeted group are sorted according to the `sort.direction` function applied to the `sort.attribute` numeric field.
2. The total quantity of units within the group is calculated by adding up the quantities of each item: `Q = q1 + q2 + ... + qn`
3. Being `M` the maximum multiple of the number `V` specified in the bundle's `value` key that doesn't exceed the total quantity `Q`, the number of units that won't be discounted `X` is calculated by subtracting `M` from `Q`: `X = Q - M = Q mod V`
4. Starting from the bottom of the ordered list generated by the sorting, a quantity of units equal to `X` is removed to get the final list of items that will be discounted.

{% hint style="info" %}
If the number specified in the `value` key is already a multiple of the total quantity (i.e. `M = Q` and consequently `X = 0`), the action will be applied to all the items of the group.
{% endhint %}

{% hint style="warning" %}
If some items of the group match the bundle's `sort.direction` function with the same numeric value of the field specified in the `sort.attribute` value, they will be sorted as they are listed in the payload against which the rule is evaluated.
{% endhint %}

## Example

Let's assume that a rule's conditions define the `discountable-items` group of line items as follows:

<pre class="language-json"><code class="lang-json">[
  {
    "id": "qOYocnANsO",
    "type": "line_items",
<strong>    "quantity": 2,
</strong><strong>    "unit_amount_cents": 2000,
</strong>    "total_amount_cents": 4000,
    "sku": {
      "id": "PeHfayCvwQ",
<strong>      "code": "HAT"
</strong>    }
  },
  {
    "id": "nlHjpkVpCG",
    "type": "line_items",
<strong>    "quantity": 3,
</strong><strong>    "unit_amount_cents": 1000,
</strong>    "total_amount_cents": 3000,
    "sku": {
      "id": "pHaoUAvTVy",
<strong>      "code": "STICKER"
</strong>    }
  },
  {
    "id": "DtZjSMEKvm",
    "type": "line_items",
<strong>    "quantity": 2,
</strong><strong>    "unit_amount_cents": 3000,
</strong>    "total_amount_cents": 6000,
    "sku": {
      "id": "TkhHRotCOA",
<strong>      "code": "TSHIRT"
</strong>    }
  }
]
</code></pre>

Let's consider the following action that applies a 10% discount to the group above. The discount will be applied to bundles built by picking the items 2 at a time and prioritizing the ones with higher unit amounts:

<pre class="language-json"><code class="lang-json">"actions": [
  {
    "type": "percentage",
    "selector": "order.line_items.sku",
<strong>    "groups": [ "discountable-items" ],
</strong>    "bundle": {
<strong>      "type": "every",
</strong>      "sort": {
<strong>        "attribute": "unit_amount_cents",
</strong><strong>        "direction": "desc"
</strong>      }
<strong>      "value": 2
</strong>    },
    "value": 0.1
  }
]
</code></pre>

### 1. Sorting

The items of the group are sorted based on their unit amount, from the highest to the lowest:

<table><thead><tr><th>Item<select><option value="VSoeYxLP5ogG" label="TSHIRT" color="blue"></option><option value="pgdBGI5a7Lib" label="HAT" color="blue"></option><option value="iMO3gKoox4L8" label="STICKER" color="blue"></option></select></th><th data-type="number">Quantity</th><th>Unit amount</th></tr></thead><tbody><tr><td><span data-option="VSoeYxLP5ogG">TSHIRT</span></td><td>2</td><td>3000</td></tr><tr><td><span data-option="pgdBGI5a7Lib">HAT</span></td><td>2</td><td>2000</td></tr><tr><td><span data-option="iMO3gKoox4L8">STICKER</span></td><td>3</td><td>1000</td></tr></tbody></table>

### 2. Total quantity of units

The total quantity of units within the group is `Q = 2 + 3 + 2 = 7`.

### 3. Number of units to be removed

Being the number of the `bundle.value` key `2`, the number of units to be removed is calculated as the remainder of the division between the total quantity and the value: `7 mod 2 = 1`.

### 4. Items to be discounted

One item will be removed starting from the bottom of the [ordered list](#id-1.-sorting) (i.e. one sticker), thus getting to the final outcome, resulting in a 10% discount applied to 2x bundles, not to the additional item with lower unit price:

<table><thead><tr><th>SKU<select><option value="VSoeYxLP5ogG" label="TSHIRT" color="blue"></option><option value="pgdBGI5a7Lib" label="HAT" color="blue"></option><option value="iMO3gKoox4L8" label="STICKER" color="blue"></option></select></th><th data-type="number">Quantity</th><th>Discounted unit amount</th></tr></thead><tbody><tr><td><span data-option="VSoeYxLP5ogG">TSHIRT</span></td><td>2</td><td>5400</td></tr><tr><td><span data-option="pgdBGI5a7Lib">HAT</span></td><td>2</td><td>3600</td></tr><tr><td><span data-option="iMO3gKoox4L8">STICKER</span></td><td>2</td><td>1800</td></tr></tbody></table>


---

# 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/actions/bundle/every.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.
