How to Create Custom Customer Attribute in Magento 2

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.

Table of Contents