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

Why Adobe Commerce Cloud is best suited for Headless eCommerce

  Headless architecture is trending now as it is considered the future of eCommerce. In this article, we will cover some points to tell Why Adobe Commerce Cloud is best suited for headless eCommerce?  Magento 2 is the most popular CMS for eCommerce Development. Also, it provides many features and tools which make the headless implementation much easier from developing from scratch. What is Headless eCommerce? Headless  is an approach where you separate the frontend and backend of the eCommerce Website. It means that your customer experience platform ( UI & UX) is independent of your Content Management system.  Today, when eCommerce is moving towards the Omnichannel approach the role of headless eCommerce becomes crucial. With its use, the shop owner can provide a more flexible, speedy, and personalized experience to their customers. Why Adobe Commerce Cloud is best for Headless eCommerce? Adobe Commerce Cloud provides many tools that make the head...

Point Of Sale System For Magento

  It refers to a system where the merchant can create the order from his physical store using the POS and the order will be generated in the Magento. The Basic feature of the POS system for Magento is to create the order in front of the customer so that customer can purchase the goods from his/her physical store as well as from his/her online eCommerce store . With the help of this module, merchants can manage their inventory and customers. And the most important thing is that they can easily manage their day-to-day transactions without any hassle. This module is a powerful tool to manage the sales and revenue. Admin can also set up the physical store along with the online Magento store. Please Note- ·          The Magento POS connects only to the Printer, and barcode reader, whereas it doesn't connect directly with the cash drawer and card swapping machine. ·          POS requires a browser to ...

Aureus ERP: Your Open Source Business Solution

  Aureus ERP: Your Open Source Business Solution Aureus ERP is an open source ERP platform built on Laravel that helps make your business operations easier and better. Whether you need to handle projects, employees, inventory, or purchasing, AureusERP provides a flexible, safe, and easy to use solution that fits the specific needs of your business. AureusERP gives you a flexible, safe, and user friendly platform designed for your business’s specific needs. Why Choose Aureus ERP? Aureus ERP is not just your average ERP solution. It’s built on the Laravel framework, which makes it strong and safe, perfect for dealing with the complicated needs of today’s businesses. Now, let’s explore the features that make AureusERP unique. 1. Free and Open Source: Aureus ERP is open source, which means you can use it for free and customize it however you want. 2. Built on Laravel: Aureus ERP uses Laravel, a strong PHP framework that is famous for being secure and fast. This ensures tha...