1

I have used this event "customer_save_after_data_object"

used this code in events.xml file

<event name="customer_save_after_data_object">
    <observer name="customer_save_after_observer" instance="Vendor\Sales\Observer\CustomereditPost" />
</event>

And this is the observer file

<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
class CustomereditPost implements ObserverInterface

    {
        protected $_request;
        protected $_layout;
        protected $_objectManager = null;
        protected $_customerGroup;
        private $logger;
        protected $_customerRepositoryInterface;

    /**
    * @param \Magento\Framework\ObjectManagerInterface $objectManager
    */
    public function __construct(
        \Magento\Framework\View\Element\Context $context,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
    ){
        $this->_layout = $context->getLayout();
        $this->_request = $context->getRequest();
        $this->_objectManager = $objectManager;
        $this->logger = $logger;
        $this->_customerRepositoryInterface = $customerRepositoryInterface;
    }

    /**
    * @param \Magento\Framework\Event\Observer $observer
    * @return void
    */
    public function execute(EventObserver $observer)
    {
        $this->logger->info(' --jafar123--  ');
        $event = $observer->getEvent();
        $customer = $observer->getCustomerDataObject();
        $customer->getId();
        $customer->getFirstName();
        $customerId = $customer->getId();
        $customerData = $this->_customerRepositoryInterface->getById($customerId);
        $customerData->setFirstName('jaf');
        $customerData->save();
        return $this;
    }
}

On saving customer from admin panel, I need to trigger this file.

Just setting the customer first name from this event. But the name is not changed.

The observer file is loading. Can anybody help me on this please?

5
  • have you checked that after saving customer from admin panel ,logs are print in var/log folder or not? i means control comes in your observer file or not? Commented Nov 23, 2018 at 14:48
  • Yes, it is coming to observer file, and it is printed to log file Commented Nov 23, 2018 at 14:56
  • then put this field also in $customer->getId(); $customer->getFirstName(); and check what value comes in this log Commented Nov 23, 2018 at 15:07
  • @balwant, yes i am getting those values. Commented Nov 23, 2018 at 15:09
  • check my answer Commented Nov 23, 2018 at 15:16

1 Answer 1

3

update your code with below code and try

<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
class CustomereditPost implements ObserverInterface
{
protected $_request;
protected $_layout;
protected $_objectManager = null;
protected $_customerGroup;
private $logger;
protected $_customerRepositoryInterface;

/**
 * @param \Magento\Framework\ObjectManagerInterface $objectManager
 */
public function __construct(
    \Magento\Framework\View\Element\Context $context,
    \Magento\Framework\ObjectManagerInterface $objectManager,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
){
    $this->_layout = $context->getLayout();
    $this->_request = $context->getRequest();
    $this->_objectManager = $objectManager;
    $this->logger = $logger;
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
}

/**
 * @param \Magento\Framework\Event\Observer $observer
 * @return void
 */
public function execute(EventObserver $observer)
{
    $this->logger->info(' --jafar123--  ');
    $event = $observer->getEvent();
    $customer = $observer->getCustomerDataObject();
    $customerId = $customer->getId();
    $customer->getFirstName();
    $customerData = $this->_customerRepositoryInterface->getById($customerId);
    $customerData->setFirstName('jaf');
    $customerData->save();
    return $this;
}
}
10

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.