An Introduction To Magento 2 Events and Observers

by Fayyaz Khattak  January 15, 2018

Working with events and observers is one of the ways to extend the core functionalities of the Magento Platform. Magento 2 provides the same events and observers customization options as Magento 1. The implementation of the events and observers in Magento 2 is based on the Publish-Subscribe pattern. Using Magento 2 events and observers, you can create and run custom code in response to a specific native or custom Magento event.

Magento 2 Observer

Magento 2 Events

Events are dispatched by Magento 2 modules whenever specific actions are triggered. When an event dispatches, it passes data to the observer that is configured to watch (or monitor) that event. You can dispatch Magento 2 Events using the Magento\Framework\Event\Manager class. This class can be obtained through dependency injection by defining the dependency in the constructor method of the class.

To dispatch an event, you have to call the dispatch function of the event manager class, providing the name of the event along with an array of data that you wish to deliver to observers.

Magento 2 Observers

Observers are the particular classes that control the general behavior, performance, or change in the business logic of the store. They are executed whenever a specific event for which they were set to listen is triggered.

To create an observer in Magento 2, you must place your class file under the ModuleRoot/Observer directory. The observer class file should use Magento\Framework\Event\Observer and Magento\Framework\Event\ObserverInterface class and define the executive function.

Cloudways Magento Hosting For Developers

Using Events and Observers in Magento 2

Magento 2 can execute the multiple numbers of observers for a single event trigger.

In this introductory article, I will discuss the use of Magento 2 events and observers. In addition, I will demonstrate the modification of the Product Name only on the product view page using Magento 2 events and observers.

Let’s start by creating a simple module with the namespace Cloudways and the module name EventsObservers. Create registration.php in Magento2Root/app/code/Cloudways/EventsObservers directory and add the following code to it:

Now, create a module.xml file in the Cloudways/EventsObservers/etc directory and add the following code to it:

Magento 2 uses Area Definition to manage the store files. It provides three different areas to create configuration files for the events:

Admin area: Namespace/ModuleName/etc/adminhtml/events.xml

Frontend area: Namespace/ModuleName/etc/frontend/events.xml

Global area: Namespace/ModuleName/etc/events.xml

In this example, since I want to make changes to the Product Name displayed in the product view, I will use the frontend area. Create events.xml file in Cloudways/EventsObservers/etc/frontend directory and add the following code:

The above XML code is listening for the catalog_controller_product_view event. Note that the name and instance attributes are in the <observer> element. The name attribute defines the Observer Name that must be unique for each event, and the instance attribute defines the specific Magento 2 class that needs to be executed when the catalog_controller_product_view event is dispatched.

Now, create an Observer file Data.php in Cloudways/EventsObservers/Observer/Product directory and add the following code to it:

So, what’s going on in the above file? Let’s find out:

  • namespace Cloudways\EventsObservers\Observer\Product is the namespace, which maps through the folder structure of our Magento 2 module.
  • use Magento\Framework\Event\Observer and use Magento\Framework\Event\ObserverInterface are the classes I am using in the observer class.
  • class Data implements ObserverInterface is the class, which is same as the file name. It implements the ObserverInterface class defined above.
  • public function execute(Observer $observer) is the execute() method. It takes the Observer class as an argument. Further, in the code, I get the product object through the magic method: getProduct().  Then, I get the product name, modify it and finally set the product name using setName() method on the product class.

Now once you’re done, run the following CLI command to apply the changes:

Before implementing the code, the product view looks like:

Product View

Once the Observer is triggered, the result will be something like this:

Magento Observer Trigger

As the observer is listening for the catalog_controller_product_view event, it will only make changes to the product view. Thus, the category view will remain unchanged (default):

Magento Observer View

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways – A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at

Stay Connected:

You Might Also Like...