How to add observer to customer edited details.

Create a custom module n the code folder,here we are going to override the EDITPOST file from the magento-customer/controller/account .

EditPost.php here added first name and last name to the event dispatch

  private function dispatchSuccessEvent(\Magento\Customer\Api\Data\CustomerInterface $customerCandidateDataObject)
    {
        $this->_eventManager->dispatch(
            'customer_account_edited',
            ['email' => $customerCandidateDataObject->getEmail(),
                'firstname'=>$customerCandidateDataObject->getFirstname(),
                'lastname'=>$customerCandidateDataObject->getLastname() ]
        );
    }

Here my vendor name is JJ and module name is Customerupdate

di.xml for the module

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Controller\Account\EditPost" type="JJ\Customerupdate\Controller\Account\EditPost" />
</config>

etc/frontend 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="customer_account_edited">
        <observer name="editcustomer" instance="JJ\Customerupdate\Observer\EditCustomer"/>
    </event>
</config>

Observer folder contains EDITCUSTOMER.php

<?php

namespace JJ\Customerupdate\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Element\Messages;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultFactory;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;


class EditCustomer implements ObserverInterface
{

    protected $resultPageFactory;
    protected $_resultJsonFactory;
    protected $storeManager;

    public function __construct(Context $context, PageFactory $pageFactory,
    JsonFactory $resultJsonFactory,
    StoreRepositoryInterface $storeRepository,
    StoreManagerInterface $storeManager,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
    \Magento\Customer\Model\Session $customerSession

    )
    {
        $this->resultPageFactory = $pageFactory;
        $this->_resultJsonFactory = $resultJsonFactory;
        $this->storeManager = $storeManager;
        $this->storeRepository = $storeRepository;
        $this->customerSession = $customerSession;
        $this->customerRepositoryInterface = $customerRepositoryInterface;
        $this->redirect = $context->getRedirect();
        //parent::__construct($context);

    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/datasent.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info("Hey");
        $customer= [
            'id' =>$this->customerSession->getCustomer()->getId(),
            'email'=>$observer->getEvent()->getEmail(),
            'firstname'=>$observer->getEvent()->getFirstname(),
            'lastname'=>$observer->getEvent()->getLastname()
        ];
        $logger->info(json_encode($customer));
    }
}

Leave a comment

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