How to Create Customer Attribute in Magento 2 Programmatically

by admin

Are you finding how to create customer attribute in Magento 2 programmatically? The article is your answer. Follow our guide below, we use a simple module demo coding for this lesson, so for the first, you need to read the tutorial here to create a simple module as BSS_HelloWorld. And now are the instructions to create customer attribute in Magento 2 programmatically. 

Step by step to create customer attribute in Magento 2 programmatically

Step 1: Create a setup file InstallData.php

Firstly, we need to create the InstallData.php file located at file: app/code/BSS/HelloWorld/Setup/InstallData.php.

<?php
namespace BSS\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;
       }
}

Step 2: Define the install() Method

After this step, we define the install() method and create eav setup model by using the code below:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
       {
              $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
       }

And now, we use this eav setup model to create customer attribute in Magento 2:

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

Finally, we set the forms for the attributes that will be used. For that, we need to define the eavConfig object that will allow us to call the attribute back and set the data.

<?php
namespace BSS\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();
       }
}

Now, let us run this command line to install the module:

php magento setup:upgrade and php bin/magento 
setup:static-content:deploy

Why need to create customer attribute in Magento 2

Customer Attribute is a crucial feature for Magento business. It helps customize your customer information, so you can serve them better. Besides, you can all their customer’s details when they register, edit their accounts, or during admin checkout. These attributes also help in making targeted promotions more specific by refining cart price rules. 

In Magento 2 Enterprise Edition, one great thing is that the admin can add custom Customer Attributes based on what the business needs. Magento Commerce gives an easy interface for creating and handling these attributes. However, Magento Open Source requires additions or updates to customer attributes to be handled by a developer in the database or through upgrade scripts. Magento Ecommerce can make it easier for your admin. 

Conclusion 

Customer attributes extension provides all the necessary functions for creating extra fields and adding them to any Sign-up Form position. With clear instructions on how to create customer attribute in Magento 2, we hope to have provided you with the right direction. 

create-customer-attribute-in-magento-2Besides, if through this guide you still encounter knowledge, we highly recommend you to an easier option: our Magento 2 Customer Attributes.

I believe that, with the support of the leading provider in the Magento industry – BSS Commerce, you will easily manage your Magento store most effectively. And if you have any other questions about Magento commerce, don’t hesitate to contact us.

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.