Magento, Magento 2

How to Add Custom Customer Attribute in Magento 2

How to Add Custom Customer Attribute in Magento 2

In this tutorial, Today I will explain you about how to add custom customer attribute in Magento 2. For that, You need to create InstallData.php file in your custom module. When you execute setup upgrade command for install module at that time, your custom customer attribute will be create by script.

Custom Customer Attribute is useful when you want to add custom field in customer address form and customer account page. After that, You can also get custom attribute value in customer object.

For example, you want to add mobile number field in customer account page. Then, you can create field by custom customer attribute.

First of all, you should have your own Magento 2 Extension. See how to create your module here.

Let’s follow the below steps to Create Custom Customer Attribute in Magento 2 :

You may also like this :

1) Create InstallData.php file at app/code/RH/Helloworld/Setup/ and paste the below code :

<?php

/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Created By : Rohan Hapani
 */

namespace RH\Helloworld\Setup;

use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

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

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            Customer::ENTITY,
            'customer_temp_attribute',
            [
                'type' => 'varchar',
                'label' => 'Custom Temp Attribute',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 1000,
                'position' => 1000,
                'system' => 0,
            ]
        );
        $customAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'customer_temp_attribute');

        /**
         * you can below used_in_forms types to set attribute in forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
         */
        $customAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer']

        );
        $customAttribute->save();
    }
}

You can set value in used_in_forms based on your requirement to display custom attribute field in different forms. I added different types as comment in above script.

2) After that, you need to upgrade your module using below command :

php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c

That’s it !!!

You can check this custom attribute in eav_attribute table that attribute creates successfully or not.

I hope this blog is easy to understand about how to add custom customer attribute in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Keep liking and sharing 🙂

Tagged ,