Magento 2: How to Add Customer Attributes

Magento 2 is a robust and highly customizable eCommerce platform. One of its powerful features is the ability to extend the customer entity by adding Magento 2 add customer attribute functionality. This allows store owners to collect additional information about customers for personalization, segmentation, and improved user experience.
In this guide, we will explore how to add a customer attribute in Magento 2 programmatically.
Why Add Customer Attributes in Magento 2?
Adding customer attributes in Magento 2 can be useful for various reasons, such as:
- Collecting additional customer details like date of birth, gender, or preferences.
- Customizing the registration or checkout process.
- Enhancing customer segmentation and marketing strategies.
- Improving customer experience by offering personalized services.
Steps to Add a Customer Attribute in Magento 2
To add a custom attribute for customers, follow these steps:
Step 1: Create a Custom Module
Before adding an attribute, you need to create a custom module. If you already have a module, you can skip this step.
- Create the module folder structure:
app/code/Vendor/CustomAttribute/
Create the module declaration file module.xml in app/code/Vendor/CustomAttribute/etc/:
<?xml version=”1.0″?>
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:Module/etc/module.xsd”>
<module name=”Vendor_CustomAttribute” setup_version=”1.0.0″/>
- </config>
Register the module by creating registration.php in app/code/Vendor/CustomAttribute/:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
‘Vendor_CustomAttribute’,
__DIR__
- );
Run the following command to enable the module:
php bin/magento module:enable Vendor_CustomAttribute
- php bin/magento setup:upgrade
Step 2: Create a Setup Script to Add the Customer Attribute
Now, create a setup script to define the new attribute.
Create the app/code/Vendor/CustomAttribute/Setup/Patch/Data/AddCustomerAttribute.php file:
<?php
namespace Vendor\CustomAttribute\Setup\Patch\Data;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Customer\Model\Customer;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class AddCustomerAttribute implements DataPatchInterface
{
private $eavSetupFactory;
private $moduleDataSetup;
public function __construct(
EavSetupFactory $eavSetupFactory,
ModuleDataSetupInterface $moduleDataSetup
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->moduleDataSetup = $moduleDataSetup;
}
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
$eavSetup = $this->eavSetupFactory->create([‘setup’ => $this->moduleDataSetup]);
$eavSetup->addAttribute(
Customer::ENTITY,
‘custom_attribute’,
[
‘type’ => ‘varchar’,
‘label’ => ‘Custom Attribute’,
‘input’ => ‘text’,
‘required’ => false,
‘visible’ => true,
‘user_defined’ => true,
‘position’ => 100,
‘system’ => false,
‘is_used_in_grid’ => true,
‘is_visible_in_grid’ => true,
‘is_filterable_in_grid’ => true,
‘is_searchable_in_grid’ => true,
]
);
$this->moduleDataSetup->getConnection()->endSetup();
}
public static function getDependencies()
{
return [];
}
public function getAliases()
{
return [];
}
- }
Run the following command to apply the patch:
php bin/magento setup:upgrade
- php bin/magento cache:flush
Step 3: Add Attribute to Customer Forms
To make the custom attribute appear on customer forms (registration, account, admin), update the customer_eav_attribute table.
Run the following MySQL query:
INSERT INTO customer_form_attribute (form_code, attribute_id)
SELECT ‘customer_account_create’, attribute_id FROM eav_attribute WHERE attribute_code = ‘custom_attribute’;
Repeat the query for other form codes like:
- customer_account_edit
- adminhtml_customer
Step 4: Verify the Attribute
After completing the setup, you can check the new attribute in the Magento admin panel:
- Go to Customers > All Customers.
- Edit any customer and verify if the new attribute appears.
- Test by updating and saving the attribute value.
Conclusion
Adding customer attributes in Magento 2 helps enhance customer data management, improve personalization, and optimize marketing strategies. By following this guide, you can successfully create and manage custom customer attributes to meet your store’s needs.