
Magento 2.x – Add custom attribute to customer model and show on admin panel
You will need to perform some basic steps to create and set a new attribute on the customer model.
- Create a new module, with a setup script, with the following files:
- app/code/{CompanyName}/{ExtensionName}/registration.php
1234567<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'CompanyName_ExtensionName',__DIR__); - app/code/{CompanyName}/{ExtensionName}/etc/module.xml
123456789<?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="{CompanyName}_{ExtensionName}" setup_version="1.0.0"><sequence><module name="Magento_Customer"/></sequence></module></config> - app/code/{CompanyName}/{ExtensionName}/Setup/InstallData.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293<?phpnamespace CompanyName\ExtensionName\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;}/*** {@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, 'attr_name', ['type' => 'varchar','label' => 'Your attribute label','input' => 'text','required' => false,'visible' => true,'user_defined' => true,'sort_order' => 1000,'position' => 1000,'system' => 0,]);/*used_in_form should be set wherever you want to see your attribute, they can also be:adminhtml_checkoutadminhtml_customeradminhtml_customer_addresscheckout_registercustomer_account_createcustomer_account_editcustomer_address_editcustomer_register_address*/$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'attr_name')->addData(['attribute_set_id' => $attributeSetId,'attribute_group_id' => $attributeGroupId,'used_in_forms' => ['adminhtml_customer'],]);$attribute->save();}} - app/code/{CompanyName}/{ExtensionName}/view/adminhtml/ui_component/customer_form.xml
12345678910<?xml version="1.0" encoding="UTF-8"?><form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"><fieldset name="customer"><field name="attr_name" formElement="input"><settings><visible>true</visible></settings></field></fieldset></form>
Note: In contrast to Magento 1, in Magento 2 you now need to add some XML to make custom attributes appear in the edit customer section of the admin.
- app/code/{CompanyName}/{ExtensionName}/registration.php
Afterwards, and before testing, remember to enable the module, clean cache, and do a setup:upgrade.