# Actions

For each rule you need to provide one or more `actions` as an array of action objects.

Actions are triggered based on the provided rule's [conditions](https://docs.commercelayer.io/rules-engine/conditions). Once the combination of conditions are evaluated according to the conditions logic specified at the [rule](https://docs.commercelayer.io/rules-engine/rules) level, if they are satisfied, all the related actions will be applied to the specified resource's fields. Among other effects, actions can modify order totals, apply fixed, percentage, and other types of discounts to specific items, or adjust quantities.

{% hint style="info" %}
Multiple actions (even involving the same resource's field) are applied in the order they are listed within the rule payload.
{% endhint %}

For each action you must define at least its type and the discount value to be applied. Based on the [type of action](https://docs.commercelayer.io/rules-engine/actions/types) some restrictions may apply, for example:

* The list of required parameters may vary.
* The required format/type for the value (if any) may vary.
* Some optional key (e.g. bundle, limit, etc.) may be available or not.

<table><thead><tr><th width="218">Key</th><th width="120">Type<select multiple><option value="arijeabpb8NC" label="Boolean" color="blue"></option><option value="g7gJKAOqAK2z" label="Integer" color="blue"></option><option value="inwV0RuSZ4sl" label="Float" color="blue"></option><option value="46spommEQ7Xc" label="String" color="blue"></option><option value="YKpZTO92O5ie" label="Array" color="blue"></option><option value="PBEkm4riJb6Y" label="Object" color="blue"></option></select></th><th width="248">Description</th><th>Example</th></tr></thead><tbody><tr><td><strong><code>type</code></strong></td><td><span data-option="46spommEQ7Xc">String</span></td><td>The type of action you want to apply (check the <a href="actions/types">list of available types</a>).</td><td><code>"percentage"</code></td></tr><tr><td><strong><code>selector</code></strong></td><td><span data-option="46spommEQ7Xc">String</span></td><td>The resource on which to apply the action (expressed in dot notation). Can be an attribute if you set also the <code>identifier</code> key.</td><td><code>"order.line_items"</code> <code>"order.line_items.id"</code></td></tr><tr><td><strong><code>identifier</code></strong></td><td><span data-option="46spommEQ7Xc">String</span></td><td>An identifier value to address one or more specific instances of the resource based on the attribute defined in the <code>selector</code> key (if any). Cannot be used if the selector is a resource.</td><td><code>"WVyPSgRwxZ"</code></td></tr><tr><td><strong><code>identifiers</code></strong></td><td><span data-option="PBEkm4riJb6Y">Object</span></td><td>Maps selector paths to non-empty arrays of strings representing the IDs to identify eligible free gift line items (SKUs, bundles, or SKU lists). Required for <a href="actions/types/free-gift">free gift</a> actions, not accepted by any other action type.</td><td><code>{ "order.line_items.sku.id": [ "STdLKgTcxG", "xDCVseBgWX"  ] }</code></td></tr><tr><td><strong><code>groups</code></strong></td><td><span data-option="YKpZTO92O5ie">Array</span></td><td>The groups that will be targeted by the action (must be one or more among the ones defined when grouping the matches of the related conditions — required when using <a href="actions/bundle">bundles</a>).</td><td><code>[ "discountable-items", "t-shirts" ]</code></td></tr><tr><td><strong><code>bundle</code></strong></td><td><span data-option="PBEkm4riJb6Y">Object</span></td><td>Additional directives on how to recursively pick a subset of elements from the specified groups.</td><td>Learn more <a href="actions/bundle">here</a>.</td></tr><tr><td><strong><code>aggregation</code></strong></td><td><span data-option="PBEkm4riJb6Y">Object</span></td><td>Additional requirements on the aggregated items targeted by the action.</td><td>Learn more <a href="actions/aggregation">here</a>.</td></tr><tr><td><strong><code>apply_on</code></strong></td><td><span data-option="46spommEQ7Xc">String</span></td><td>The specific field of the selector resource on which to apply the action (available for specific action types only).</td><td><code>"compare_at_amount_cents"</code></td></tr><tr><td><strong><code>discount_mode</code></strong></td><td><span data-option="46spommEQ7Xc">String</span></td><td>The type of distribution of the discount over the selected items. Can be one of <code>default</code> or <code>distributed</code>, default is <code>default</code>  (available for <a href="actions/types/fixed-amount">fixed amount</a> actions only).</td><td><code>"distributed"</code></td></tr><tr><td><strong><code>round</code></strong></td><td><span data-option="arijeabpb8NC">Boolean</span></td><td>Indicates whether the discount will be rounded off to the nearest integer value or not (available for <a href="actions/types/percentage">percentage discount</a> actions only). Default is <code>false</code>.</td><td><code>true</code></td></tr><tr><td><strong><code>quantity</code></strong></td><td><span data-option="g7gJKAOqAK2z">Integer</span></td><td>Overrides the resource quantity in terms of the number of items on which the action will be applied, as long as the specified value is less than or equal to the resource quantity (available for specific action types only). For <a href="actions/types/free-gift">free gift</a> actions, defines the total number of units to discount across all matching line items (default is <code>1</code>).</td><td><code>2</code></td></tr><tr><td><strong><code>limit</code></strong></td><td><span data-option="PBEkm4riJb6Y">Object</span></td><td>Additional restrictions on the number of items on which an action will be applied (available for specific action types only, cannot be used if the <code>bundle</code> key is specified).</td><td>Learn more <a href="actions/limit">here</a>.</td></tr><tr><td><strong><code>value</code></strong></td><td><span data-option="g7gJKAOqAK2z">Integer, </span><span data-option="inwV0RuSZ4sl">Float, </span><span data-option="PBEkm4riJb6Y">Object</span></td><td>The amount or percentage (or any other format required) to be discounted (format and type may vary based on the <a href="actions/types">action type</a>). Not accepted for <a href="actions/types/free-gift">free gift</a> actions — its presence will cause a validation error.</td><td><code>0.2</code><br><code>2500</code><br><code>{ "x": 3, "y": 2 }</code></td></tr></tbody></table>

{% hint style="warning" %}
Referring to the [Core API integration](https://docs.commercelayer.io/rules-engine/readme#core-api-integration), an action `selector` can be a resource or an attribute. If it's a **resource** (i.e. anything but the last leaf of the relationship tree using the dot notation — e.g. `order` or `order.line_items`) the action will be applied on a default field (usually the unit amount of the resource), unless specified differently in the `apply_on` key. If it's an **attribute** (e.g. or `order.line_items.id`), you need to set also the `identifier` key (e.g. `WVyPSgRwxZ`) so that the action will be applied to the specified instance only.
{% endhint %}

{% hint style="info" %}
For those conditions that don't have a group explicitly specified, the UUID autogenerated by the [Core API integration](https://docs.commercelayer.io/rules-engine/readme#core-api-integration) will be automatically set as the group to which apply the action.
{% endhint %}

## Examples

### Selector

The following action applies a 10% discount to all the line items associated with an SKU:

<pre class="language-json"><code class="lang-json">"actions": [
  "type": "percentage",
<strong>  "selector": "order.line_items.sku",
</strong>  "value": 0.1
]
</code></pre>

### Identifier

The following action sets to 1500 cents the unit amount of the line items associated with the SKU identified by the code `XMASMUG1234` only:

<pre class="language-json"><code class="lang-json">"actions": [
  "type": "fixed_price",
<strong>  "selector": "order.line_items.sku.code",
</strong><strong>  "identifier": "XMASMUG1234",
</strong>  "value": 1500
]
</code></pre>

### Apply on

The following action applies a fixed amount discount of 1500 to the total amount (instead of the default unit amount) of all the line items associated with an SKU:

<pre class="language-json"><code class="lang-json">"actions": [
  "type": "fixed_amount",
<strong>  "selector": "order.line_items.sku",
</strong><strong>  "apply_on": "total_amount_cents",
</strong>  "value": 1500
]
</code></pre>

The following action applies a fixed amount discount of 1500 to the total amount (instead of the default unit amount) of the line items associated with the SKU identified by the code `XMASMUG1234` only:

<pre class="language-json"><code class="lang-json">"actions": [
  "type": "fixed_amount",
<strong>  "selector": "order.line_items.sku.code",
</strong><strong>  "identifier": "XMASMUG1234",
</strong><strong>  "apply_on": "total_amount_cents",
</strong>  "value": 1500
]
</code></pre>

{% hint style="info" %}
You can use `apply_on` on [percentage](https://docs.commercelayer.io/rules-engine/actions/types/percentage), [fixed amount](https://docs.commercelayer.io/rules-engine/actions/types/fixed-amount), and [fixed price](https://docs.commercelayer.io/rules-engine/actions/types/fixed-price) actions only.
{% endhint %}
