Magento 2 is a popular eCommerce platform with robust features to create and manage online stores. One of the key advantages of Magento 2 is its flexibility to customize the store’s functionality per the business requirements. One such customization is adding customer attributes programmatically.
Magento 2 Customer attributes are additional fields that store the customer’s information, which can be used for personalization, segmentation, and targeted marketing.
In this article, we will discuss the step-by-step process of Magento 2 create customer attribute programmatically. This approach helps businesses efficiently capture and utilize valuable customer data for various marketing and operational needs.
As an example, we created a “subscription status” attribute for the customer entity to demonstrate how to implement customer attributes Magento in a practical scenario.
How To Add Magento 2 Customer Attributes
Follow the steps below to add Magento 2 Customer Attributes:
1. Create a module
Let’s start things off by creating a module in Magento, which is a self-contained unit of functionality that can be added to the platform to enhance its capabilities.
Creating Modules allow developers to add new features and functionality to the platform without modifying the core code. This means the platform can be updated more easily, and future upgrades and patches are less likely to break customizations. To create a module in Magento 2 follow these steps:
Step 1: Create the module directory
In your Magento installation, navigate to the app/code directory, and create a new directory named Humcommerce/CustomModule. This directory will contain the files and directories for your module.
Step 2: Create the module configuration file
In the Humcommerce/CustomModule/etc directory, create a file named module.xml. This file will contain the configuration information for your module, such as its name, version, and dependencies. Here is an example of what the file might contain:
```
<?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="Humcommerce_CustomModule" setup_version="1.0.0">
<sequence>
<module name="Magento_Customer"/>
</sequence>
</module>
</config>
```
In this example, the module is named Humcommerce_CustomModule and has a setup version of 1.0.0. It also has a dependency on the Magento_Catalog module.
Step 3: Create the module registration file
In the Humcommerce/CustomModule directory, create a file named registration.php. This file will register your module with Magento and load its configuration file. Here is an example of what the file might contain:
```
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Humcommerce_CustomModule',
__DIR__
);
```
Step 4: Create your module files
Within each of the subdirectories you created in step 4, create the necessary files for your module. For example, in the Block directory, you might create a file named CustomBlock.php that contains a custom block class. In the Controller directory, you might create a file named Index.php that contains a custom controller action. And so on for each of the other subdirectories.
Step 5: Enable your module
Finally, enable your module in Magento by running the following command from your Magento root directory:
```
php bin/magento module:enable Humcommerce_CustomModule
```
This will enable your module and make it available in your Magento store.
2. Patch to create Magento 2 Customer Attributes
After creating a module, you must create a patch file to add Magento 2 customer attributes to your store.
Step 1
First, Create a new file, ‘CustomerAttribute.php’ in the folder Humcommerce/CustomModule/Setup/Patch/Data
In this file, add the following code:
<?php declare(strict_types=1);
/**
* Patch to create Customer Attribute
*
*/
namespace HumcommerceCustomModuleSetupPatchData;
use MagentoEavModelConfig;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoEavModelEntityAttributeSourceBoolean;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
class CustomerAttribute implements DataPatchInterface
{
/**
* @var Config
*/
private $eavConfig;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* AddressAttribute constructor.
*
* @param Config $eavConfig
* @param EavSetupFactory $eavSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
Config $eavConfig,
EavSetupFactory $eavSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->eavConfig = $eavConfig;
$this->eavSetupFactory = $eavSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* {@inheritdoc}
*/
public static function getDependencies(): array
{
return [];
}
/**
* Create customer attribute
* @return void
*/
public function apply(): void
{
$eavSetup = $this->eavSetupFactory->create();
$customerEntity = $this->eavConfig->getEntityType(MagentoCustomerModelCustomer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$eavSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY, 'subscription_status', [
'type' => 'int',
'input' => 'boolean',
'label' => 'Subscription Active',
'visible' => false,
'source' => Boolean::class,
'required' => false,
'user_defined' => true,
'system' => false,
'global' => true,
'default' => 0,
'visible_on_front' => false,
'sort_order' => 50,
'position' => 50
]);
$customAttribute = $this->eavConfig->getAttribute(MagentoCustomerModelCustomer::ENTITY, 'subscription_status');
$customAttribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'customer_account_edit']
]);
$customAttribute->save();
}
public function getAliases()
{
return [];
}
}
```
Step 2
Save the file.
Step 3
Now, go to the command line and navigate to the root directory of your Magento installation.
Step 4
Run the following commands:
php bin/magento setup:upgrade
php bin/magento cache:clean
This will apply the patch and create the Instaquote subscription Active attribute for the customer entity.
Step 5
You can verify that the attribute has been created by going to the admin panel, navigating to Customers > Attributes > Manage Customer Attributes, and checking that the attribute named ‘Instaquote Subscription Active’ is present.

If you want to modify or delete the Magento 2 customer attribute, you can do so from the admin panel or by creating a new patch or script to perform the action. This flexibility makes managing customer attributes Magento straightforward and adaptable to your business needs.
How simple was that? You can easily add Magento 2 create customer attribute by following the above steps..
However, it’s important to note that any development activity associated with Magento requires a high level of knowledge and expertise in PHP. Any gaps in coding standards can have serious consequences, and you may compromise your store’s health and security.
f you don’t have developers who are well-versed in PHP, we advise you to hire a team of expert developers who can handle customer attributes Magento efficiently and ensure your store’s functionality remains secure and optimized.