In this tutorial, you will learn how to hook into the Episerver Commerce V8 event handlers. A common requirement on an Episerver Commerce project is to trigger some custom code when certain actions within Commerce manager occurs. For example:

  • Reindex the search when a new product/variant is published

  • Call a third-party API, like update a PIM or pricing service

  • Add custom validation to prevent a product from being saved under certain circumstances

In today's guide, we are going to cover the basics of doing this in Episerver Commerce 8, using the CatalogEventListenerBase. If this sounds interesting to you, read on 🔥🔥🔥

Catalog Event Listener

The CatalogEventListener is a new API released with Episerver Commerce 8. CatalogEventListener provides a mechanism to listen to all the events Episerver fires whenever a change is made within the catalogue. To start hooking into these events you need to create a new class and extend from the CatalogEventListenerBase and then decorate your new class with the ServiceConfiguration attribute with the typeof property set as CatalogEventListenerBase, like so:

You can then hook into the events that you are interested in. Using the code below:

When Episerver raises an event, this method will be triggered and your custom code run. Personally, I do not think this is the most developer-friendly API I've worked with. Instead of working with typed objects as you do in Episerver CMS, the Commerce API works on a much lower abstraction level.

When you work with the CatalogEvent entities you will be working in the DataTables and DataRows level. These objects can be tricky to figure out. Especially determining, where you need to get your data from, however, with a bit of persistence you can hopefully survive. After you have your CustomCatalogEventListenerBase set up, you can think about events you want to hook into.

The two most common events that you will likely hook into are the publishing event and the relation events. You can see the complete list of available Commerce events here. For this tutorial, we will deep dive into publishing and relation events 💥

Publish Event: To hook into either the save/publish events, you need to override the EntryUpdating event. This uses the CatalogEntryDto as the source object. The below snippet ensures the variant is set using the first value it was published with:

You can get the published contents data, using this code:

Typing objects correctly type is a really important code standard to follow. If you work with a variant and you do not type it to VariationContent, you won't be able to use any of the VariationContent specific properties.

You can use RowState to determine if the item being saved already exists within the database, or, if it is new.

Relation Updating Event: This event is useful if you want to add validation when content editors move products around the catalogue. In Episerver Commerce, new associations can be made without the EntryUpdating event being triggered. If you want to hook into the relation updating events use the RelationUpdating event. This uses CatalogRelationDto as the source object:

You now know how to hook into Episerver Commerce's catalogue events. Using the CustomCatalogEventListenerBase you can listen out for all the catalogues, nodes, and products events. By overriding CustomCatalogEventListenerBase and creating your own implementation you can hook into events and trigger your own custom code. Happy Coding 🤘