0

Here my question is not how to create customer attribute. I want to know the running flow explanation of this process? We can create this by below code in custom module.

<?php

namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;

public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
{
    $this->eavSetupFactory = $eavSetupFactory;
    $this->eavConfig       = $eavConfig;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'test_attribute',
        [
            'type'         => 'varchar',
            'label'        => 'test Attribute',
            'input'        => 'text',
            'required'     => false,
            'visible'      => true,
            'user_defined' => true,
            'position'     => 999,
            'system'       => 0,
        ]
    );
    $testAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'test_attribute');

    // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
    $testAttribute->setData(
        'used_in_forms',
        ['adminhtml_customer']

    );
    $testAttribute->save();
}
}

How can i get this customer attribute to show in grid?

If i want to add another attribute is it ok to add that attribute code below this? and run setup:upgrade? how the versions want to mention? Please explain.

1
  • If data still not update, then remove already created attribute and then, upgrade module. Dec 19, 2019 at 10:25

1 Answer 1

1

You need to add 'is_used_in_grid' => true, in your InstallData.php for attribute options.

<?php 

namespace Vendor\Module\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, 'test_attribute', [
            'test_attribute' => [
            'label' => 'Test Attribute',
            'type' => 'varchar',
            'input' => 'text',
            'position' => 1000,
            'visible' => true,
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
            'required' => false,
            'system' => 0,
            'user_defined' => true,
            'position' => 1000,
            'is_used_in_grid' => true,
            'is_visible_in_grid'    => true,
        ]
        ]);

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

        $attribute->save();
    }
}

Do setup upgrade again and check it.

2
  • Thank you. This worked for me.Any idea how to add a dropdown for the same attribute in grid like Gender?
    – Mujahidh
    Dec 19, 2019 at 11:05
  • You can set 'input' => 'select', and set your options in 'option' attribute. Happy coding !! :) Dec 19, 2019 at 11:09

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.