I added a new field in the Magento 2 backend for the customer which is visible. I created this using mage2gen but whatever i try, the new field is not being saved when i update it in the backend.
Since the field is visible in the backend and both the view and api is working as expected i have no clue why it is not working. Below the actual patch i do to add the field and the xml config to make it visible as well.
I tried to add it to the attributeset but as far as i can see the field is registered properly and is also there in customer_eav_attribute
<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fieldset name="general">
<field formElement="text" name="external_reference" sortOrder="20">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">external_reference</item>
</item>
</argument>
<settings>
<dataType>text</dataType>
<label translate="true">External reference</label>
<dataScope>external_reference</dataScope>
<validation>
<rule name="required-entry" xsi:type="boolean">false</rule>
</validation>
</settings>
</field>
</fieldset>
</form>
<?php
declare(strict_types=1);
namespace MeDirectLoginSetupPatchData;
use MagentoCustomerApiCustomerMetadataInterface;
use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoFrameworkSetupPatchPatchRevertableInterface;
class AddExternalReferenceCustomerAttribute implements DataPatchInterface, PatchRevertableInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Constructor
*
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
*/
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->addAttribute(
MagentoCustomerModelCustomer::ENTITY,
'external_reference',
[
'type' => 'varchar',
'label' => 'External reference',
'input' => 'text',
'source' => '',
'frontend' => '',
'required' => false,
'backend' => '',
'default' => null,
'user_defined' => true,
'unique' => true,
'group' => 'General',
'system' => 0,
'order' => 1000,
'note' => 'Reference to external managed user',
'is_used_in_grid' => false,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
]
);
$eavSetup->addAttributeToSet(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
null,
'external_reference'
);
$this->moduleDataSetup->getConnection()->endSetup();
}
public function revert()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->removeAttribute(MagentoCustomerModelCustomer::ENTITY, 'external_reference');
$this->moduleDataSetup->getConnection()->endSetup();
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [
];
}
}
Hope someone can point me to a solution
Best,
Pim
2
Answers
I have checked that your code little bit different from the actual mage2gen code. Like you did not add data for "used_in_forms". You can try the below code to create a custom customer attribute (If you want to go with mage2gen)
https://mage2gen.com/snippets/customerattribute/
Or else you can try with the install method. Check below Mageplaza blog url:
https://www.mageplaza.com/magento-2-module-development/magento-2-add-customer-attribute-programmatically.html
Try to use the following in Setup/Patch/Data path