3

I am new in Magento and want to create an observer (event) which should fire after adding a customer. I tried below tutorial but not working: https://webkul.com/blog/observers-in-magento2/

I created folders and files as described in this but that is not working. Any help will really appreciable.

Thanks in advance.

1 Answer 1

4

Assuming you have created custom module to do this operation .

So in your Vendor_Module directory create one directory called etc.

In etc directory create one file called events.xml

Put below code in events.xml file

<?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="adminhtml_customer_save_after">
        <observer name="Vendor_Module_Customer_Save_After" instance="Vendor\Module\Observer\Customersaveafter" />
    </event>
</config>

Now you need to create observer for the same . so create directory called Observer in your Vendor/Module Directory

In your observer directory create Customersaveafter.php file

<?php
/**
* Copyright � 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;


class Customersaveafter implements ObserverInterface
{
    protected $_request;
    protected $_layout;
    protected $_objectManager = null;
    protected $_customerGroup;

    /**
    * @param \Magento\Framework\ObjectManagerInterface $objectManager
    */
    public function __construct(
        \Magento\Framework\View\Element\Context $context,
        \Magento\Framework\ObjectManagerInterface $objectManager,
    ) {
        $this->_layout = $context->getLayout();
        $this->_request = $context->getRequest();
        $this->_objectManager = $objectManager;
    }

    /**
    * @param \Magento\Framework\Event\Observer $observer
    * @return void
    */
    public function execute(EventObserver $observer)
    {
        echo "Do your operaiton here";
    }
}

Note : adminhtml_customer_save_after event is called after adding the customer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.