Loading...

How to add customer last logged in column customer grid in Magento 2?

Jayram Prajapati  ·   13 Oct 2022
How to add customer last logged in column customer grid in Magento 2?
service-banner

Introduction

Magento Customer Grid Customisation is very handy.

Follow the steps to customize the Customer Grid in Magento 2 and add the Customer's last logged-in date.

Step 1 :

app/code/Elightwalk/CustomerGrid/registration.php  1

php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
   \Magento\Framework\Component\ComponentRegistrar::MODULE,
   'Elightwalk_CustomerGrid',
   __DIR__
);

Step 2:

app/code/Elightwalk/CustomerGrid/etc/module.xml

xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
   <module name="Elightwalk_CustomerGrid" setup_version="1.0.0">
       <sequence>
           <module name="Magento_Customer"/>
       </sequence>
   </module>
</config>

Step 3 :

app/code/Elightwalk/CustomerGrid/view/adminhtml/ui_component/customer_listing.xml

xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
   <columns name="customer_columns" class="Magento\Customer\Ui\Component\Listing\Columns" >
       <column name="last_login" class="Elightwalk\CustomerGrid\Ui\Component\Listing\Column\LastLogin" component="Magento_Ui/js/grid/columns/date" sortOrder="90">
           <settings>
               <dataType>date</dataType>
               <label translate="true">Customer Last Login</label>
               <sortable>false</sortable>
               <filter>false</filter>
           </settings>
       </column>
   </columns>
</listing>

Step 4 :

app/code/Elightwalk/CustomerGrid/Ui/Component/Listing/Column/LastLogin.php

php

<?php
namespace Elightwalk\CustomerGrid\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

class LastLogin extends Column
{
   protected $logger;
   /**
    * 
    * @param ContextInterface   $context           
    * @param UiComponentFactory $uiComponentFactory   
    * @param array              $components        
    * @param array              $data              
    */
   public function __construct(
       ContextInterface $context,
       UiComponentFactory $uiComponentFactory,
       array $components = [],
       array $data = [],
       \Magento\Customer\Model\Logger  $logger
   ) {

       $this->logger = $logger;
       parent::__construct($context, $uiComponentFactory, $components, $data);
   }

   /**
    * Prepare Data Source
    *
    * @param array $dataSource
    * @return array
    */
   public function prepareDataSource(array $dataSource)
   {
       if (isset($dataSource['data']['items'])) {
           foreach ($dataSource['data']['items'] as & $item) {
               $item[$this->getData('name')] = $this->logger->get($item['entity_id'])->getLastLoginAt();//Value which you want to display
           }
       }
       return $dataSource;
   }
}

Step 5:

Inside your terminal or CMD

cmd

php bin/magento module:enable Elightwalk_CustomerGrid

 

php bin/magento setup:upgrade

 

php bin/magento setup:di:compile

 

php bin/magento cache:clean
 

Contact us:+91 8128405131

Email send us at hello@elightwalk.com

Jayram Prajapati
Full Stack Developer

Jayram Prajapati brings expertise and innovation to every project he takes on. His collaborative communication style, coupled with a receptiveness to new ideas, consistently leads to successful project outcomes.

Most Visited Blog

Password Option Change using the command!

With a single command, you can improve the security of your website! Explore our step-by-step guide to password options and creating a robust and unique authentication method for online platform.

Duplicate entry error When getting an order using REST API in Magento

When retrieving orders using the Magento REST API, troubleshoot the 'Duplicate Entry' problem. Our guide gives insights and solutions to ensure a seamless and error-free order retrieval experience on Magento platform.

How to add customer total order in the column customer grid in Magento 2?

Improve your client management with Magento 2! Learn how to add the order value to customer grid column step by step. Improve visibility and consumer insights for informed and efficient shopping experience.