Hello Friends,
In This Topic, I will cover how you can create Magento 2 observer
What is Magento 2 observer?
If you want to Trigger any functionality after your any business logic or action then you can create observer
For example, If you want to send sms to customer after order place.
Then there is an event sales_order_place_after which you can use to send sms after order place.
First of you all you need to create custom Module.
Now create events.xml file inside etc folder and add below code
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="mageacademy_observerexample_event_after">
<observer name="mageacademy_observerexample_event_after_observer" instance="MageAcademy\ObserverExample\Observer\CustomObserver"/>
</event>
</config>
Here I will explain each terminology in simple language
event name : this is event name which is define for particular action
observer name : this is unique name of your observer , which will differentiate with your multiple observers
instance : This is class file for Observer and you need to use execute function which default function for each observer.
Below is example for Observer class file
<?php
namespace MageAcademy\ObserverExample\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class CustomObserver implements ObserverInterface
{
public function __construct()
{
// Here you can define DependencyInjection of any class
}
public function execute(Observer $observer)
{
$myEventData = $observer->getData('myEventData');
}
}
Event areas
- global : This type of observer can be used in every area like frontend , admin , api , cron etc so you can create events.xml in MyCompany/Module/etc/ folder
- frontend : If you want to create observer which can be used only for frontend area but not other area like admin , api etc then you need to create events.xml in MyCompany/Module/etc/frontend folder
- adminhtml : If you want to create observer which can be used only for admin area but not other area like frontend , api etc then you need to create events.xml in MyCompany/Module/etc/adminhtml folder
- crontab : If you want to create observer which can be used only for cron but not other area like frontend , api etc then you need to create events.xml in MyCompany/Module/etc/crontab folder
- webapi_rest : If you want to create observer which can be used only for rest api but not other area like frontend , admin , crontab etc then you need to create events.xml in MyCompany/Module/etc/webapi_rest folder
- webapi_soap : If you want to create observer which can be used only for soap api but not other area like frontend , adminhtml etc then you need to create events.xml in MyCompany/Module/etc/webapi_soap folder
- graphql : If you want to create observer which can be used only for graphql but not other area like frontend , adminhtml etc then you need to create events.xml in MyCompany/Module/etc/graphql folder
You can watch our video on this topic Click Here
Hope you like our this Blog Topic, Please share with your friends and motivate us so we can write more topics.