🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Create Custom Customer Attribute in Magento 2

By Sanjay JethvaUpdated on Jun 16, 2025 2 min read

An attribute is a property of an entity that can be used in various Magento 2 store’s function.

Here we’ll talk about customer attribute. Create custom customer attribute in Magento 2 with the below solution.

Magento is a user-friendly platform and it allows creating custom attributes. Such customer custom attributes help in understanding the customer in a better way.

Some examples of custom customer attributes are customers’ mobile number, interests/hobbies, etc. which can be programmatically created using this method:

Method to Create Custom Customer Attribute in Magento 2:

<?php

namespace Vendor\Extension\Setup;

use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Model\Config as EavConfig;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Catalog\Model\Product;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
    /**
     * @var EavConfig
     */
    private $eavConfig;

    public function __construct(EavSetupFactory $eavSetupFactory, EavConfig $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavsetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $attributecode = 'interests';

        $eavsetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributecode, [
            'label' => 'Interests',
            'required' => 0,
            'user_defined' => 1,
            'note' => 'Separate Multiple Intrests with comms',
            'system' => 0,
            'position' => 100,
        ]);

        $eavsetup->addAttributeToSet(
            CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
            CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
            null,
            $attributecode
        );

        $attribute = $this->eavConfig->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributecode);
        $attribute->setData('used_in_forms', [
            'adminhtml_customer',
            'customer_account_create',
            'customer_account_edit'
        ]);

        $attribute->setData('validate_rules', [
            'input_validation' => 1,
            'min_text_length' => 3,
            'max_text_length' => 30,

        ]);
        $attribute->getResource()->save($attribute);
    }
}

Now, run the below commands:

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush
php bin/magento cache:clean

That’s it.

Feel free to share the solution among fellow Magento developers via social media.

Thank you.

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.