Question

I need to add custom column and filter in the customer grid in Magento 2 admin. The column attribute is from different modules. There are many solutions for sales order grid, but none (or incomplete) for customer grid.

I had the same issue, so I found the solution by myself. The solution is in the answer.

Was it helpful?

Solution

OTHER TIPS

you can add custom column in customer grid by this

customer_listing.xml

path : app\code\vender\module\view\adminhtml\ui_component\

<?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="custom_columns_name_same_as_field_in_db">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">55</item>
                <item name="filter" xsi:type="string">text</item>
                <item name="label" translate="true" xsi:type="string">My Custom Column</item>
            </item>
        </argument>
    </column>
</columns>
</listing>

CustomerDataProvider.php

path : app\code\vender\module\Ui\Component\Listing\

<?php

namespace vender\module\Ui\Component\Listing;

class CustomerDataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
{
    protected function _initSelect()
    {
        parent::_initSelect();
        $this->getSelect()->joinLeft
        (
            ['custom_table_name' => $this->getTable('custom_table_name')],
            'main_table.entity_id = custom_table_name.customer_id',
            ['filed_you_want_to_display']
        );
        return $this;
    }
}

di.xml

path : app\code\vender\module\etc

<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Model\ResourceModel\Grid\Collection" type="Magento\Customer\Model\ResourceModel\Grid\Collection2" />
    <virtualType name="Magento\Customer\Model\ResourceModel\Grid\Collection2" type="Tatva\Corerewrite\Ui\Component\Listing\CustomerDataProvider">
        <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>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top