Menu Close
    Searching for an experienced Magento 2 Development Company ?

    Observers in Magento2

    Today we are discussing observer in magento 2.

    To create observer in magento2, first we need to define observer in file:

    app/code/Webkul/Hello/etc/events.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="controller_action_catalog_product_save_entity_after">
            <observer name="Webkul_Hello_Product_Save_After" instance="Webkul\Hello\Observer\productSaveAfter" />
        </event>
    </config>
    

    Note: There are different places to create files for different handlers.

    1. To create observer for frontend you can create file under : app/code/Webkul/Hello/etc/frontend/events.xml
    2. To create observer for frontend you can create file under : app/code/Webkul/Hello/etc/adminhtml/events.xml
    3. To create observer for both end, you need to create file under : app/code/Webkul/Hello/etc/events.xml

    After this file, you need to create your observer file at the path you have mentioned above, i.e.:

    app/code/Webkul/Hello/Observer/productSaveAfter.php

    <?php
    namespace Webkul\Hello\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    
    class productSaveAfter implements ObserverInterface
    {
        /**
         * @var ObjectManagerInterface
         */
        protected $_objectManager;
    
        /**
         * @param \Magento\Framework\ObjectManagerInterface $objectManager
         */
        public function __construct(
            \Magento\Framework\ObjectManagerInterface $objectManager
        ) {
            $this->_objectManager = $objectManager;
        }
    
        /**
         * customer register event handler
         *
         * @param \Magento\Framework\Event\Observer $observer
         * @return void
         */
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            //Do your stuff here!
            die('Observer Is called!');
        }
    }
    
    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    5 comments

  • Anonymous
    how to work this program in magento 2
    • ashutosh srivastava (Moderator)
      this will work when you will create or update a product from admin and event filename was incorrect I updated it.
  • Qa Test0110
    There is text problem. for second line in below text should be backend. you have type frontend for second as well.

    To create observer for frontend you can create file under : app/code/Webkul/Hello/etc/frontend/event.xml
    To create observer for frontend you can create file under : app/code/Webkul/Hello/etc/adminhtml/event.xml
    To create observer for both end, you need to create file under : app/code/Webkul/Hello/etc/event.xml

  • Ali Saleem
    What is the correct filename for the xml file, events.xml or event.xml
    • Akbar Adeeb
      The correct file name will we following
      To create observer for frontend you can create file under : app/code/Webkul/Hello/etc/frontend/events.xml
      To create observer for frontend you can create file under : app/code/Webkul/Hello/etc/adminhtml/events.xml
      To create observer for both end, you need to create file under : app/code/Webkul/Hello/etc/events.xml
  • Back to Top