In today's article, we will guide you about steps to add customer attributes programmatically in Magento 2. Customer attributes help in collecting additional information from customers and offering personalized experience using that information. First of all, we'll create a simple module and name it Magedelight_Helloworld and use it here.
To know how to create a simple module, click here.
Also read: How to Check if Attribute is Swatch Attribute in Magento 2?
Steps to Create Customer Attributes in Magento 2 Programmatically:
Step 1. Create a setup file InstallData.php.
Step 2. Define the install() Method.
Step 3. Create Custom Attribute.
Let's learn each step in-depth.
Step 1: Create a setup file InstallData.php
Firstly, we will create InstallData.php file.
It will be located at app/code/Magedelight/HelloWorld/Setup/InstallData.php
<?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; } }
Step 2: Define the install() Method
Now, we will define the install() method and create eav setup model using the below-mentioned code:
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); }
Next, we will use eavSetup object to add the 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
In the end, we would be required to set the forms in which the attributes will be used. We need to define the eavConfig object that will allow us to call the attribute back and set the data for it.
<?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(); } }
Now, let us run the command line to install the module:
php magento setup:upgrade and php bin/magento setup:static-content:deploy
There you go! You'll have your sample attribute ready. We hope we've made every step clear. But if you still need our professional help with your Magento website development project, feel free to reach us out!
PS: You can also create custom attributes with MageDelight Customer Attributes Magento 2 Extension.