Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8f73c84
CEXT-5400: Add origData to the event payload
oshmyheliuk Oct 27, 2025
9d1616a
CEXT-5400: Add origData to the event payload
oshmyheliuk Oct 27, 2025
7e2a73e
CEXT-5400: Add origData to the event payload
oshmyheliuk Oct 27, 2025
95492d6
CEXT-5400: Add origData to the event payload
oshmyheliuk Oct 28, 2025
b19380c
CEXT-5400: Add origData to the event payload
oshmyheliuk Oct 28, 2025
f961ea7
CEXT-5400: Add origData to the event payload
oshmyheliuk Oct 29, 2025
4383a7c
Merge branch 'main' into CEXT-5400
oshmyheliuk Oct 29, 2025
16681dc
CEXT-5400: Add origData to the event payload
oshmyheliuk Oct 29, 2025
5c07f81
Merge remote-tracking branch 'oshmyheliuk/CEXT-5400' into CEXT-5400
oshmyheliuk Oct 29, 2025
409fe5a
CEXT-5400: Add origData to the event payload
oshmyheliuk Oct 29, 2025
1891498
Apply suggestion from @keharper
oshmyheliuk Oct 29, 2025
dd63c8a
Apply suggestion from @keharper
oshmyheliuk Oct 29, 2025
565ecce
Apply suggestion from @keharper
oshmyheliuk Oct 29, 2025
5e3402e
Apply suggestion from @keharper
oshmyheliuk Oct 29, 2025
5fc78b9
Apply suggestion from @keharper
oshmyheliuk Oct 29, 2025
e98499e
Apply suggestion from @keharper
oshmyheliuk Oct 29, 2025
987b6de
Apply suggestion from @keharper
oshmyheliuk Oct 29, 2025
615e73f
Apply suggestion from @keharper
oshmyheliuk Oct 29, 2025
9a3a3fb
CEXT-5400: Add origData to the event payload
oshmyheliuk Oct 29, 2025
c9f12b5
Correct badging
keharper Oct 29, 2025
745c130
CEXT-5400: Add origData to the event payload
oshmyheliuk Oct 30, 2025
ecf2e1a
CEXT-5400: Add origData to the event payload
oshmyheliuk Oct 30, 2025
be2ec8c
Add links and fix grammar
keharper Oct 30, 2025
6171340
Merge branch 'main' into CEXT-5400
keharper Oct 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/_includes/conditional-event-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ The following example creates and registers a conditional event named `plugin.ma
* The `category_id` is either `3`, `4`, or `5`
* The product `name` contains `TV`
* The `store_id` of product category is either `1` or `2`
* The `quantity_and_stock_status.qty` value has changed

These fields are present and declared in the parent event.
3 changes: 3 additions & 0 deletions src/_includes/conditional-event.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ Each rule contains the following:
| `equal` | Checks whether the payload value matches the specified value. For Boolean data types, use `1` to compare to `true` and `0` to compare to `false`. |
| `regex` | A regular expression that checks for matches. The specified value must be compatible with the [regular expression match](https://www.php.net/manual/en/function.preg-match.php). |
| `in` | Checks whether the payload value is one of multiple specified values. The value must be a comma-separated list. You do not need to provide additional escape characters. |
| `onChange` | <Edition name="paas" />Checks whether the provided field's value has changed compared to its previous value. The value attribute is optional. If provided, the operator checks whether the field's new value is equal to the specified value field. |

* The value to compare against. When you assign the `regex` operator, you must delimit the regular expression value with valid characters, such as forward slashes (/). For example, `/^TV .*/i`, which checks whether the string starts with the string `TV`, ignoring the case of the letters.

* The value in the `onChange` operator is a path to a field to compare against. By default, comparison is done against `field` and `_origData.field` values. In cases where the event payload has a different structure, you can provide a custom path to compare against.
4 changes: 4 additions & 0 deletions src/data/navigation/sections/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ module.exports = [
title: "Create event subscriptions in a module",
path: "/events/module-development.md",
},
{
title: "Create event subscriptions with original data",
path: "/events/events-original-data.md",
},
{
title: "Create conditional events",
path: "/events/conditional-events.md",
Expand Down
62 changes: 61 additions & 1 deletion src/pages/events/conditional-events.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
title: Create conditional events
description: Create custom Adobe Commerce conditional events using declarative configuration.
edition: paas
keywords:
- Events
- Extensibility
Expand Down Expand Up @@ -50,11 +49,72 @@ You can create conditional events within your module's or root `io_events.xml` f
<operator>in</operator>
<value>1,2</value>
</rule>
<rule>
<field>quantity_and_stock_status.qty</field>
<operator>onChange</operator>
<value />
</rule>
</rules>
</event>
</config>
```

## Trigger events on specific field changes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add an example when the events need to be triggered once qty or price is being changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that we don't have OR rules, all rules are processed as AND.
By adding the next rules:

          <rule>
               <field>quantity_and_stock_status.qty</field>
               <operator>onChange</operator>
               <value />
           </rule>
          <rule>
               <field>price</field>
               <operator>onChange</operator>
               <value />
           </rule>

The event will be triggered only if both price and quantity are changed.

Currently, it's possible to resolve by creating a separate subscription with an Alias name, one for price change and one for quantity change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should we state somewhere that OR rules aren't supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the next definition at the beginning of that page.

If one or more rules evaluate as false, the service sends neither the parent event nor the conditional event, unless the parent has been subscribed separately, without any rules.

Should we mention it one more time?


<Edition name="paas" />

<InlineAlert variant="warning" slots="text"/>

This rule is possible only for events that include original data [`_origData`](./events-original-data.md) in the payload. If an event does not include original data, the `onChange` operator cannot be used, and such an event will not be triggered.

For events that contain original data [`_origData`](./events-original-data.md) within the payload, you can create conditional events that trigger only when specific fields change. It can reduce the number of events sent to your application when only specific field changes are relevant.

To check if the event payload contains original data, you can visit **System** > Events > **Events List** page or use the [`bin/magento events:info <event_code>`](./commands.md#return-event-details) command.

For example, you want to trigger an event only when the product stock quantity changes:

```xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="...">
<event name="plugin.magento.catalog.model.resource_model.product.on_stock_change_event"
parent="plugin.magento.catalog.model.resource_model.product.save">
<fields>
<field name="qty"/>
<field name="category_id"/>
<field name="name"/>
</fields>
<rules>
<rule>
<field>quantity_and_stock_status.qty</field>
<operator>onChange</operator>
<value />
</rule>
</rules>
</event>
</config>
```

In this example the payload value of `quantity_and_stock_status.qty` is compared to its previous value stored in `_origData.quantity_and_stock_status.qty`. The event is triggered only when the quantity value has changed.

For some events the payload structure may differ and the original data [`_origData`](./events-original-data.md) may not be available as a root node. In such cases you can provide a custom path to compare against in the `value` element:

```xml
<event name="observer.checkout_cart_product_add_before">
<fields>
<field name="*"/>
<field name="_origData"/>
</fields>
<rules>
<rule>
<field>product.quantity_and_stock_status.qty</field>
<operator>onChange</operator>
<value>product._origData.quantity_and_stock_status.qty</value>
</rule>
</rules>
</event>
```

In this example the payload value of `product.quantity_and_stock_status.qty` is compared to the value stored in `product._origData.quantity_and_stock_status.qty`.

## Command line

The `bin/magento events:subscribe <event_code> --force --fields=<name1> --fields=<name2>` command creates and registers custom and native Commerce events. When you also specify the `--parent <event_code>` and `--rules=<field-name>|<operator>|<value>` options, you create and register a conditional event.
Expand Down
6 changes: 6 additions & 0 deletions src/pages/events/create-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ Operator: in
Value: 1,2
```

<!--
Field: quantity_and_stock_status.qty
Operator: onChange
Value:
-->

## Events Subscriptions grid actions

Click **Select** > **Edit** in the **Action** column of an event subscription's row to display a form for editing the subscription.
Expand Down
107 changes: 107 additions & 0 deletions src/pages/events/events-original-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
title: Create event subscriptions with original data
description: Use original event data in Adobe Commerce events.
edition: paas
keywords:
- Events
- Extensibility
---

# Create event subscriptions with original data

Events that are based on objects that implement a specific interface in Adobe Commerce can include the original data of the object in the event payload. The original data represents the state of the object before any changes were made. This is particularly useful for events that are triggered by updates to objects, as it allows you to compare the previous state with the new state.

If the object is new (such as when a new product is created), the original data will be empty. Events that support original data also include an `_isNew` field in the payload, which indicates whether the object is new or existing.

To check if an event includes original data in its payload, you can visit **System** > Events > **Events List** page or use the following command [`bin/magento events:info <event_code>`](./commands.md#return-event-details). Keep in mind that that the returned payload for some events, especially those that are dynamically defined, might not include full details.

To include original data in the event payload, the event must be configured to include the `_origData` field even if the wildcard `*` is used to include all fields. For example, if you want to receive the whole payload including original data for the `observer.catalog_product_save_after` event, you can configure it as follows:

<InlineAlert variant="warning" slots="text" />

The `_origData` field is not included by default even when using the wildcard `*`, so you must explicitly add it to the event configuration.

```xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module-commerce-events-client/etc/io_events.xsd">
<event name="observer.catalog_product_save_after">
<fields>
<field name="*"/>
<field name="_origData"/>
</fields>
</event>
</config>
```

As a result, the event payload will include the `_origData` field with the original data of the product before it was saved:

```json
{
"sku": "Simple product",
"name": "Simple product",
"price": "500.000000",
"quantity_and_stock_status": {
"is_in_stock": "1",
"qty": "3126"
},
.....
"_origData": {
"sku": "Simple product",
"name": "Simple product New Name",
"price": "600.000000",
"quantity_and_stock_status": {
"is_in_stock": true,
"qty": 3125
},
.....
},
"_isNew": false
}
```

For some events with a complex payload structure, the original data might not be available as a root node. In such cases, you can provide a custom path to include the original data. For example, for the `observer.checkout_cart_product_add_before` event, you can configure it as follows:

```xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module-commerce-events-client/etc/io_events.xsd">
<event name="observer.checkout_cart_product_add_before">
<fields>
<field name="*"/>
<field name="product._origData"/>
<field name="quote_item._origData"/>
</fields>
</event>
</config>
```

You can define specific original and new fields to be included in the event payload. For example, if you want to include only the `sku` and `quantity_and_stock_status.qty` fields, along with their original values, configure the event as follows:

```xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module-commerce-events-client/etc/io_events.xsd">
<event name="observer.catalog_product_save_after">
<fields>
<field name="sku"/>
<field name="quantity_and_stock_status.qty"/>
<field name="_origData.sku"/>
<field name="_origData.quantity_and_stock_status.qty"/>
</fields>
</event>
</config>
```

As a result, the event payload will include only the selected fields and their original values:

```json
{
"sku": "Simple product",
"quantity_and_stock_status": {
"qty": "3126"
},
"_origData": {
"sku": "Simple product",
"quantity_and_stock_status": {
"qty": 3125
}
}
}
```

The original data can be used in conditional events using the `onChange` operator to trigger events only when specific fields have changed. For more information, see [Create conditional events](./conditional-events.md#trigger-events-on-specific-field-changes).