How to Add Customer Attribute Programmatically in Magento 2?

In this tutorial, you will go through how to add customer custom attribute in Magento 2. Please follow below steps to create customer custom attribute for your store.

Add Customer Custom Attribute in Magento 2

To create customer custom attribute in Magento 2, follow these steps:

Step 1: Firstly, you need to create InstallData.php file in your Setup folder:

<?php
namespace WebShouters\CustomAttribute\Setup;

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{

/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;

/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;

/**
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create([‘setup’ => $setup]);

$customerEntity = $customerSetup->getEavConfig()->getEntityType(‘customer’);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();

/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

$customerSetup->addAttribute(Customer::ENTITY, ‘custom_attribute’, [
‘type’ => ‘varchar’,
‘label’ => ‘Custom Attribute’,
‘input’ => ‘text’,
‘required’ => false,
‘visible’ => true,
‘user_defined’ => true,
‘position’ =>999,
‘system’ => 0,
]);

$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, ‘custom_attribute’)
->addData([
‘attribute_set_id’ => $attributeSetId,
‘attribute_group_id’ => $attributeGroupId,
‘used_in_forms’ => [‘adminhtml_customer’], //you can use other forms also [‘adminhtml_customer_address’, ‘customer_address_edit’, ‘customer_register_address’]
]);

$attribute->save();
}
}

Step 2: After all of that, you need open SSH and run command:

php bin/magento setup:upgrade

Step 3: Finally, Clear the cache for sure every working

php bin/magento cache:flush

That’s it !!


I hope you like this post, Please feel free to comment below, suggestion and problems!

You May Also Like

Editorial Staff

About the Author: Editorial Staff

Editorial Staff at the MageBeginners is a team of Magento experts writing about Magento Tutorials, Themes, Extensions, Guides, Tricks and much more...

Leave a Reply

Your email address will not be published. Required fields are marked *