What is observer in Magento 2?

Magento 2 observer is a certain type of class that can impact performance, general behavior or change business logic. Acting like attentive listeners, observers keep an eye out for specific events happening within the Magento system. Whenever one of these events occurs, the observers spring into action and execute tasks that were assigned to them. In simple terms, observers are ready to react whenever something important happens in the system.The most common Magento 2 observer example is notification email, that might be sent when a new order is placed, or customer’s information was updated. 

Magento 2 Events

Observers always go together with events. When certain actions are triggered, events are dispatched. You can create custom event by yourself, and it will be dispatched in your code. When an event is triggered, it has the capability to deliver data to any observers that are set up to monitor that event.

How to create an event?

Create registration.php in Magento/Framework/Amasty/EventsObservers directory and add the following code to it:

<?php
\Magento\Framework\Component\ComponentRegistrar::register
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Amasty_EventsObservers',
    __DIR__
);

Then head to your module's directory and create a new directory named "Observer". Within this directory, place your observer class file. Your observer class should implement the Magento\Framework\Event\ObserverInterface interface and define its execute method.

Here's an example of the basic structure for an observer class:

namespace MyCompany\MyModule\Observer;
use Magento\Framework\Event\ObserverInterface;
class MyObserver implements ObserverInterface
{
  public function __construct()
  {
   

Subscribing to events

Observers can be configured to watch certain events in the events.xml file.

There are following properties of the xml element

name (required) - Defines observer’s name for the event definition.

instance (required) - The fully qualified class name of the observer.

disabled - Defines whether this observer is active or not. Has false value by default.

shared - Determines the lifestyle of the class. Has true value by default.

Take a look how observers can be assigned to watch specific events

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=
"urn:magento:framework:Event/etc/events.xsd">
    <event name="my_module_event_before">
        <observer name="myObserverName" instance="MyCompany\MyModule\Observer\MyObserver" />
    </event>
    <event name="my_module_event_after">
        <observer name="myObserverName" instance="MyCompany\MyModule\Observer\AnotherObserver" />
    </event>
</config>

Please note that each observer name must be unique within an event definition. Having two observers with the same name in the same event definition can lead to override conflicts and should be avoided.

How to dispatch events?

To dispatch events you have to be using the Magento\Framework\Event\Manager class. It can be obtained through dependency injection by defining the dependency in your constructor.

At first, call the invoke the dispatch function of the event manager class, providing it with the name of the event you intend to dispatch along with an optional array of data to be passed to observers.

Below are examples demonstrating how to dispatch an event with and without an array of data.

namespace MyCompany\MyModule;
use Magento\Framework\Event\ManagerInterface as EventManager;
class MyClass
{
  /**
  * @var EventManager
  */
  private $eventManager;
  public function __construct(EventManager $eventManager)
  {
    $this->eventManager = $eventManager;
  }
  public function something()
  {
    $eventData = null;
   

Now that you have a basic understanding of what observers are in Magento 2, you can explore the full observers and events list on the following page.

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.