To add customer attribute programatically, create InstallData.php and and define the method install() as follows
File : app/code/PHPCodez/First/Setup/InstallData.php
<?php namespace PHPCodez\First\Setup; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Eav\Model\Config; use Magento\Customer\Model\Customer; class InstallData implements InstallDataInterface { private $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig) { $this->eavSetupFactory = $eavSetupFactory; $this->eavConfig = $eavConfig; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'phpcodez_attribute', [ 'type' => 'varchar', 'label' => 'PHPCodez Rating', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'position' => 999, 'system' => 0, ] ); $sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'phpcodez_attribute'); // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'] $sampleAttribute->setData( 'used_in_forms', ['adminhtml_customer'] ); $sampleAttribute->save(); } }
Now purche cache and deploy static content by running the following command.
php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f
One thought on “Magento 2 Add Customer Attribute Programmatically”