0

I am trying to save customer custom attribute value at customer_save_after observer. but observer script is not working at observer but that script working at contoller.

events.xml

<event name="customer_save_after">
<observer name="VestricsM2_Connector_Customer_Save_After" instance="VestricsM2\Connector\Observer\CustomerSaveAfter" /> </event>

Observer/CustomerSaveAfter.php

protected $customerRepository;

protected $_logger;

/**
 * @param \Magento\Framework\ObjectManagerInterface $objectManager
 */
public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
) {
    $this->_objectManager = $objectManager;
    $this->_logger = $logger;
    $this->customerRepository = $customerRepository;
}

/**
 * customer register event handler
 *
 * @param \Magento\Framework\Event\Observer $observer
 * @return void
 */
public function execute(\Magento\Framework\Event\Observer $observer)
{
    $customer = $observer->getEvent()->getCustomer();

    if($customer->getId()){
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerRepository = $objectManager->create('Magento\Customer\Model\Customer')->load('1');
         $customerRepository->setUpdatedinsap('1');
         $customerRepository->save();

       /* $writer = new \Zend\Log\Writer\Stream(BP .'/var/log/observer-log-file.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($customerRepository->getData());*/

    }
  • whats your custom attribute value name? – Rakesh Jesadiya Apr 10 '17 at 9:55
  • Rakesh , my custom attribute name is "updatedinsap", but that above script working in controller – Ramesh Apr 10 '17 at 9:56
  • have you check customer id is getting or not? – Rakesh Jesadiya Apr 10 '17 at 10:28
  • i checked with this script $customerRepository = $objectManager->create('Magento\Customer\Model\Customer')->load('1'); i am getting data in log file – Ramesh Apr 10 '17 at 10:30
0

Using CustomerRepositoryInterface you can save your custom customer attribute value,

Save your customer attribute value using try/cache block,

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $customer = $observer->getEvent()->getCustomer();

    if($customer->getId()){
        $customer = $this->customerRepository->getById($customer->getId());
        $customer->setCustomAttribute('updatedinsap',1);
        try {                
            $customer = $this->customerRepository->save($customer);
        }catch (Exception $e) {
            return $e->getMessage();
        }
    }
   return;

}
| improve this answer | |
  • Rakesh, am facing same issue. – Ramesh Apr 10 '17 at 10:11
  • please first remove var/generation folder from root. – Rakesh Jesadiya Apr 10 '17 at 10:13
  • actually i removed total var folder and done upgrade & compile – Ramesh Apr 10 '17 at 10:17
  • which error are thrown please share. – Rakesh Jesadiya Apr 10 '17 at 10:19
  • the page is continuously loading then i have restart Apache sever other wise it's not working. in debug log " cache_invalidate: {"method":"GET","url":"http:/","invalidateInfo":{"tags":[],"mode":"all"},"is_exception":false} [] " – Ramesh Apr 10 '17 at 10:25

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.