1

I need to add some custom logic in my extension after Editing Account Information, It should not trigger at Change Password or Edit Address.

Will customer_account_edited work.

$this->_eventManager->dispatch(
        'customer_account_edited',
        ['email' => $customerCandidateDataObject->getEmail()]
    );
0
2

Yes, You can use it (will work). Magento 2 event "customer_account_edited" is official/legal event name.

In Your module add this event into your events.xml file. For example: VendorName/ModuleName/etx/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="VendorModuleEditAccount" instance="VendorName\ModuleName\Observer\EditCustomer"/>
    </event>
</config>

Then create needed file EditCustomer.php which gets data from observer. For example: VendorName/ModuleName/Observer/EditCustomer.php

<?php

declare (strict_types = 1);

namespace VendorName\ModuleName\Observer;

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

class EditCustomer implements ObserverInterface
{

    private $logger;

    public function __construct(
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->logger = $logger;
    }

    public function execute(Observer $observer)
    {
       try {
            $editedCustomerEmail = $observer->getEvent()->getEmail();
            $this->logger->info($editedCustomerEmail);
         } catch (\Exception $e) {
             $this->logger->critical('Error message', ['exception' => $e]);
         }

    }
}

Upgrade Your modules and clear cache with commands in terminal:

php bin/magento setup:upgrade

php bin/magento cache:clean


Go to frontend, login or create new account. Edit account. Go to /var/log/info.log There You'll see "test@testemail.com [] []"

Please note that there is no point to print "$observer->getEvent()->getCustomer()", cause observer not output all customer info after "customer_account_edited" event. It only outputs email address (that's why I logged only email address). If You'll print out all $observer You'll get

object(Magento\Framework\Event\Observer)#1529 (1) { ["_data":protected]=> array(2) { ["event"]=> object(Magento\Framework\Event)#1398 (2) { ["_observers":protected]=> object(Magento\Framework\Event\Observer\Collection)#1575 (1) { ["_observers":protected]=> array(0) { } } ["_data":protected]=> array(2) { ["email"]=> string(18) "test@testemail.com" ["name"]=> string(23) "customer_account_edited" } } ["email"]=> string(18) "test@testemail.com" } }

Tested with Magento 2.3.0

1
  • But it is not working in 2.3.5
    – Sabareesh
    Jun 26 '20 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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