Create Customer Attribute in Magento2

In Magento2, to create new custom attribute for customer you need your custom module.

If you have already any module installed in your magento then you can use UpgradeData.phpΒ file to create new customer attribute or if you don’t have any custom module then you can create new attribute by usingΒ InstallData.phpΒ file.

So first of all let’s see how to create new customer attribute withΒ InstallData.phpΒ file.

Using InstallData.php

For that you need to create new custom module withΒ registration.phpΒ file andΒ etc/module.xml file. so let’s start…

1.) Create your namespace directory and module directory..

In this step, go to you Magento 2 root directory and navigate insideΒ app/codeΒ directory and inside that directory create new directory and add your namespace and move inside it and create another subdirectory there with your module name.

2.) Add registration.php file.

In second step, you will need to createΒ registration.phpΒ file here.

app/code/Thecoachsmb/CustomerAttribute/registration.php

Content for this file is..

<?php
/**
 * @package   Thecoachsmb\CustomerAttribute
 */
use \Magento\Framework\Component\ComponentRegistrar as Registrar;
Registrar::register( Registrar::MODULE, 'Thecoachsmb_CustomerAttribute', __DIR__ );

3.) Create module.xml file.

CreateΒ module.xml Β file inside module’sΒ etc/Β directory here

app/code/Thecoachsmb/CustomerAttribute/etc/module.xml

Content for this file is..

<?xml version="1.0"?>
<!--
/**
 * @package   Thecoachsmb\CustomerAttribute
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:CustomerAttribute/etc/module.xsd">
    <module name="Thecoachsmb_CustomerAttribute" setup_version="1.0.0" />
</config>

4.) Create InstallData.php file.

CreateΒ SetupΒ name directory inside your module and move into that directory and createΒ InstallData.phpΒ file there..

app/code/Thecoachsmb/CustomerAttribute/Setup/InstallData.php

Content for this file is..

<?php

/**
Β * @package Β  Thecoachsmb\CustomerAttribute
Β */

namespace Thecoachsmb\CustomerAttribute\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Model\Config;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class InstallData implements InstallDataInterface
{
Β  Β  /**
Β  Β  Β * @var Config
Β  Β  Β */
Β  Β  private $eavConfig;

Β  Β  /**
Β  Β  Β * @var EavSetupFactory
Β  Β  Β */
Β  Β  private $eavSetupFactory;
Β  Β  /**
Β  Β  Β * @var AttributeSetFactory
Β  Β  Β */
Β  Β  private $attributeSetFactory;

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

Β  Β  public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context){
Β  Β  Β  Β  $customerEntity = $this->eavConfig->getEntityType('customer');
Β  Β  Β  Β  $attributeSetId = $customerEntity->getDefaultAttributeSetId();
Β  Β  Β  Β  $attributeSet = $this->attributeSetFactory->create();
Β  Β  Β  Β  $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
Β  Β  Β  Β  $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
Β  Β  Β  Β  $eavSetup->addAttribute(
Β  Β  Β  Β  Β  Β  Customer::ENTITY,
Β  Β  Β  Β  Β  Β  'customer_profile_link',
Β  Β  Β  Β  Β  Β  [
Β  Β  Β  Β  Β  Β  Β  Β  'type' => 'varchar',
Β  Β  Β  Β  Β  Β  Β  Β  'label' => 'Customer Profile Link',
Β  Β  Β  Β  Β  Β  Β  Β  'input' => 'text',
Β  Β  Β  Β  Β  Β  Β  Β  'required' => false,
Β  Β  Β  Β  Β  Β  Β  Β  'visible' => true,
Β  Β  Β  Β  Β  Β  Β  Β  'user_defined' => true,
Β  Β  Β  Β  Β  Β  Β  Β  'sort_order' => 50,
Β  Β  Β  Β  Β  Β  Β  Β  'position' => 50,
Β  Β  Β  Β  Β  Β  Β  Β  'system' => 0,
Β  Β  Β  Β  Β  Β  Β  Β  'adminhtml_only' => 1,
Β  Β  Β  Β  Β  Β  ]
Β  Β  Β  Β  );

Β  Β  Β  Β  $customAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'customer_profile_link');

  Β  Β  Β  $customAttribute->addData([
Β  Β  Β  Β  Β  Β  'attribute_set_id' => $attributeSetId,
Β  Β  Β  Β  Β  Β  'attribute_group_id' => $attributeGroupId,
Β  Β  Β  Β  Β  Β  'used_in_forms' => ['adminhtml_customer']
Β  Β  Β  Β  ]);

Β  Β  Β  Β  $customAttribute->save();
Β  Β  }
}

Here in above file, I added new attributeΒ Customer profile linkΒ so we can use it in create account form and customer can put link and they can submit form.
Now, run below commands to activate new module and register it.

php bin/magento module:enable Thecoachsmb_CustomerAttribute
php bin/magento setup:upgrade

Above command will enable new module and register, also it’ll create new customer attribute. You can check your newly created attribute inΒ eav_attributeΒ table in your database.

Now, if you have any existing module already then you can follow below steps to create new customer attribute with the use of UpgradeData.php file..

2. Using UpgradeData.php

Create UpgradeData.php file inside Setup directory.

app/code/Thecoachsmb/CustomerAttribute/Setup/UpgradeData.php

Content for this file is..

<?php
/**
 * @package   Thecoachsmb\CustomerAttribute
 */

namespace Thecoachsmb\CustomerAttribute\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Model\Config;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class UpgradeData implements UpgradeDataInterface
{
    private $eavSetupFactory;

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

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context){
          $customerEntity = $this->eavConfig->getEntityType('customer'); Β  Β  Β  Β  
          $attributeSetId = $customerEntity->getDefaultAttributeSetId(); Β  Β  Β  Β  
          $attributeSet = $this->attributeSetFactory->create(); Β  Β  Β  Β  
          $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);        
         $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        if ($context->getVersion()
            && version_compare($context->getVersion(), '1.0.1') < 0
        ) {
            $eavSetup->addAttribute(
                Customer::ENTITY,
                'instagram_profile_link',
                [
                    'type' => 'varchar',
                    'label' => 'Instagram Profile link',
                    'input' => 'text',
                    'required' => false,
                    'visible' => true,
                    'user_defined' => true,
                    'sort_order' => 50,
                    'position' => 50,
                    'system' => 0,
                ]
            );
            $customAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'instagram_profile_link');
            $customAttribute->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer']
            ]);
        }
        $customAttribute->save();
    }
}

Now you have to changeΒ setup_versionΒ in yourΒ etc/module.xmlΒ file, we hadΒ 1.0.0Β version previously, so we will change it fromΒ 1.0.0Β toΒ 1.0.1. So let’s do it.

app/code/Thecoachsmb/CustomerAttribute/etc/module.xml

Updated content after version change is liike below…

<?xml version="1.0"?>
<!--
/**
 * @package   Thecoachsmb\CustomerAttribute
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:CustomerAttribute/etc/module.xsd">
    <module name="Thecoachsmb_CustomerAttribute" setup_version="1.0.1" />
</config>

Now, you need to upgrade setup so run below command, and it will create your new attributeΒ instagram_profile_link. You can setΒ used_in_formsΒ value based on your requirements. You can set following options in that array..

  • customer_account_create,
  • customer_account_edit,
  • checkout_register,
  • adminhtml_customer_address,
  • customer_address_edit,
  • customer_register_address

Depending upon in which forms you want to display this attribute, you can mention these name sin used_in_forms.
Then run the below command.

php bin/magento setup:upgrade