i want to add a custom column in customer grid like "is_approved" customer section. please suggest.

2 Answers 2

  • First you need to create an customer attribute is_approve


Note Make sure you add 'is_used_in_grid' => true, in your attribute options or you may get sql error.


namespace Namespace\Modulename\Setup; 

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface

     * @var \Magento\Customer\Setup\CustomerSetupFactory
    private $customerSetupFactory;

     * @var \Magento\Eav\Model\Entity\Attribute\SetFactor
    private $attributeSetFactory;    

     * Init
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
         /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(Customer::ENTITY, 'is_approve', [
            'type' => 'int',
            'label' => 'Is Approved',
            'input' => 'select',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1001,
            'position' => 1001,
            'is_used_in_grid' => true,
            'system' => 0,
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_approve')
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],

  • Display it in the customer listing create 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="is_approve">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">select</item>
                    <item name="editor" xsi:type="string">select</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                    <item name="dataType" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">Is Approved</item>
                    <item name="sortOrder" xsi:type="number">51</item>
  • Finally add the attribute to the customer grid index


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
    <indexer id="customer_grid">
        <fieldset name="customer">
            <field name="is_approve" xsi:type="filterable" dataType="int"/>
  • run command php bin/magento indexer:reindex and check.
  • but i think custom attribute is not created and all my customers are not showing now
    – Jay Kapoor
    Commented Aug 10, 2017 at 5:39
  • after removing this code all customers are displaying
    – Jay Kapoor
    Commented Aug 10, 2017 at 6:00
  • Check your database if your attribute is created. Also check in logs if there are any errors found.
    – Priyank
    Commented Aug 10, 2017 at 6:00
  • check which block is getting error. There must be some typo error. It works fine for me
    – Priyank
    Commented Aug 10, 2017 at 6:01
  • which table to check in database
    – Jay Kapoor
    Commented Aug 10, 2017 at 6:11

Here is my solution. I have created my own module. My custom attribute is from another table and another module so ihave created this module.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.