How to Create Observer in Magento 2?

  • Nidhi Arora
  • 4 years
How to Create Observer in Magento 2?

Creating an observer in Magento 2 is a bit different from Magento 1.x version. In Magento 2, an observer can be originated from the events.xml file under etc directory.

Let’s have a closer look on how to create observer in Magento 2:

1.  The very first step is to define observer in the file:

<?xml version="1.0"?>
 <event name="controller_action_catalog_product_save_entity_after">
 <observer name="Envision_Eecom_Product_Save_Later" instance="Envision\Eecom\Observer\productSaveLater" />

Note: Different places are required to make files for different handlers.

• Make file under: app/code/ Envision/Eecom/etc/frontend/event.xml for frontend observer.

• Make file under: app/code/Envision/Eecom/etc/adminhtml/event.xml for frontend observer.

• Generate file under: app/code/Envision/Eecom/etc/event.xml for having observer at both ends.

2. Now, create your observer file in the following path: after


namespace Envision\Eecom\Observer;

use Magento\Framework\Event\ObserverInterface;

class productSaveLater implements ObserverInterface
* @var ObjectManagerInterface
protected $_objectManager;

* @param \Magento\Framework\ObjectManagerInterface $objectManager
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager
) {
$this->_objectManager = $objectManager;

* customer register event handler
* @param \Magento\Framework\Event\Observer $observer
* @return void
public function execute(\Magento\Framework\Event\Observer $observer)
//Do your stuff here!
die('Envision Observer Is Here!');

Now, if you still have any query regarding Magento 2 observers, leave it in the comment section below.

Join us for a FREE webinar