This article shows how you can programmatically add or create a new customer attribute in Magento 2. It also shows how you can update and delete/remove the customer attribute programmatically in Magento 2.
I will also show how you can add the product attributes from both Install Script and Upgrade Script:
– Add customer attribute from Install Script (InstallData.php)
– Add customer attribute from Upgrade Script (UpgradeData.php)
The module name for this article is: Chapagain_CustomerAttribute
Add/Create Customer Attribute Using the Install Script
Here, we created two customer attributes:
– My Customer Type (attribute code:
my_customer_type
)
– My Customer File/Image (attribute code:my_customer_image
)
File: app/code/Chapagain/CustomerAttribute/Setup/InstallData.php
<?php
namespace Chapagain\CustomerAttribute\Setup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var \Magento\Eav\Setup\EavSetupFactory
*/
private $eavSetupFactory;
/**
* Customer setup factory
*
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* Constructor
*
* @param EavSetupFactory $eavSetupFactory
* @param CustomerSetupFactory $customerSetupFactory
*/
public function __construct(
EavSetupFactory $eavSetupFactory,
CustomerSetupFactory $customerSetupFactory
)
{
$this->eavSetupFactory = $eavSetupFactory;
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* {@inheritdoc}
*/
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
/**
* Create a select box attribute
*/
$attributeCode = 'my_customer_type';
$customerSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
$attributeCode,
[
'type' => 'int',
'label' => 'My Customer Type',
'input' => 'select',
'source' => 'Chapagain\CustomerAttribute\Model\Config\Source\MyCustomerType',
'required' => false,
'visible' => true,
'position' => 300,
'system' => false,
'backend' => ''
]
);
// show the attribute in the following forms
$attribute = $customerSetup
->getEavConfig()
->getAttribute(
\Magento\Customer\Model\Customer::ENTITY,
$attributeCode
)
->addData(
['used_in_forms' => [
'adminhtml_customer',
'adminhtml_checkout',
'customer_account_create',
'customer_account_edit'
]
]);
$attribute->save();
/**
* Create a file type attribute
* For File or Image Upload
*/
$attributeCode = 'my_customer_image';
$customerSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
$attributeCode,
[
'type' => 'text',
'label' => 'My Customer File/Image',
'input' => 'file',
'source' => '',
'required' => false,
'visible' => true,
'position' => 200,
'system' => false,
'backend' => ''
]
);
// show the attribute in the following forms
$attribute = $customerSetup
->getEavConfig()
->getAttribute(
\Magento\Customer\Model\Customer::ENTITY,
$attributeCode
)
->addData(
['used_in_forms' => [
'adminhtml_customer',
'adminhtml_checkout',
'customer_account_create',
'customer_account_edit'
]
]);
$attribute->save();
$setup->endSetup();
}
}
Here, we created two customer attributes:
– My Customer Type (my_customer_type)
– My Customer File/Image (my_customer_image)
My Customer Type is a select-box attribute. So, for it, we have defined a custom source file: Chapagain\CustomerAttribute\Model\Config\Source\MyCustomerType
Hence, we need to create the source file as well.
File: app/code/Chapagain/CustomerAttribute/Model/Config/Source/MyCustomerType.php
<?php
namespace Chapagain\CustomerAttribute\Model\Config\Source;
class MyCustomerType extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/**
* Get all options
*
* @return array
*/
public function getAllOptions()
{
if ($this->_options === null) {
$this->_options = [
['value' => '', 'label' => __('Please Select')],
['value' => '1', 'label' => __('My Option 1')],
['value' => '2', 'label' => __('My Option 2')],
['value' => '3', 'label' => __('My Option 3')],
['value' => '4', 'label' => __('My Option 4')]
];
}
return $this->_options;
}
/**
* Get text of the option value
*
* @param string|integer $value
* @return string|bool
*/
public function getOptionValue($value)
{
foreach ($this->getAllOptions() as $option) {
if ($option['value'] == $value) {
return $option['label'];
}
}
return false;
}
}
Add/Create/Update/Remove Customer Attribute Using the Upgrade Script
Here:
– first of all, we create a new attribute named:
My Customer Date
with the attribute code:my_customer_date
– then, we set the attribute to different forms
– then, there’s code to update thefrontend_model
of the attribute
– we also update thefrontend_label
of the attribute
– finally, we see the code to remove any attribute by its attribute code
–version_compare()
function is used to run the code on each version upgrade
File: app/code/Chapagain/ProductAttribute/Setup/UpgradeData.php
<?php
namespace Chapagain\CustomerAttribute\Setup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* @codeCoverageIgnore
*/
class UpgradeData implements UpgradeDataInterface
{
/**
* Customer setup factory
*
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* EAV setup factory
*
* @var \Magento\Eav\Setup\EavSetupFactory
*/
private $eavSetupFactory;
/**
* Constructor
*
* @param EavSetupFactory $eavSetupFactory
* @param CustomerSetupFactory $customerSetupFactory
*/
public function __construct(
EavSetupFactory $eavSetupFactory,
CustomerSetupFactory $customerSetupFactory
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* {@inheritdoc}
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
/**
* run this code if the module version stored in database is less than 1.0.1
* i.e. the code is run while upgrading the module from version 1.0.0 to 1.0.1
*
* you can write the version_compare function in the following way as well:
* if(version_compare($context->getVersion(), '1.0.1', '<')) {
*
* the syntax is only different
* output is the same
*/
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$attributeCode = 'my_customer_date';
$customerSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
$attributeCode,
[
'type' => 'static', // backend type
'label' => 'My Customer Date',
'input' => 'date', // frontend input
'source' => '', // source model
'backend' => \Magento\Eav\Model\Entity\Attribute\Backend\Datetime::class,
'required' => false,
'visible' => true,
'sort_order' => 200,
'position' => 300,
'system' => false
]
);
// show the attribute in the following forms
$attribute = $customerSetup
->getEavConfig()
->getAttribute(
\Magento\Customer\Model\Customer::ENTITY,
$attributeCode
)
->addData(
['used_in_forms' => [
'adminhtml_customer',
'adminhtml_checkout',
'customer_account_create',
'customer_account_edit'
]
]);
$attribute->save();
}
if(version_compare($context->getVersion(), '1.0.2', '<')) {
$attributeCode = 'my_customer_date';
// add/update frontend_model to the attribute
$customerSetup->updateAttribute(
\Magento\Customer\Model\Customer::ENTITY, // customer entity code
$attributeCode,
'frontend_model',
\Magento\Eav\Model\Entity\Attribute\Frontend\Datetime::class
);
// update the label of the attribute
$customerSetup->updateAttribute(
\Magento\Customer\Model\Customer::ENTITY, // customer entity code
$attributeCode,
'frontend_label',
'My Custom Date Modified'
);
}
if(version_compare($context->getVersion(), '1.0.3', '<')) {
$attributeCode = 'your_customer_attribute_code';
// remove customer attribute
$customerSetup->removeAttribute(
\Magento\Customer\Model\Customer::ENTITY,
$attributeCode // attribute code to remove
);
}
$setup->endSetup();
}
}
After updating upgrade setup code, you also need to make sure that you have upgraded the module version number in your module’s etc/module.xml
file.
After that, you need:
- open terminal
- go to your Magento’s root directory
- and run the following command:
php bin/magento setup:upgrade
Hope this helps. Thanks.