2

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.

How can I show the last logged in date of online users in the grid?

Detail view filtered grid

4

Follow following step:

  1. 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>
  1. 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>
  1. 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.

6
  • what magento module am I overriding here? I don't really want to create my own view. – Philipp Sander Aug 22 '16 at 8:13
  • At least you need to create your own module, then follow step. – Sohel Rana Aug 22 '16 at 8:45
  • Here Vendor = your custom vendor name and Module = Your custom module name – Sohel Rana Aug 22 '16 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 – Philipp Sander Aug 25 '16 at 16:00
  • This solution will filters [exclude] the customers which haven't logged in into the site. – Rizwan Khan Jul 8 '19 at 7:52
0

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.

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.