We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
- Home
- Blog
- eCommerce Talk
- How To Create Customer Attribute Programmatically In Magento 2
How To Create Customer Attribute Programmatically In Magento 2
Attributes is a property that can be used for several Magento 2 stores functions.
Magento 2 allows you to create multi-purpose attributes for different entities. In this article, we’ll discuss how to create custom attributes that help to know your customer in a good way.
Magento 2 provides functionalities to create different customer attributes like mobile number, interest/hobbies, and many more using the code below.
Before we begin, make sure to have your own Magento 2 extension. If you have just started out, then check out this tutorial on how to create a simple module in Magento 2.
Steps to add customer attribute programmatically
STEP 1: Create And Register The Module
We need to create a registration.php file to register our module.
FILE PATH: app/code/MageSpark/CustomerAttribute/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'MageSpark_CustomerAttribute',
__DIR__
);
Register the module.xml file and define the setup to setup_module table.
FILE PATH: app/code/MageSpark/CustomerAttribute/etc/module.xml
<?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="MageSpark_CustomerAttribute" setup_version="1.0.0">
<sequence>
<module name="Magento_Customer"/>
</sequence>
</module>
</config>
STEP 2: Create and define Install.php file
The next thing you need is an install.php file for adding entries in the attributes database as given below.
FILE PATH: app/code/MageSpark/CustomerAttribute/Setup/InstallData.php
<?php
declare(strict_types=1);
namespace MageSpark\CustomerAttribute\Setup;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* Class InstallData
*
* @package MageSpark\CustomerAttribute\Setup
*/
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* Constructor
*
* @param CustomerSetupFactory $customerSetupFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* {@inheritdoc}
*/
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(Customer::ENTITY, 'custom', [
'type' => 'varchar',
'label' => 'Custom Attribute',
'input' => 'text',
'source' => '',
'position' => 700,
'visible' => true,
'required' => false,
'system' => false
]);
// Mention here in which form you want to show created customer attribute
$attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'custom')
->addData(['used_in_forms' => [
'adminhtml_customer',
'adminhtml_checkout',
'customer_account_create',
'customer_account_edit'
]
]);
$attribute->save();
}
}
STEP 3: Installing the module
Now, run the commands below to install our module.
php bin/magento module:enable MageSpark_CustomerAttribute
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
It will show you customer attribute in the admin panel like this:
Conclusion
Hope you find the blog to be helpful and quite interesting.
If you find it helpful, then Help other fellow developer by sharing this code with social media or email.
Get any queries, feel free to ask in the comment section. We will be glad to help you out.
Did you get any new results in your e-commerce site with the use of customer attributes?