Hello Everyone,
In this blog, we will learn about how to Create Custom Customer Attribute using Data Patch in Magento 2.
Data Patch feature is introduced from Magento 2.3.x.
Without wasting your time, let us guide you straight away. Follow the easy step given below to Create Customer Attribute in Magento 2.
STEPS FOR CREATE CUSTOM CUSTOMER ATTRIBUTE IN MAGENTO 2
Step 1: Create CustomerAttribute.php file
app/code/Vendor/Extension/Setup/Patch/Data/CustomerAttribute.php
<?php
namespace Vendor\Extension\Setup\Patch\Data;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\Set;
use Magento\Eav\Model\Entity\Attribute\SetFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
class CustomerAttribute implements DataPatchInterface, PatchRevertableInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var CustomerSetup
*/
private $customerSetupFactory;
/**
* @var SetFactory
*/
private $attributeSetFactory;
/**
* Constructor
*
* @param ModuleDataSetupInterface $moduleDataSetup
* @param CustomerSetupFactory $customerSetupFactory
* @param SetFactory $attributeSetFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory,
SetFactory $attributeSetFactory
)
{
$this->moduleDataSetup = $moduleDataSetup;
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* {@inheritdoc}
*/
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create([‘setup’ => $this->moduleDataSetup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet Set */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(
Customer::ENTITY,
‘mobile_number’,
[
‘input’ => ‘text’,
‘type’ => ‘varchar’,
‘label’ => ‘Mobile Number’,
‘source’ => ”,
‘required’ => false,
‘position’ => 40,
‘visible’ => true,
‘system’ => false,
‘is_used_in_grid’ => true,
‘is_visible_in_grid’ => true,
‘is_filterable_in_grid’ => true,
‘is_searchable_in_grid’ => false,
‘backend’ => ”
]
);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, ‘mobile_number’);
$attribute->addData([
‘used_in_forms’ => [
‘adminhtml_customer’,
‘customer_account_create’,
‘customer_account_edit’
]
]);
$attribute->addData([
‘attribute_set_id’ => $attributeSetId,
‘attribute_group_id’ => $attributeGroupId
]);
$attribute->save();
$this->moduleDataSetup->getConnection()->endSetup();
}
public function revert()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create([‘setup’ => $this->moduleDataSetup]);
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, ‘mobile_number’);
$this->moduleDataSetup->getConnection()->endSetup();
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
}
In above code we created text type attribute you can change as per your requirements.
Step 2: run the below commands
$ php bin/magento setup:upgrade
$ php bin/magento cache:clean
$ php bin/magento cache:flush
Step 3: Output:

Above created custom customer attribute is stored in eav_attribute table.
Now go to Admin -> Customers -> All Customers -> Edit -> Account Information.
Final Thoughts:
This is how you can Create Custom Customer Attribute in Magento 2. Hope you liked the blog.
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.