Magento 2 – Save Custom Customer Attribute in Observer Using Customer Session

custom-attributesevent-observermagento-2.1

I am working in Magento2.1 and created a custom module which changes the customer's group id and now i want to display the date when the customer's id changed in admin customer listing So i have created a custom attribute and want to save the date when the customer's id changed in that attribute. Below is my observer file code

   <?php
    namespace vendor\module\Observer;
    use Magento\Framework\Event\ObserverInterface;

    class customeramount implements \Magento\Framework\Event\ObserverInterface
    { 
     public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig )
        {
         $this->_scopeConfig = $scopeConfig;
        }
    public function execute(\Magento\Framework\Event\Observer $observer)
      {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
         $customerSession = $objectManager->create('Magento\Customer\Model\Session');

     if($customerSession->isLoggedIn()) 
     {
       $customer_id = $customerSession->getCustomer()->getId();
       $initial_group_id = $customerSession->getCustomer()->getId();
       $customer_group =  $this->decide_group_id($total, 
        $customer_group, $quantity);
       if ($initial_group_id != $customer_group){
          $customerSession->setMemebrCustomAttr(date('d/m/Y'));
          }
    }
  }

}
?> 

"decide_group_id" is a custom function which gives the "customer group id" depending on desired conditions.

Please guide me.

Best Answer

Thanks. below is the solution of my question

<?php
    namespace vendor\module\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\Message\ManagerInterface ;

    class customeramount implements \Magento\Framework\Event\ObserverInterface
    { 
     public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig )
        {
         $this->_scopeConfig = $scopeConfig;
        }
    public function execute(\Magento\Framework\Event\Observer $observer)
      {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
         $customerSession = $objectManager->create('Magento\Customer\Model\Session');

     if($customerSession->isLoggedIn()) 
     {
       $customer_id = $customerSession->getCustomer()->getId();
       $initial_group_id = $customerSession->getCustomer()->getId();
       $customer_group =  $this->decide_group_id($total, 
        $customer_group, $quantity);
       if ($initial_group_id != $customer_group){
           $customerId = $customer_id;
            $customer = $this->customer->load($customerId);
            $data = date('d/m/Y');
            $customerData = $customer->getDataModel();
            $customerData->setCustomAttribute('memebr_custom_attr',$data);
            $customer->updateData($customerData);
            $customerResource = $this->customerFactory->create();
            $customerResource->saveAttribute($customer, 'memebr_custom_attr');

          }
    }
  }

}
?> 
Related Topic