0

Using Magento ver. 2.2.6, how can I add new columns to customer grid showing the last logged-in activity and Last order date?

1
1

I created extension

https://github.com/DominicWatts/CustomerInfo

But in summary

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>
        <column name="order_created_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 Order At</item>
                    <item name="sortOrder" xsi:type="number">1000</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Model/ResourceModel/Grid/Collection.php

<?php
namespace Xigen\CustomerInfo\Model\ResourceModel\Grid;
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
/**
 * Class Collection
 * @package Xigen\CustomerInfo\Model\ResourceModel\Grid
 */
class Collection extends SearchResult
{
    /**
     * 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']
        )->joinLeft(
            ['sales_order_grid' => $this->getTable('sales_order_grid')],
            'sales_order_grid.customer_id = customer_log.customer_id',
            ['max(sales_order_grid.created_at) as order_created_at']
        )
        ->group('main_table.entity_id');
        $this->addFilterToMap('customer_id', 'customer_log.customer_id');
        $this->addFilterToMap('created_at', 'main_table.created_at');
        // echo (string) $this->getSelect();
        return $this;
    }
}

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.