Hi Coders,
In this blog post, we’ll discuss what’re customer attributes and how we add them programmatically in Magento 2.
In Magento, fields which are related in registration, login, and filling customer address are basically customer attributes. They’re additional divided into customer attributes and customer address attributes. Now if attributes used in customer registration/login are customer attributes and attribute used in address information for customers are customer address attributes. Some examples of customer attributes are below :
a. Customer Attributes
– First Name, Last Name, DOB, etc.
b. Customer Address Attributes
– Zip, City, Street, Country, Region/State, etc.
Let see how we can create customer attributes programmatically in Magento 2 step by step:-
Step 1: Create the folder of Custom Attribute module
For our module, we will use the Vendor name ‘Webiators‘ and Module name ‘CustomerAttribute‘.
Once we will create folders the path to our module will be app/code/Webiators/CustomerAttribute/
Step 2: Create etc/module.xml file.
File: Webiators/CustomerAttribute/etc/module.xml
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Webiators_CustomerAttribute" setup_version="1.0.0"> </module> </config> |
Step 3: Create registration.php file.
File: Webiators/CustomerAttribute/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Webiators_CustomerAttribute', __DIR__ ); |
Step 4: Create setup file InstallData.php
File: Webiators/CustomerAttribute/Setup/InstallData.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<?php namespace Webiators\CustomerAttribute\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; } /** * {@inheritdoc} */ 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, 'magento_username', [ 'type' => 'varchar', 'label' => 'Magento Username', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'sort_order' => 1000, 'position' => 1000, 'system' => 0, ]); $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'magento_username') ->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => ['adminhtml_customer'], ]); $attribute->save(); } } |
Now, let run command line to install the module: php magento setup:upgrade
and php bin/magento setup:static-content:deploy
Then check the result. It will show like this:
That’s all for adding customer attribute programmatically.
Please use the Comments section below if you have any doubts related to this topic. I’ll be happy to help.
Happy Coding!