15

What are steps to create a custom attribute for Customer entity in Magento 2?

26

In the article Magento 2: How to make customer attribute? describe it step by step.

The main part is DataInstall::install method below:

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, '{attributeCode}', [
            'type' => 'varchar',
            'label' => '{attributeLabel}',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);
        //add attribute to attribute set
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'magento_username')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);

        $attribute->save();


    }
  • What is the benefit of injecting CustomerSetupFactory instead of directly injecting CustomerSetup? Thanks for explaining. – Vinai Feb 10 '16 at 5:41
  • @Vinai, Looks looks customerSetup class expects ModuleDataSetupInterface in constructor but this class is argument of install method. – KAndy Feb 10 '16 at 7:30
  • Since ModuleDataSetupInterface has no state that is specific to the setup class, wouldn't it be better to let the ObjectManager be responsible for creating the instance dependencies then? That way the CustomerSetup client would be less coupled to the implementation. As far as I can see. – Vinai Feb 10 '16 at 8:00
  • Removing the module doesn't remove the attribute, how should it be removed then ? – maxagaz Dec 29 '16 at 10:00
  • How can we add more than one fieds or attributes? – Jai Apr 7 '17 at 11:39
1

In your module, implement this file below that'll create a new Customer entity.

Test\CustomAttribute\Setup\InstallData.php

<?php
namespace test\CustomAttribute\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;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    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, 'custom_attribute', [
            'type' => 'varchar',
            'label' => 'Custom Attribute',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'position' =>999,
            'system' => 0,
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_attribute')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
        ]);

        $attribute->save();
    }
}
  • not working.... – Sarfaraj Jun 15 '18 at 11:40
  • This worked for me on Magneto 2.3 ibnab.com/en/blog/magento-2/… – Raivis Dejus Dec 3 '18 at 11:02
  • @Rafael Corrêa Gomes is it possible to create multiple attributes using this method? How? – Pragman Feb 18 at 0:50
  • @ZUBU you just need to add a new $customerSetup->addAttribute next of the first one, you can search for ->addAttribute into the core also to see references. – Rafael Corrêa Gomes Feb 19 at 15:31

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.