Solving the Magento 2 Customer Attribute Saving Issue
Dear developer,
I understand your frustration when custom customer attributes fail to save in Magento 2. Let’s troubleshoot this issue step-by-step.
The Problem: Custom Attribute Not Saving
You’ve created a new customer attribute using mage2gen and registered it properly. However, even though the field is visible in the backend and works with view and API, it doesn’t get saved when updated. The attribute seems to be registered correctly in customer_eav_attribute.
The Code Analysis
Let’s break down your code:
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fieldset name="general">
<field formElement="text" name="external_reference" sortOrder="20">
<!-- Field configuration -->
</field>
</fieldset>
</form>
This XML defines a new field named external_reference in the customer’s general information section. The field is of type text and has no required validation.
The PHP patch code successfully registers this attribute:
public function apply()
{
// Start setup
$eavSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'external_reference',
[
'type' => 'varchar',
'label' => 'External reference',
'input' => 'text',
...
]
);
$eavSetup->addAttributeToSet(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
null,
'external_reference'
);
// End setup
}
The Solution: Fixing the Attribute Configuration
Upon reviewing your code, I noticed a potential issue with your attribute configuration.
Step 1: Check the Backend Model
Ensure you have created an appropriate backend model for this attribute. Magento needs a backend model to handle the data properly. You can use the Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend as the base class.
namespace Me\DirectLogin\Backend;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
class ExternalReference extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
/**
* @var RequestInterface
*/
private $request;
public function __construct(
ScopeConfigInterface $config,
RequestInterface $request,
array $data = []
) {
parent::__construct($config, $data);
$this->request = $request;
}
// Implement the _afterSave method here
}
Step 2: Update the Attribute Configuration
Update your attribute configuration in the apply method:
$eavSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'external_reference',
[
'type' => 'varchar',
'label' => 'External reference',
'input' => 'text',
'backend_model' => 'Me\DirectLogin\Backend\ExternalReference', // Add this line
...
]
);
Step 3: Flush the Cache
After making these changes, don’t forget to flush your Magento cache:
php bin/magento cache:flush
php bin/magento setup:upgrade --keep-generated
Conclusion
Ensuring a proper backend model for your custom attribute is crucial in Magento. By implementing this step, you should now be able to save the external_reference field correctly.
Remember, when working with Magento’s EAV system, always check if you need to handle data types, validation rules, or special behavior using a dedicated backend model.
If you still encounter issues after following these steps, I recommend reaching out to a dedicated Magento developer for further assistance.
Happy coding!
Best, [Your Name] Laravel Company