Magento – Magento 2: Add custom attributes to customer grid

customer-attributecustomer-gridgridmagento2uicomponent

I want to add a custom attribute to the admin customer grid. For that, I have added two attributes using ui_component.

The columns show up in the grid but without data from customer_grid_flat (because Magento 2 uses a flat grid table for retrieving data to the grid).

I found the following answer on StackExchange:

add a custom column in customer grid

According to that, I added an indexer.xml file to my custom module:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
    <indexer id="customer_grid" view_id="customer_dummy" class="Magento\Framework\Indexer\Action\Entity" primary="customer">
        <fieldset name="customer" source="Magento\Customer\Model\ResourceModel\Customer\Collection" provider="Magento\Customer\Model\Indexer\AttributeProvider">
            <field name="webshop" xsi:type="filterable" dataType="int"/>
            <field name="aktiv" xsi:type="filterable" dataType="int"/>
        </fieldset>
    </indexer>
</config>

and executed php bin/magento indexer:reindex customer_grid.

Now, I face two different errors in two different conditions:

  1. Please note above indexer attribute view_id. it is customer_dummy (Which is default to native customer module). if I use the same view_id name in my custom module I get error Customer grid index is locked by another reindex process. Skipping

  2. If I use a different view_id name and execute php bin/magento indexer:reindex customer_grid then it shows view_id (in my case it just dummy) view does not exist.

Best Answer

Leaving out view_id seems to work:

<indexer id="customer_grid">

This way, the XML will be merged into the existing indexer instead of defining a new indexer.