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
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
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.
[mc4wp_form id="194"]



