In the customer grid, which is accessable through the main menu point Customers
I have a user that has a blank last logged in
column. When I look in the details off that user by clicking the edit
link in the row I can clearly see, that the user has logged in AND is still online.
3 Answers
Follow following step:
- Create Vendor/Module/view/adminhtml/ui_component/customer_listing.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_at"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="filter" xsi:type="string">text</item> <item name="label" xsi:type="string" translate="true">Last Logged In</item> <item name="sortOrder" xsi:type="number">1000</item> </item> </argument> </column> </columns> </listing>
- Create Vendor/Module/etc/di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory"> <arguments> <argument name="collections" xsi:type="array"> <item name="customer_listing_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\Grid\Collection</item> </argument> </arguments> </type> <virtualType name="Vendor\Module\Model\ResourceModel\Grid\Collection"> <arguments> <argument name="mainTable" xsi:type="string">customer_grid_flat</argument> <argument name="resourceModel" xsi:type="string">Magento\Customer\Model\ResourceModel\Customer</argument> </arguments> </virtualType> </config>
- Create Vendor/Module/Model/ResourceModel/Grid/Collection.php
namespace Vendor\Module\Model\ResourceModel\Grid; use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult; class Collection extends SearchResult { /** * Init collection select * * @return $this */ protected function _initSelect() { parent::_initSelect(); $this->getSelect()->join( ['customer_log' => $this->getTable('customer_log')], 'customer_log.customer_id = main_table.entity_id', ['last_login_at'] ); return $this; } }
Clear cache.
-
what magento module am I overriding here? I don't really want to create my own view. Commented Aug 22, 2016 at 8:13
-
At least you need to create your own module, then follow step. Commented Aug 22, 2016 at 8:45
-
Here Vendor = your custom vendor name and Module = Your custom module name Commented Aug 22, 2016 at 8:45
-
thank you. it worked. I also added
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
to keep a consistent formatting Commented Aug 25, 2016 at 16:00 -
This solution will filters [exclude] the customers which haven't logged in into the site. Commented Jul 8, 2019 at 7:52
Use $this->getSelect()->joinLeft(
instant of $this->getSelect()->join(
, This way you could show all the customers. Otherwise it will show only customers who have logged in before.
@sohel-rana answer works. However, I will suggest some adjusts:
- In my customer_listing.xml to display the value and the filter as a date, I add the class Magento\Ui\Component\Listing\Columns\Date that filter type dateRange and the dataType date, also the component Magento_Ui/js/grid/columns/date. The final file looks like this:
<?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_at" class="Magento\Ui\Component\Listing\Columns\Date"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="filter" xsi:type="string">dateRange</item> <item name="visible" xsi:type="boolean">true</item> <item name="label" xsi:type="string" translate="true">Last Logged In</item> <item name="dataType" xsi:type="string">date</item> <item name="sortOrder" xsi:type="number">100</item> <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item> </item> </argument> </column> </columns> </listing>
- If you have another extension or module that adds additional fields to grid select (for example the Mgento B2B Module) you could have some issues to display that additional data. But I fixed it in my Vendor/Module/Model/ResourceModel/Grid/Collection.php changed the extends to \Magento\Customer\Model\ResourceModel\Grid\Collection Additional, to display customer that doesn't have any last login, I changed the join to joinLeft
<?php namespace Vendor\Module\Model\ResourceModel\Customer\Grid; class Collection extends \Magento\Customer\Model\ResourceModel\Grid\Collection { /** * Init collection select * * @return $this */ protected function _initSelect(){ parent::_initSelect(); $this->getSelect()->joinLeft( ['customer_log' => $this->getTable('customer_log')], 'customer_log.customer_id = main_table.entity_id', ['last_login_at'] ); return $this; } } ?>