1

I'm trying to Create new customer attribute "last_login_at" to store customer last login date time. My requirement is to update "last_login_at" data for the customer after the customer logged in. I really don't know how to update it with an observer. Can anybody help me? Many thanks,

Here is my code for creating the attribute:

if (version_compare($context->getVersion(),'2.2.17','<')){
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
      $attributeCode = 'last_login_at';


        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY, // customer entity code
            'last_login_at',
            [

                'label' =>"Last Login",
                'type' => 'datetime',
                'input' => 'date',
                'frontend' => \Magento\Eav\Model\Entity\Attribute\Frontend\Datetime::class,
                'backend' => \Magento\Eav\Model\Entity\Attribute\Backend\Datetime::class,
                'required' => false,
                'user_defined' => true,
                'visible' => true,
                'system' => false,
                'input_filter' => 'date',
                'validate_rules' => '{"input_validation":"date"}',
                'position'     => 999,


            ]
        );

        $eavSetup->addAttributeToSet(
            \Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
            \Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
            null,
            'last_login_at');

        $lastLogin = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'last_login_at');


        $lastLogin->setData(
            'used_in_forms',
            ['customer_account_edit','adminhtml_customer','customer_account_create'] 
        );
        $lastLogin->save(); 
    }
    $setup->endSetup();
}
  • last_login_at field is showing in database ? – Pawan Mar 25 '19 at 2:38
  • yes, it is showing in database – trem jenny Mar 29 '19 at 10:03
  • Glad to know you found solution :) – Pawan Mar 30 '19 at 2:25
0

Try the below function in your observer as :

namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Customer;

class Observerclass implements ObserverInterface
{
    /**
    * @var \Magento\Framework\App\ResponseFactory
    */
    protected $_customerRepositoryInterface;

    public function __construct(
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
) {
        $this->_customerRepositoryInterface = $customerRepositoryInterface;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {   
        $customer = $observer->getEvent()->getCustomer();
        $customerData = $customer->getDataModel();
        $customerData->setCustomAttribute('last_login_at', $value);
        $customer->updateData($customerData);
        $customer->save();
    }
}
  • 1
    Thanks for your help, i have a question, what does this '$value' mean? – trem jenny Mar 24 '19 at 8:53
  • @tremjenny, Its a variable you need to set this value as according to your requirement. – Amit Naraniwal Mar 25 '19 at 5:10
  • many thanks, i got it. – trem jenny Mar 29 '19 at 10:03

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.