---
title: "How to Create Events Observer in Magento 2?"
url: "https://magecomp.com/blog/create-events-observer-magento-2/"
date: "2017-04-07T15:28:51+00:00"
modified: "2025-02-22T07:21:01+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 624
reading_time: "4 min read"
summary: "The events in Magento 2 architecture helps extending the default functionality of it. It is a flag that rises at the occurrence of some specific situation for example, click “submit” button on ..."
description: "Detailed guide on creating events observer in Magento 2. Learn with an easy example."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Add URL Rewrite Programmatically in Magento 2?"
    url: "https://magecomp.com/blog/add-url-rewrite-programmatically-magento-2/"
  - title: "How to Create Custom Facade in Laravel?"
    url: "https://magecomp.com/blog/create-custom-facade-laravel/"
  - title: "How to Create, Activate and Authorize a &#8216;New Integration&#8217; Programmatically in Magento 2?"
    url: "https://magecomp.com/blog/create-activate-authorize-a-new-integration-programmatically-in-magento-2/"
---

# How to Create Events Observer in Magento 2?

_Published: April 7, 2017_  
_Author: Dhiren Vasoya_  

![How to Create Events Observer in Magento 2](https://magecomp.com/blog/wp-content/uploads/2017/04/How-to-Create-Events-Observer-in-Magento-2-1024x332.webp)

The events in Magento 2 architecture helps extending the default functionality of it. It is a flag that rises at the occurrence of some specific situation for example, click “submit” button on contact page is an event performed by website visitors. An observer works as a listener that detects events and performs an action defined at the time of event occurrence.

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-3.webp)](https://magecomp.com/magento-malware-removal.html)

It allows adding own logic to the core code without changing core files or code. Having powerful event/observer pattern, we can use it very effectively in Magento 2 custom modules.

**Catching and handling an Event:**

Area definition is used by Magento to manage the store. We will have frontend area and admin area where we can put area definition with configuration file:

- Under **etc/ folder** – to use both in admin and frontend.
- Under **etc/frontend** folder – to use in frontend area.
- Under **etc/adminhtml** folder – to use in admin area.

Create events configuration file for each area like this:

- **Admin area:** app/code/Magecomp/HelloWorld/etc/adminhtml/events.xml
- **Frontend area:** app/code/Magecomp/HelloWorld/etc/frontend/events.xml
- **Global area:** app/code/Magecomp/HelloWorld/etc/events.xml

Implementation of event/observers in Magento 2 is based on public-subscribe pattern. Magento 2 can execute any number of observers for an event occurrence. Here, we will learn to ***create events observers in Magento 2***.

## Detailed Guide on Creating Event Observer in Magento 2:

In Magento 2, Magento\Framework\Event\ManagerInterface interface is responsible for dispatching the events.
**Magento/Checkout/Model/Cart.php**

```
namespace MagentoCheckoutModel;

use MagentoCheckoutModelCartCartInterface;
use MagentoFrameworkDataObject;

class Cart extends DataObject implements CartInterface
{
    ...
   public function save()
   {
       $this->_eventManager->dispatch('checkout_cart_save_before', ['cart' => $this]);
       //Perform save cart action
       $this->_eventManager->dispatch('checkout_cart_save_after', ['cart' => $this]);
       return $this;
   }
   ...
}
```

Here, we are dispatching ‘**checkout_cart_save_before**’ and ‘**checkout_cart_save_after**’ events and thus we can now easily trigger any action before and after the cart is saved. This helps avoiding rewrite of the entire class.
Magento allows passing values to the observers to trigger the action on event occurrence as you saw in above example.

**Custom Events:**
To dispatch custom events in Magento 2, create an object for **Magento\Framework\Event\ManagerInterface** and pass the unique custom event name to the object.

```
$this->_eventManager->dispatch(‘custom_action_after_save’, [‘param’ => $this]);
```

Here, parameters are optional.

**Mapping Events and Observer:**
To configure the observers to certain events, **Events.xml** files is responsible.

```
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="magecomp_mymodule_event_after">
        <observer name="magecomp_mymodule_event_after_observer" instance="MagecompMyModuleObserverMyObserver"/>
    </event>
</config>
```

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2023/10/Magento-2-Speed-Performance-Optimization-Services-CTA-4.webp)](https://magecomp.com/magento-performance-optimization.html)

**observer** node is having following properties:

- name – Observer Name that must be unique for each event.
- instance – Magento class that needs to be executed when the event is dispatched.
- disabled – To activate or deactivate the observer.
- shared – whether the instance to be created is shared or not. Default is false.

**Observers:**
Observers are executed when the mapped event is triggered. Event Manager is responsible for dispatching events and executing the respective observers.

```
namespace MAGECOMPMyModuleObserver;

use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;

class MyObserver implements ObserverInterface
{

 public function execute(Observer $observer)
 {
    // get parameters passed in events
    $cart = $observer->getData('param');

    //Perform action
 }

}
```

The above structure is a basic observer class. Note that Observer class must implement **\Magento\Framework\Event\ObserverInterface**.

I hope this blog has helped you to create and implement events observer in Magento 2.

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-2.webp)](https://magecomp.com/services/hyva-theme-development/)

If you stuck with something while implementation, let me know through commenting. I’ll be happy to assist you.


---

_View the original post at: [https://magecomp.com/blog/create-events-observer-magento-2/](https://magecomp.com/blog/create-events-observer-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-14 09:01:08 UTC_  
