Magento 2 — Module — Create Observer

Observer is a feature of Magento 2 Module to make an action on events such as User adds product to cart, User logins, etc.

So, how to make it?

1. Create etc/events.xml file

You can make them into a different areas:

  • Global Area : <root>/etc/events.xml
  • Front-End Area : <root>/etc/frontend/events.xml
  • Back-End Area : <root>/etc/adminhtml/events.xml

Here’s a simple example of this file :

You can see what events Magento has here :

https://www.simicart.com/blog/magento-2-events-list/

2. Create Observer Class

Create a Class that has execute method on it. Why? Because Magento 2 will automatically calls execute method. Here’s the example :

Don’t forget to flush the cache! And run it.

I wrote these tutorials for myself in future when I forget for the next steps.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store