Create Customer Attributes in Magento 2

How to Create Customer Attributes in Magento 2?

Today, we’re going to teach you guys how to create customer attributes in Magento 2.

An attribute is basically a property of an entity that is often used in different types of Magento 2 store’s functions.

Since Magento 2 is a highly user-friendly eCommerce platform, it allows its users to create custom attributes as per their needs.

And Customer attribute is one of the important attributes that help store owners to understand their customers in a better way.

In case if you’re still not sure what customer attribute is, it is basically customers’ mobile number, interests, hobbies, etc.

And in this article, we will show you how to create customer attributes in your  Magento 2 store.

Steps to Create Customer Attributes in Magento 2

Step #1 – Create Setup file: InstallData.php

First of all, you will need to create an InstallData.php file in the app/code/MageDelight/HelloWorld/Setup/ folder and copy the following code:

<?php

namespace MageDelight\HelloWorld\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;

class InstallData implements InstallDataInterface

{

    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)

    {

       $this->eavSetupFactory = $eavSetupFactory;

    }

}

In the above code, we have basically defined the EAV setup model that will be used for interaction with the new customer attribute.

Step #2 – Define install() Method

Now, we also need to define the install() method and create the EAV setup model with the following code:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

    {

       $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    }

    Next, we will use eavSetup object to add attribute:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

    {

       $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

       $eavSetup->addAttribute(

           \Magento\Customer\Model\Customer::ENTITY,

           'sample_attribute',

           [

               'type'         => 'varchar',

               'label'        => 'Sample Attribute',

               'input'        => 'text',

               'required'     => false,

               'visible'      => true,

               'user_defined' => true,

               'position'     => 999,

               'system'       => 0,

           ]

       );

}

Step #3 – Create Custom Attribute

Lastly, we will need to decide the forms in which the new customer attribute will be displayed.

For this, we will have to define eavConfig object in order to call the attribute back and set data in it.

And to do this, copy the following code in the same InstallData.php file.

<?php

namespace MageDelight\HelloWorld\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,

           'sample_attribute',

           [

               'type'         => 'varchar',

               'label'        => 'Sample Attribute',

               'input'        => 'text',

               'required'     => false,

               'visible'      => true,

               'user_defined' => true,

               'position'     => 999,

               'system'       => 0,

           ]

       );

       $sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');

       // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']

       $sampleAttribute->setData(

           'used_in_forms',

           ['adminhtml_customer']

       );

       $sampleAttribute->save();

    }

}

And it’s done!

Now, execute the following commands to install the module.

php magento setup:upgrade

php bin/magento setup:static-content:deploy

After you’ve executed both the commands, it’s time to check the output.

Also read: How to Create Product Attributes in Magento 2?

Conclusion

This is how easy it is to create customer attributes in a Magento 2 store.

And if you need our professional assistance, feel free to contact us at any time.

Recommended Read: Magento 2 Customer Attribute Extension

Tags