0

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.

  • What is the error message you are receiving? – Haritha May 22 '18 at 11:01
  • No error, but the attribute value doesn't save. actually the value is saved in session only. after session vanishes the attribute value also vanishes. IF i manually adds value, it saves. it appears in customer grid and also in filter options. – Prabhat Mishra May 22 '18 at 11:12
1

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');

          }
    }
  }

}
?> 
0

Replace your code like this :

$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'));

   // Add this code
   $customer = $objectManager->create('Magento\Customer\Model\Session')->load($customer_id);
   $customer->setMemebrCustomAttr(date('d/m/Y'));
   $customer->save();
}

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.