Commerce module development

This topic describes how to enable your custom modules for Adobe I/O Events. You can also manually register observer events using the events:subscribe command.

Find supported events

Adobe Commerce is capable of emitting thousands of different observer and plugin events, but most of them aren't good candidates for integrating with an external App Builder application. For example, Commerce emits events before and after a customer address is loaded, saved, or deleted, but the only events of consequence are those that indicate a change of status after the address is saved or deleted.

You can use the Commerce Admin or the command line to find supported events and their payloads:

Register events

You can programmatically register events using the following methods:

data-src=/_includes/sample-event.md

After you've registered at least one event, run the eventsmodule command to generate the required plugins.

io_events.xml

Create the <module-root>/etc/io_events.xml or app/etc/io_events.xml file and define a list of events that should always be transmitted. Events listed in this file cannot be disabled with the events:unsubscribe command.

You can transmit all the fields within an event by setting the value of the field element to * (<field name="*" />). You cannot use the * wildcard character to match partial strings.

data-src=/_includes/event-size-limitation.md

Add custom fields to an event describes how to enhance the payload of pre-defined events.

<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="entity_id" />
            <field name="sku" />
            <field name="is_new" />
        </fields>
    </event>
    <event name="plugin.magento.sales.api.invoice_item_repository.save">         
        <fields>
            <field name="entity_id" />
            <field name="parent_id" />
            <field name="base_price" />
            <field name="tax_amount" />
            <field name="base_row_total" />
            <field name="discount_amount" />
            <field name="qty" />
        </fields>
    </event>
    <event name="plugin.magento.catalog.model.resource_model.product.save">
        <fields>
            <field name="entity_id" />
            <field name="sku" />
            <field name="name" />
            <field name="price" />
            <field name="created_at" />
        </fields>
    </event>
</config>

The contents of an observer.catalog_product_save_after event are similar to the following:

{
    "value": {
    "entity_id": "3",
    "sku": "test2",
    "is_new": "0"
  }
}

The <field> element can also contain the converter attribute. Use this attribute to change the value of a field in the event payload. Convert payload field values describes its usage.

Array of nested objects

data-src=/_includes/nested-event.md
<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.sales_order_invoice_save_after">
      <fields>
         <field name="order_id" />
         <field name="items[].sku" />
         <field name="items[].qty" />
      </fields>
   </event>
</config>

The contents of the event are similar to the following:

{
   "order_id": "8",
   "items": [
      {
         "sku": "simple-product-2",
         "qty": "3.000000"
      },
      {
         "sku": "simple-product-1",
         "qty": "5.000000"
      }
   ]
}

config.php and env.php

You can also create an io_events section in the Commerce app/etc/config.php file. As of version of 1.11.0 of Adobe I/O Events for Adobe Commerce, you can also create an io_events section in the Commerce app/etc/env.php file. Events registered using this mechanism can be disabled from the command line.

For example:

'io_events' => [
    'observer.catalog_product_save_after' => [
        'fields' => [
            'entity_id',
            'sku',
            'is_new',
        ],
        'enabled' => 1
    ],
    'plugin.magento.sales.api.invoice_item_repository.save' => [
        'fields' => [
            'entity_id',
            'parent_id',
            'base_price',
            'tax_amount',
            'base_row_total',
            'discount_amount',
            'qty',
        ],
        'enabled' => 1
    ],
    'plugin.magento.catalog.model.resource_model.product.save' => [
        'fields' => [
            'sku',
            'entity_id',
            'name',
            'price'
        ],
        'enabled' => 1
    ],
]

The payload for the observer.catalog_product_save_after event is the same as shown in io_events.xml.

Configuration merging from different modules

If multiple Commerce modules register the same event, the configuration manager merges the two configurations. The transmitted event includes all the fields defined in the modules.

In the following example, the MODULE1/etc/io_events.xml file registers the observer.catalog_product_save_after event with the entity_id and sku fields.

<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="entity_id" />
            <field name="sku" />
        </fields>
    </event>
</config>

The MODULE2/etc/io_events.xml file registers the same event with the entity_id field and two others.

<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="entity_id" />
            <field name="quantity_and_stock_status.qty" />
            <field name="stock_data.min_qty" />
        </fields>
    </event>
</config>

The resulting configuration:

<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="entity_id" />
            <field name="sku" />
            <field name="quantity_and_stock_status.qty" />
            <field name="stock_data.min_qty" />
        </fields>
    </event>
</config>

Mask sensitive data in event payloads

data-slots=text
data-backgroundcolor=blue
PaaS Only

Before Commerce sends an event, it masks the values of payload fields that might contain sensitive data, such as passwords, tokens, and payment card details. Masked values are replaced with ******. Masking applies to events built from plugin method arguments, observer event data, and resource model events. String values that contain a JSON object or array are decoded and masked using the same rules.

Keys are matched case-insensitively, and non-alphanumeric characters are ignored. For example, apiKey, api_key, and API-KEY are treated as the same key. Keys that name an identifier, such as payment_token_id or paymentTokenId, are not masked by key fragments. Boolean values are never masked.

The Magento\AdobeCommerceEventsClient\Event\PluginPayload\ArgumentPayloadSanitizer class defines the following lists of keys in the AdobeCommerceEventsClient module's di.xml file:

Argument
Description
Default values
sensitiveKeyFragments
Masks the scalar value of any key that contains one of the fragments. Nested arrays are sanitized rather than replaced.
access_key, api_key, api_signature, card_number, cc_number, credential, cvv, gift_card_code, insert_key, license_number, passwd, password, private_key, secret, token
sensitiveKeys
Masks the scalar value of keys that exactly match one of the values.
cid, confirmation, otp, pin, pwd
maskedWholeKeyFragments
Masks the whole value of any key that contains one of the fragments, even when the value is an array.
config_data, serialized_data, workspace_configuration
notSensitiveKeys
Never masks these keys. Use this list to prevent false positives from key fragments.
rp_token_created_at

To customize masking, add items to these arguments in the etc/di.xml file of your module. To stop masking a default key, override its item with xsi:type="null". The following example masks keys that contain ssn and stops masking the pin key:

<type name="Magento\AdobeCommerceEventsClient\Event\PluginPayload\ArgumentPayloadSanitizer">
    <arguments>
        <argument name="sensitiveKeyFragments" xsi:type="array">
            <item name="ssn" xsi:type="string">ssn</item>
        </argument>
        <argument name="sensitiveKeys" xsi:type="array">
            <item name="pin" xsi:type="null"/>
        </argument>
    </arguments>
</type>

Your module must load after the Magento_AdobeCommerceEventsClient module. Add Magento_AdobeCommerceEventsClient to the <sequence> node of your module's etc/module.xml file.