How to create multiple customer attributes in magento 2?
-
27-03-2021 - |
Question
How to create multiple customer attributes in Magento 2 using InstallData or UpgradeData?
<?php
namespace DeviMage\CustomerAttribute\Setup;
use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
class UpgradeData implements UpgradeDataInterface
{
private $customerSetupFactory;
public function __construct(
CustomerSetupFactory $customerSetupFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* {@inheritdoc}
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context){
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
if (version_compare($context->getVersion(), '1.0.6') < 0) {
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'designation', [
'type' => 'varchar',
'label' => 'Designation',
'input' => 'text',
'source' => '',
'required' => false,
'visible' => true,
'position' => 333,
'system' => false,
'backend' => ''
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'designation')
->addData(['used_in_forms' => [
'adminhtml_customer',
'adminhtml_checkout',
'customer_account_create',
'customer_account_edit'
]]);
$attribute->save();
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'landlinenumber', [
'type' => 'varchar',
'label' => 'Landline Number',
'input' => 'text',
'source' => '',
'required' => false,
'visible' => true,
'position' => 333,
'system' => false,
'backend' => ''
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'landlinenumber')
->addData(['used_in_forms' => [
'adminhtml_customer',
'adminhtml_checkout',
'customer_account_create',
'customer_account_edit'
]]);
$attribute->save();
}
}
}
What is Difference between InstallData & UpgradeData?
Solution
Hii @Devidas Please try this code
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$attributesInfo = [
'vat_id' => [
'label' => 'VAT number',
'type' => 'static',
'input' => 'text',
'position' => 140,
'visible' => true,
'required' => false,
],
'vat_is_valid' => [
'label' => 'VAT number validity',
'visible' => false,
'required' => false,
'type' => 'static',
],
'vat_request_id' => [
'label' => 'VAT number validation request ID',
'type' => 'static',
'visible' => false,
'required' => false,
],
'vat_request_date' => [
'label' => 'VAT number validation request date',
'type' => 'static',
'visible' => false,
'required' => false,
],
'vat_request_success' => [
'label' => 'VAT number validation request success',
'visible' => false,
'required' => false,
'type' => 'static',
],
];
foreach ($attributesInfo as $attributeCode => $attributeParams) {
$customerSetup->addAttribute('customer_address', $attributeCode, $attributeParams);
}
$vatIdAttribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'vat_id');
$vatIdAttribute->setData(
'used_in_forms',
['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
);
$vatIdAttribute->save();
OTHER TIPS
- InstallSchema – setup database structure
- UpgradeData – upgraded (add/remove) data from table
namespace YourModule\ModuleName\Setup; use Magento\Eav\Model\Config; use Magento\Customer\Model\Customer; 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; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; class UpgradeData implements UpgradeDataInterface { protected $customerSetupFactory; private $eavConfig; private $eavSetupFactory; private $attributeSetFactory; public function __construct( CustomerSetupFactory $customerSetupFactory, Config $eavConfig, EavSetupFactory $eavSetupFactory, AttributeSetFactory $attributeSetFactory ) { $this->customerSetupFactory = $customerSetupFactory; $this->eavConfig = $eavConfig; $this->eavSetupFactory = $eavSetupFactory; $this->attributeSetFactory = $attributeSetFactory; } public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); // Set your module version if (version_compare($context->getVersion(), '1.0.1', '<')) { $customerSetup->addAttribute( Customer::ENTITY, 'delivered_count', [ 'type' => 'varchar', 'label' => 'delivery count', 'input' => 'text', 'required' => false, 'sort_order' => 100, 'visible' => true, 'system' => false, 'user_defined' => true, 'adminhtml_only' => true ] ); $customerSetup->addAttribute( Customer::ENTITY, 'order_count', [ 'type' => 'varchar', 'label' => 'order count', 'input' => 'text', 'required' => false, 'sort_order' => 110, 'visible' => true, 'system' => false, 'user_defined' => true, 'adminhtml_only' => true ] ); } $setup->endSetup(); } }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange