How to Get a list of all observers of specific events in Magento 2

How to get list of all observers of specific event in magento 2

Hello Devs, In this tutorial we will see how to get a list of all observers of specific events in Magento 2 programmatically.

Suppose you are debugging some code and you get a requirement to find all observers which are triggered when a specific event occurs.

Suppose that, you want to find the list of observers of the “sales_order_place_after” event.

In this situation, you can simply get all the observer lists using the below snippet code.

In Magento 2, You can get observers by event name using “Magento\Framework\Event\Config” class.

We can use getObservers() method to fetch an array of all the observers by passing the event name.

Here is the example code you can use :

Then you can call this function by passing the event name in an argument like below.

If you print the data, It will return the array list of all observers in the below format.

You will also love reading this :

Store Emulation in Magento 2.

That’s it for this tutorial. Thank you for reading. Happy coding !!

Leave a Reply

Your email address will not be published. Required fields are marked *