How to Create Customer Attribute in Magento 2
In Magento 2 Customer Attributes are used for adding extra attribute fields to collect valuable customer information on registration or account page.
MageAnts have developed via module Magento 2 Customer Attributes so you don't need any programming with our programming.
Magento 2 create/add customer attribute
Step 1
: Firstly, we will create the InstallData.php file at below path :Mageants > Blog > Setup > InstallData.php
<?php namespace Mageants\Blog\Setup; use Magento\Eav\Model\Config; 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; 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\Catalog\Model\Customer::ENTITY, 'my_custom_attribute', [  'label' => 'My Custom Atrribute',  'system' => 0,  'position' => 700,  'sort_order' => 700,  'visible' => true,  'note' => '',  'type' => 'varchar',  'input' => 'text', ]); $this->getEavConfig()->getAttribute('customer', 'my_custom_attribute')->setData('is_user_defined', 1)->setData('is_required', 0)->setData('default_value', '')->setData('used_in_forms', ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout'])->save();  }  public function getEavConfig() { return $this->eavConfig;  } } |
Step 2
: After Creating InstallData.php You have to run php magento setup:upgrade and php bin/magento setup:static-content:deploy Then check the result at admin side customer details page as below :
Magento 2 set customer custom attribute value
To set Custom Customer Attribute value you have to use below block of code in your controller file :
Here, we have used Magento\Customer\Model\Customer instance and created it’s object($customer) and used Magento\Customer\Model\ResourceModel\CustomerFactory instance and created it’s object($customerFactory).
<?php namespace Mageants\Blog\Controller\Index; class Index extends \Magento\Framework\App\Action\Action {  public function __construct( \Magento\Customer\Model\Customer $customer, \Magento\Customer\Model\ResourceModel\CustomerFactory $customerFactory, \Magento\Framework\App\Action\Context $context  ) { $this->customer = $customer; $this->customerFactory = $customerFactory; return parent::__construct($context);  }  public function execute()  { $customerId = "1"; // set any cusromer id $customer = $this->customer->load($customerId); $data = "new customer value"; // set any value $customerData = $customer->getDataModel(); $customerData->setCustomAttribute('my_custom_attribute',$data); $customer->updateData($customerData); $customerResource = $this->customerFactory->create(); $customerResource->saveAttribute($customer, 'my_custom_attribute');  } } |
Magento 2 get customer custom attribute value
To set Custom Customer Attribute value you have to use below block of code in your controller file :
Here, we have used Magento\Customer\Api\CustomerRepositoryInterface instance and created it’s object($customerRepositoryInterface).
<?php namespace Mageants\Blog\Controller\Index; class Index extends \Magento\Framework\App\Action\Action {  public function __construct( \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface, \Magento\Framework\App\Action\Context $context  ) { $this->customerRepositoryInterface = $customerRepositoryInterface; return parent::__construct($context);  }  public function execute()  { $customer =$this->customerRepositoryInterface->getById(1); // Pass any Customer Id whose custom customer atribute value you want to fetch print_r($customer->getCustomAttribute('my_custom_attribute')->getValue());  } } |
Conclusion:
We hope above blog helps you to clearly understand How to Create Customer Attribute in Magento 2 Programmatically.
In case of any kind of problem with the above code implementation, you can contact us or let us know in comment section.