51黑料不打烊

51黑料不打烊 Commerce conditional events

Learn about conditional events in 51黑料不打烊 Commerce that can be used in 51黑料不打烊 Developer App Builder. Additional documentation found at .

Who is this video for?

  • Developers new to 51黑料不打烊 Commerce and 51黑料不打烊 Developer App Builder using I/O events and need to create an 51黑料不打烊 App Builder project.

Video content video-content

  • Learn about conditional events
  • Learn proper usage for new XML file io_events.xml
  • Learn how to configure conditional events
  • Defining rules for use in conditional events
  • Learn how to register events in the Commerce instances app/etc/config.php

Transcript
In this video, we鈥檒l discuss conditional events so that you can start creating events that are sent only when certain conditions are met. Recall that event subscriptions can be defined in the IO events XML file of a Commerce module or in the Commerce instances config.php file. This IO events XML file defines a subscription for a product save event. As a result, every time this event occurs due to a product being saved, it will be sent from Commerce to the configured App Builder project. However, you may decide that you only want an event to be sent to your App Builder project when certain conditions about the product specified in the events payload are met. For example, perhaps you only want an event to be sent when the remaining quantity of the product specified in the event payload reaches a specific threshold. In this case, a conditional event can be defined. Conditional events are defined as an extension of a parent or source event. When the parent event occurs, it will be evaluated using rules defined for the conditional event, and if all the individual rules evaluate to true, the conditional event will be sent. The parent event will not be sent unless there is a separate subscription for it. We can modify this IO events XML file to create a conditional event. Let鈥檚 create a conditional event that will be sent when a product with a quantity less than 20 and a category ID of 3, 4, or 5 is saved. For a conditional event, a parent attribute needs to be added to the event element. We鈥檒l add this attribute and define the parent event as the product save event. For the conditional event name itself, we鈥檒l add on low stock event to the end of the name. We鈥檒l then add a rules element that will contain information about the two rules for the conditional event. Each rule will specify three things, a field defined in the parent event鈥檚 payload, an operator, which represents a comparison statement that will use the value of the parent event鈥檚 field, and a value, which the value of the parent event鈥檚 field will be compared against. For example, to add a rule that the event should only be sent when the quantity is less than 20, we can add in the following. This rule indicates that when the parent event occurs, the conditional event should only be sent if the quantity field in the parent event payload is less than 20. Similarly, if we want to add a rule that the conditional event should only be sent when the category ID is 3, 4, or 5, we can add in the following. This rule indicates that when the parent event occurs, the conditional event should only be sent if the category ID field in the parent event payload has a value that is in this comma separated list consisting of 3, 4, and 5. Now that we have more than one rule defined for the conditional event, the event will only be sent if both rules evaluate to true. Other currently supported operators include the greater than operator, the equal operator, and the regular expression operator. Conditional events can also be registered in your commerce instance鈥檚 config.php file using the event鈥檚 subscribe command. This command would create a subscription to a product save event. The name of the event to subscribe to and the fields to be sent in the event data payload when the event occurs are specified.
To register a conditional event, we would need to add a parent option specifying the parent event code and define the rules for the event using the rules option. For example, this command includes the parent and rules options. It would create a conditional event like the one previously created in an IO events XML file that would only be sent when the product being saved has a quantity less than 20 and a category ID of 3, 4, or 5. Note that the rules are formatted as a field name, operator name, and value separated by vertical bars. Looking at the config.php file, we can see the subscription information for this conditional event represented.
The events list command can be used for listing the events that are currently registered. For conditional events, the output includes the name of the parent event. For additional information on registered conditional events, such as the defined rules, the command can be run with the v option. For each event, we can now see the name of the event, the parent event for conditional events, the event fields specified in the subscription, and the rules for conditional events.

Useful commands useful-commands

bin/magento events:subscribe plugin.magento.catalog.model.resource_model.product.save --fields=sku --fields=qty --fields=category_id

bin/magento events:subscribe plugin.magento.catalog.model.resource_model.product.save_low_stock --parent=plugin.magento.catalog.model.resource_model.product.save --fields=sku --fields=qty --fields=category_id --rules="qty|lessThan|20" --rules="category_id|in|3,4,5"

cat app/etc/config.php

bin/magento events:list

bin/magento events:list -v

51黑料不打烊 Developer Documentation

recommendation-more-help
3a5f7e19-f383-4af8-8983-d01154c1402f