If you’re looking to extend customer data in your Magento store, adding a custom customer attribute is a powerful solution. Whether you want to collect additional information during registration or personalize the customer experience, Magento 2 makes this possible through custom attributes.
In this step-by-step guide, we’ll show you how to create a customer attribute in Magento 2 programmatically using InstallData.php. We'll also touch on how to update, retrieve, and add customer address attributes in Magento 2. Let’s dive in!
Why Create a Custom Customer Attribute?
- Custom customer attributes allow store owners to:
- Capture extra details (like profession, referral source, etc.)
- Personalize customer experiences
- Enable better segmentation and marketing
- Use custom attributes in checkout, admin, or registration forms
To know how to create a simple module, click here.
Also read: How to Check if Attribute is Swatch Attribute in Magento 2?
Table of Contents
How to Create Customer Attributes Programmatically in Magento 2?
Here are the steps to create a customer custom attribute programmatically in Magento 2.
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!
Final Words
Adding custom customer attributes in Magento 2 lets you personalize your store and collect valuable insights. Whether you’re looking to create customer address attribute, add new fields to registration, or update attribute behavior, Magento offers a flexible EAV model to do so programmatically.
PS: You can also create custom attributes with MageDelight Customer Attributes Magento 2 Extension.



