Magento 2 Create Event Observer

Magento 2 Create Event Observer– Observers are great ways to extend the default functionality in Magento. Using Observers we can easily implement the custom changes required related to different-different functionalities. Here in this tutorial we are going to explain how you can create Event Observers in Magento 2.

Magento 2 Create Event Observer Example

Creating observer event in Magento 2 is totally different than the Magento 1. We need to create events.xml configuration file to create events-

Step 1 – Create events.xml

Go to etc folder and create events.xml and define the event obsever simply as below-

Magento 2 Create Event Observer Example:

<?xml version="1.0" ?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
	<event name="sales_order_place_after">
		<observer instance="Tutorialsplane\HelloWorld\Observer\Sales\OrderPlaceAfter" name="tutorialsplane_helloworld_observer_sales_orderplaceafter_sales_order_place_after"/>

Step 2 – Create Obsever

Now go to Observer folder and create folder named as Sales. Inside this sales folder create OrderPlaceAfter.php and add the code as below-

Magento 2 sales order place after Event Example:


namespace Tutorialsplane\HelloWorld\Observer\Sales;

class OrderPlaceAfter implements \Magento\Framework\Event\ObserverInterface

     * Execute observer
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        //Your observer code goes here

Thus you can create any observer event in magento 2.

Download Sample Code

If you want to download the sample code you can download it from here - Magento 2 Observer Example Sample


Add Comment

📖 Read More