4

Actually, I am trying to get customer data in observer and my event is

"adminhtml_customer_save_after"

I have made observer

Code:-

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

namespace Amitshree\Customer\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 "<pre>";
    $event = $observer->getEvent();
    $customer = $event->getCustomer();
    print_r($customer->getData());
    die('Observer Is called!');

    }
}

I want to get Customer infromation and i am trying to print customer in this file using

$event = $observer->getEvent();
$customer = $event->getCustomer();
print_r($customer->getData());

but don't get any result.

I have created events.xml in etc/adminhtml/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="adminhtml_customer_save_after">
        <observer name="Amitshree_Customer_Customer_Save_After" instance="Amitshree\Customer\Observer\Customersaveafter" />
    </event>
</config>
  • 1
    Please share your event file code – inrsaurabh Jan 22 '18 at 6:31
  • 1
    @SaurabhRanjan I have updated my answer – Jaimin Jan 22 '18 at 6:33
  • 1
    please try $customer->getId() id is getting or not? – Rakesh Jesadiya Jan 22 '18 at 6:35
  • 1
    @RakeshJesadiya you are right i got id and email also. – Jaimin Jan 22 '18 at 6:40
  • 1
    @Jaimin You problem is solved. Great – inrsaurabh Jan 22 '18 at 7:00
5

You can get the id and email from observer as shown below,

public function execute(EventObserver $observer)
    {
    $event = $observer->getEvent();
    $customer = $event->getCustomer();
    $email = $customer->getEmail();
    $id = $customer->getId();
}

You can also get customer object with the below method,

print_r($customer->debug());
  • How I will get all the customer data or Params? – Ashna Mar 30 '18 at 12:48
2

If you need customers information you can do:

public function execute(EventObserver $observer)
{

//echo "<pre>";
$event = $observer->getEvent();
$customer = $event->getCustomer();
print_r($customer->getEmail());
var_dump( $customer->getId());
var_dump( $customer->getName());
var_dump( $customer->getGroupId());
die('Observer Is called!');

}

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.