Skip to main content

How to Create Customer Custom Attribute in Magento 2

 Here we will learn, How to create a Customer Custom attribute in Magento 2.

Note: We also have a separate module (Magento 2 Custom Registration Fields) that allows the admin to create custom fields for customers.

Henceforth, using this module you can extend your customer sign-up form. For more information, you can check it on our store.

Let’s start the process.

We can achieve this by using the following methods:

1st Method: Using InstallData file
Create the file Webkul\CustomAttribute\Setup\InstallData.php

<?php namespace Webkul\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; 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_cust_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_cust_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_account_edit', 'customer_address_edit', 'customer_register_address', 'customer_account_create'] ]); $attribute->save(); } }

InstallData conforms to InstallDataInterface , which requires the implementation of the install method that accepts two parameters of type ModuleDataSetupInterface and ModuleContextInterface .
Using the addAttribute method on the instance of Magento\Customer\Setup\CustomerSetupFactory, we are instructing Magento to add a number of attributes.
Note: The module’s module.xml file must have setup_version to create customer custom attribute using InstallData. To know steps to create module in Magento 2, you can check our another blog here.

2nd Method: Using Data Patch
In this method, we need to create a data patch file as CreateAttributes.php in our custom module Setup folder. Note: You can also use the different file name and class name.
complete path: app/code/Webkul/CustomAttribute/Setup/Patch/Data/CreateAttributes.php

To know more about data and schema patches, click here.

<?php namespace Webkul\CustomAttribute\Setup\Patch\Data; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Customer\Model\Customer; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; class CreateAttributes implements DataPatchInterface { /** * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * @var CustomerSetupFactory */ protected $customerSetupFactory; /** * @var AttributeSetFactory */ private $attributeSetFactory; /** * @param ModuleDataSetupInterface $moduleDataSetup * @param CustomerSetupFactory $customerSetupFactory * @param AttributeSetFactory $attributeSetFactory */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory ) { $this->moduleDataSetup = $moduleDataSetup; $this->customerSetupFactory = $customerSetupFactory; $this->attributeSetFactory = $attributeSetFactory; } /** * Add eav attributes */ public function apply() { /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer'); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); /** @var $attributeSet AttributeSet */ $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); $customerSetup->addAttribute(Customer::ENTITY, 'custom_customer_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_customer_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_account_edit', 'customer_address_edit', 'customer_register_address', 'customer_account_create'] ]); $attribute->save(); } /** * Get dependencies */ public static function getDependencies() { return []; } /** * Get Aliases */ public function getAliases() { return []; } }

After adding these files, run the following command in your magento2 root directory through a terminal.

php bin/magento setup: upgrade

That is all for the How to Create Customer Custom Attribute in Magento 2. Hope it will help you. Thank you.

Originally published at https://webkul.com on February 19, 2016.

Comments

Popular posts from this blog

Top Considerations for Hiring a Laravel Mobile App Development Company for Business

In today's digital age, mobile apps have become an essential component for businesses to thrive and stay competitive. Whether you have a small startup or a large enterprise, having a mobile app can significantly boost your brand presence and customer engagement. Consequently, choosing the right mobile app development company is a crucial decision that can impact the success of your business. When it comes to developing a mobile app using Laravel, there are several important considerations to keep in mind. Laravel Development Company When hiring a Laravel development company, it is essential to find a team of skilled professionals who specialize in this framework. Laravel is a widely popular PHP framework known for its elegant syntax and extensive set of features. It offers scalability, security, and flexibility, making it an excellent choice for building robust and reliable mobile applications. Here are the top considerations you should keep in mind when selecting a Laravel mobile ...

How to Create Mobile Application with Laravel?

Laravel is a free and open-source PHP framework for "web artisans". Laravel is well-known for being a logical and strong framework that allows you to fully utilize the power of PHP once you understand what you're doing. The laravel native mobile app is enriched with numerous features which can help the user get the best. It powers numerous websites, online applications, and corporate systems throughout the internet. Using the framework, you may build anything from tiny, lightweight applications to huge complicated systems. Perhaps your Laravel eCommerce web app has received thousands of visitors, signups, and satisfied users. Advantages Of Laravel Laravel is ideally suited for all sorts of applications, especially those with specialized requirements such as portal mobile apps, social networking apps, and others. Source Code Integrity And Flexibility The PHP source code is customizable and versatile. Therefore, creating cross-platform applications frequently uses PHP. It...

How can Laravel Development Services Help Your Business?

You are using the PHP framework. Well, we’ll make things more comfortable for you, which will help best improve your company strategy. If you want to gain your business shifting in a good direction, the Laravel development service is properly what you must think about! With the Laravel development service, you’ll be capable of custom web applications for your exact business requirements. This is excellent for a small industry or even a larger business. You should decide on a Laravel development company like webkul for the following reasons: It is simple It is quick It is effective As a development, you will help from an inbuilt validation library when utilizing the Laravel development service . It will save misconduct at bay and improve the probability of more results. Whether you require website development or business applications for your company, you still look for a platform that can deliver you with the most components. And the Laravel framework consistently exceeds expecta...