In this blog, I created customer attribute programmatically in magento 2. Code is supported to php8 and magento 2.4.6 😍
Let’s see, how to create customer attribute programmatically.
We are creating customer attribute name external_id.
First, Create data patch class like AddExternalIdCustomerAttribute.php
file at path app/code/Vendor/Module/Setup/Patch/Data/AddExternalIdCustomerAttribute.php
<?php
declare(strict_types=1);
namespace MageDad\Module\Setup\Patch\Data;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Model\ResourceModel\Attribute as AttributeResource;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
/**
* Creates a customer attribute for managing a customer's external system ID
*/
class AddExternalIdCustomerAttribute implements DataPatchInterface
{
public function __construct(
private ModuleDataSetupInterface $moduleDataSetup,
private CustomerSetupFactory $customerSetupFactory,
private AttributeResource $attributeResource,
) { }
/**
* Run code inside patch
*/
public function apply()
{
// Start setup
$this->moduleDataSetup->getConnection()->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerSetup->addAttribute(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
'external_id',
[
'type' => 'text',
'label' => 'External ID',
'input' => 'text',
'required' => false,
'visible' => false,
'user_defined' => true,
'position' => 200,
'system' => false,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
],
);
$customerSetup->addAttributeToSet(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
$customerSetup->getDefaultAttributeGroupId(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER),
'external_id'
);
// Get the newly created attribute's model
$attribute = $customerSetup->getEavConfig()
->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'external_id');
// Make attribute visible in Admin customer form
$attribute->setData('used_in_forms', [
'adminhtml_customer',
'customer_account_create',
'customer_account_edit'
]);
// Save attribute using its resource model
$this->attributeResource->save($attribute);
// End setup
$this->moduleDataSetup->getConnection()->endSetup();
}
public function getAliases(): array
{
return [];
}
public static function getDependencies(): array
{
return [];
}
}
After create patch run magento setup upgrade command to create customer attribute.php bin/magento setup:upgrade
We can see newly created customer attribute in customer edit/add.

I hope this blog is useful for create customer attribute programmatically in magento 2. In case, I missed anything or need to add some more information, Don’t heisted to leave a comment in this blog, I’ll get back with some positive approach.
Keep loving ❤️ Keep inspiring 🤩 Keep liking 👍 No sharing 😄
12 Comments
Your article helped me a lot, is there any more related content? Thanks!
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Your article helped me a lot, is there any more related content? Thanks!
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://accounts.binance.com/ES_la/register?ref=T7KCZASX
Your article helped me a lot, is there any more related content? Thanks!
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Your article helped me a lot, is there any more related content? Thanks!
Explore the ranked best online casinos of 2025. Compare bonuses, game selections, and trustworthiness of top platforms for secure and rewarding gameplayBonus offer.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Your article helped me a lot, is there any more related content? Thanks! https://accounts.binance.com/ES_la/register-person?ref=T7KCZASX
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?