2

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.

5

Here is the module i have created.

https://drive.google.com/open?id=13l29KVZSekCBEmzNtuxc3FsqjoCI9UL1

  • I don't have any custom table now, I just created an attribute in the customer, and created a customer_listing.xml and a data provider, so that case how can I use this with out any custom table – Naveenbos Nov 23 '18 at 5:55
  • fix the issues it has some fatal errors – HaFiz Umer Sep 27 at 11:29
1

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>
  • I have already tried this solution but it didn't work. So, i made module which worked for me. – Ankit8 Aug 3 '18 at 6:27

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.