Magento – Magento 2 filter not working on custom customer attribute

customer-attributemagento2magento2.2

I have created custom customer attribute. It is displaying in customer grid in admin panel but when I try to filter on my custom attribute, I get error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customer_attribute' in 'where clause'.

This is because it's not available in table customer_grid_flat
I have already done re-indexing.

When I change value of is_used_in_grid to 1 from table customer_eav_attribute and start re indexing i am getting following error

PHP Fatal error: Uncaught Error: Call to undefined method Magento\Customer\Model\Indexer\Source::addAttributeToSelect() in /opt/lampp/htdocs/magento-221/vendor/magento/framework/Indexer/Handler/AttributeHandler.php:38

I have tried changing following column value to 1 but not working.

is_visible_in_grid
is_filterable_in_grid
is_searchable_in_grid

Here is setup file code

use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;

class CustomerSetup extends EavSetup {

    protected $eavConfig;

    public function __construct(
        ModuleDataSetupInterface $setup,
        Context $context,
        CacheInterface $cache,
        CollectionFactory $attrGroupCollectionFactory,
        Config $eavConfig
        ) {
        $this -> eavConfig = $eavConfig;
        parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
    } 

    public function installAttributes($customerSetup) {
        $this -> installCustomerAttributes($customerSetup);
        $this -> installCustomerAddressAttributes($customerSetup);
    } 

    public function installCustomerAttributes($customerSetup) {


        $customerSetup -> addAttribute(\Magento\Customer\Model\Customer::ENTITY,
            'customer_approval',
            [
            'label' => 'Customer Approve',
            'system' => 0,
            'position' => 108,
            'sort_order' =>108,
            'visible' =>  false,
            'note' => 'Customer Approval',


                        'type' => 'int',
                        'input' => 'boolean',
                        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',

            ]
            );

        $customerSetup -> getEavConfig() -> getAttribute('customer', 'customer_approval')->setData('is_user_defined',1)->setData('is_required',0)->setData('default_value','')->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']) -> save();


    } 

    public function installCustomerAddressAttributes($customerSetup) {

    } 

    public function getEavConfig() {
        return $this -> eavConfig;
    } 

Can anyone please let me know what is wrong with that.

Best Answer

First, you need to delete the attribute from the system and try the below code:

I think the problem with options and is_used_in_grid if you want to see this field in the grid is_used_in_grid should be yes (is_used_in_grid =1) then only you can apply the filter in the grid.

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, 'customer_approval', [
            'type'           => 'int',
            'label'          => 'customer approval',
            'input'          => 'bool',
            'option'         => ['values' => ['0', '1']],
            'source'         => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'required'       => 0,
            'sort_order'     => 210,
            'visible'        => 0,
            'position'       => 300,
            'admin_checkout' => 1,
            'system'         => 0,
            'is_null'        => false,
            'user_defined'   => true,
            'is_system'      => 0,
            'is_used_in_grid'              => 1,
            'is_visible_in_grid'           => 1,
            'is_filterable_in_grid'        => 1,
            'is_searchable_in_grid'        => 1,
            'is_used_for_customer_segment' => 1,
        ]);
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_approval');
        $attribute->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_form_attribute'],
        ]);
        $attribute->save();
}
Related Topic