1

event.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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="customer_customer_account_edited" instance="Customer\CustomerAccountEdit\Observer\CustomerAccountEdit"/>
    </event>
</config>

CustomerAccountEdit.php

<?php
namespace Customer\CustomerAccountEdit\Observer;

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


class CustomerAccountEdit 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)
    {
        $event = $observer->getEvent();
        $customer = $event->getCustomer();
        $email = $customer->getEmail();
        $id = $customer->getId();
        print_r($customer->debug());
    }
}

Result.

Fatal error: Uncaught Error: Call to a member function getEmail() on null in ...

2 Answers 2

1

Use getData() method to get email

$customer = $observer->getEvent();
$email=$customer->getData('email');

Updated answer:

$CustomerModel = $objectManager->create('Magento\Customer\Model\Customer');
$CustomerModel->loadByEmail($email);
$userId = $CustomerModel->getId();

Note: Code Tested

5
  • it works, but i can't get customer id, first name and last name through this , thanks MSA
    – Robinio
    Jul 19, 2019 at 15:42
  • 1
    You can get name by load customer collection by email id Jul 19, 2019 at 15:52
  • check my updated answer Jul 19, 2019 at 15:54
  • I think that objectmanager places are best injected in the class \Magento\Customer\Model\Customer
    – Robinio
    Jul 19, 2019 at 16:30
  • Yes, You can use Di method(Injection in a constructor) Jul 19, 2019 at 16:32
0

Accepted answer is correct however using object manager.

Better practice is to use \Magento\Customer\Api\CustomerRepositoryInterface

    /**
     * @var \Magento\Customer\Api\CustomerRepositoryInterface
     */
    protected $customerRepository;

    public function __construct(
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
    ) {
        $this->customerRepository = $customerRepository;
    }

    public function getCustomerByEmail($email)
    {
        try{
            $customer = $this->customerRepository->get($email);
            $customerId = $customer->getId();
        }catch(\Magento\Framework\Exception\NoSuchEntityException $exception){
            // handle this
        }catch(\Magento\Framework\Exception\LocalizedException $exception){
            // handle this
        }
        return $customer;
    }

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.