
Providing high-quality customer service is one of the pillars of a successful business. Magento 2 offers you extensive features for customer accounts management out of the box. Thus, you can configure customer name, address, login, password options, and more.
However, the default options are not always sufficient if you need to gather some specific info. That said, you'll need to create custom customer attributes.
Unfortunately, Magento 2 doesn't offer the simple admin panel setup of this functionality. So, you need to add customer attributes programmatically.
Don't know how? No worries, you're about to find out!
Post Contents [hide]
1. Create a Magento 2 module
Magento handles customer attributes using the EAV model structure and the Data Patches to alter the info. Thus, this is what we'll apply today.
So, before adding customer attributes, you need to create a basic Magento module. If you already have a suitable one, you can just use it.
The method below requires you to have a module to run the code. So make sure you have it.
2. Create the InstallData.php file
The next step is to create the InstallData.php file in your-module/Setup and add the following code:
<?php
namespace vendor\your-module\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,
'custom_attribute',
[
'type' => 'varchar',
'label' => 'Custom Attribute',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
);
$customAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'custom_attribute');
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$customAttribute->setData(
'used_in_forms',
['adminhtml_customer']
);
$customAttribute->save();
}
}
In this code, you create the EAV setup model and define the install() method and eavConfig object. Depending on the type of attribute you want to add, you'll have to adjust the code and set the corresponding attribute name, data type, input type, etc.
3. Add customer attribute
Once you add all the necessary content to the InstallData.php file, there are just a few finishing touches. Run the following commands to install the module:
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
Note: if you don't want your website to be down during deployment, try these zero downtime deployment commands for Magento 2.
Now, you can finally go to the Customers section in the admin panel and check your newly created attribute for each customer.
These are all the steps to add customer attributes in Magento 2 programmatically. This task is not that complicated when you apply the correct method. Thus, make sure you follow all the steps carefully and add accurate data.
Except for the customer attributes, you can also add custom fields to Magento 2 checkout. This way, you'll have all the required details for seamless shipment and delivery.