Last active
December 28, 2021 16:13
-
-
Save 0-Sony/35f9238601ce14b40458b9c1a29a2459 to your computer and use it in GitHub Desktop.
Magento 2: Create custom Customer Attribute (Using Patch Data for Magento version >= 2.3.x )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
* @author Phuong LE <[email protected]> | |
* @copyright Copyright (c) 2020 Men In Code Ltd (https://www.menincode.com) | |
*/ | |
namespace MyNamespace\MyModule\Setup\Patch\Data; | |
use Magento\Customer\Model\Customer; | |
use Magento\Customer\Setup\CustomerSetupFactory; | |
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; | |
use Magento\Framework\Setup\Patch\DataPatchInterface; | |
class AddCustomCustomerAttribute implements DataPatchInterface | |
{ | |
/** | |
* @var CustomerSetupFactory | |
*/ | |
private $customerSetupFactory; | |
/** | |
* @var AttributeSetFactory | |
*/ | |
private $attributeSetFactory; | |
/** | |
* AddCustomCustomerAttribute constructor. | |
* @param CustomerSetupFactory $customerSetupFactory | |
* @param AttributeSetFactory $attributeSetFactory | |
*/ | |
public function __construct( | |
CustomerSetupFactory $customerSetupFactory, | |
AttributeSetFactory $attributeSetFactory | |
) { | |
$this->customerSetupFactory = $customerSetupFactory; | |
$this->attributeSetFactory = $attributeSetFactory; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public static function getDependencies() | |
{ | |
return []; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function getAliases() | |
{ | |
return []; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function apply() | |
{ | |
/** @var \Magento\Customer\Setup\CustomerSetup $customerSetup */ | |
$customerSetup = $this->customerSetupFactory->create(); | |
$customerSetup->removeAttribute(Customer::ENTITY, 'my_custom_attribute'); | |
$customerSetup->addAttribute( | |
Customer::ENTITY, | |
'my_custom_attribute', | |
[ | |
'group' => 'General', | |
'type' => 'text', | |
'label' => 'My Label', | |
'input' => 'text', | |
'position' => 160, // position defined in the admin customer form | |
'user_defined' => true, | |
'required' => false, | |
'system' => false, // Allow to save values , @see Magento\Customer\Model\Metadata\CustomerMetadata::getCustomAttributesMetadata() | |
], | |
/** Boolean example | |
[ | |
'type' => 'int', | |
'label' => 'Boolean Attribute', | |
'input' => 'boolean', | |
'backend' => \Magento\Customer\Model\Attribute\Backend\Data\Boolean::class, | |
'visible' => false, | |
'position' => 160, | |
'user_defined' => true, | |
'required' => false, | |
'system' => false, | |
'default' => 0, | |
] | |
**/ | |
); | |
$attribute = $customerSetup->getEavConfig()->getAttribute( | |
Customer::ENTITY, | |
'my_custom_attribute' | |
); | |
$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY); | |
$attributeSetId = $customerEntity->getDefaultAttributeSetId(); | |
$attributeSet = $this->attributeSetFactory->create(); | |
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); | |
/** @var mixed[] $rules */ | |
$rules = [ | |
'input_validation' => 'alphanumeric', | |
'min_text_length' => 10, | |
'max_text_length' => 15, | |
]; | |
$attribute->setValidationRules($rules); | |
$attribute->addData( | |
[ | |
'attribute_set_id' => $attributeSetId, | |
'attribute_group_id' => $attributeGroupId, | |
'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'], //define the areas where the attribute field should be displayed | |
] | |
); | |
$attribute->save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment