Magento Observers and Events
Posted By : Rahul Gupta | 31-Jan-2022
Do you know about Observer and Events in Magento 2?
What are events in Magento?
Events are dispatched in Magento modules when certain action performs. Magento allows us to create custom events also that can be dispatched in our code. When an event is dispatched it calls the observer to perform work that we have defined in the observer.
What are observers in Magento?
Observers are a PHP file in Magento that can influence general behavior or change working logic. Observer executed when an event is dispatched by the event manager.
How to trigger a custom event?
1. Create a custom event in your code using event dispatch.
EventManager class use for dependency injection "Magento\Framework\Event\ManagerInterface"
$this->_eventManager->dispatch('custom_event', ['data' => $data]); |
2. Create an event.xml file
- Global area: /etc/events.xml
- Frontend area: /etc/frontend/events.xml
- Admin area: /etc/adminhtml/events.xml
<?xml version="1.0"?> |
3. Create observer class
<?php use Magento\Framework\Event\ObserverInterface; class CustomObserver implements ObserverInterface |
How to trigger a pre-define event?
You can also use Magento pre-define events by creating an events.xml file with that event and create an observer according to that.
We have created a custom observer in one project so that we can put data in the custom table after successful order for that we have used a Magento pre-define event once event trigger/dispatch our custom obeserver is call and perform actions we have created.
1. Create events.xml file with Magento pre-defien event "checkout_onepage_controller_success_action"
<?xml version="1.0"?> |
2. Observer file
<?php use Magento\Framework\Event\ObserverInterface; class GetSalesOrderData implements ObserverInterface |
To know more about Magento events and observers reach us at [email protected].
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rahul Gupta
Rahul Gupta is an experienced backend developer with over 1+ years of experience. He has a wide range of technical skills, including proficiency in several programming languages such as C, C++, Java, and PHP. Additionally, Rahul has expertise in using various tools such as Eclipse, Prolog, Linux (Ubuntu), VS Code, Postman, Git, Trello, and Zoho. He also has a strong understanding of web technologies such as HTML, CSS, and JavaScript, as well as RDBMS systems like SQL Server. Rahul has worked with several frameworks including Magento 2, Laravel, and WordPress. He has contributed to several projects such as Fidem Management System, Colmena, Yami Paws, Oodles Technology Site, Email Sending Tool, Dermava, Info Sharing Website Phase, Oodles Scaffold (CRM/HRM/POS), Fiore. He is a self-motivated individual who takes a proactive approach to his work and is always eager to learn new technologies and techniques.