Sometimes you may face the issue like Value not update/Save for the customer attribute when you have created a customer attribute using Setup Patch Data in Magento 2.
When you check the log files, you don’t see any error log for this issue, its time consuming to trace the issue instead of customer attribute record generated in eav_attribute, customer_eav_attribute and customer_form_attribute
table.
Customer Attribute is not saved any value to the database, because the attribute data not stored in 'eav_entity_attribute'
table while creating customer attributes using the Patch Data file.
To avoid these issues, you must specify additional parameters while creating the customer attribute.
- attribute_set_id
- attribute_group_id
I will just be given an example of the `b2b_user` customer attribute with a boolean type.
Create a Setup Patch Data file for the User Defined Customer attribute,
<?php declare(strict_types=1); /** * Patch to create Customer Attribute * * Creates b2b_user user defined customer attribute */ namespace Jesadiya\B2bAccount\Setup\Patch\Data; use Zend_Validate_Exception; use Magento\Eav\Model\Config; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Eav\Model\Entity\Attribute\Source\Boolean; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; /** * Patch Class to create b2b_user attribute */ class B2BUserAttribute implements DataPatchInterface { /** * @var Config */ private $eavConfig; /** * @var EavSetupFactory */ private $eavSetupFactory; /** * @var AttributeSetFactory */ private $attributeSetFactory; /** * AddressAttribute constructor. * * @param Config $eavConfig * @param EavSetupFactory $eavSetupFactory * @param AttributeSetFactory $attributeSetFactory */ public function __construct( Config $eavConfig, EavSetupFactory $eavSetupFactory, AttributeSetFactory $attributeSetFactory ) { $this->eavConfig = $eavConfig; $this->eavSetupFactory = $eavSetupFactory; $this->attributeSetFactory = $attributeSetFactory; } /** * {@inheritdoc} */ public static function getDependencies(): array { return []; } /** * Create strategic account customer attribute * @return void * @throws LocalizedException * @throws Zend_Validate_Exception */ public function apply(): void { $eavSetup = $this->eavSetupFactory->create(); $customerEntity = $this->eavConfig->getEntityType('customer'); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); $eavSetup->addAttribute('customer', 'b2b_user', [ 'type' => 'int', 'input' => 'boolean', 'label' => 'B2B User', 'visible' => false, 'source' => Boolean::class, 'required' => false, 'user_defined' => true, 'system' => false, 'global' => true, 'default' => 0, 'visible_on_front' => false, 'sort_order' => 50, 'position' => 50 ]); $customAttribute = $this->eavConfig->getAttribute('customer', 'b2b_user'); $customAttribute->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => ['adminhtml_customer', 'customer_account_edit'] ]); $customAttribute->save(); } /** * {@inheritdoc} */ public function getAliases(): array { return []; } }
You have to specify two additional parameters in the above script class,
attribute_set_id is used to Get the default attribute set identifier for the entity type customer.
attribute_group_id used to Return default Group Id for current Attribute Set.
This snippet of code is the important in the given Patch file,
$customAttribute->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => ['adminhtml_customer', 'customer_account_edit'] ]);
You must pass the above two-parameter within used_in_forms
data array to fixed the issue of the customer attribute value saving.