Solving Magento 2 Custom Address Attribute Loading Issues
Hello developers! Today, let’s tackle an intriguing issue that has been puzzling a fellow developer. They’ve created a Magento module for versions 2.2.4 and 2.2.3 to add a custom attribute in the Shipping Address and Customer Address. The attribute works seamlessly when creating a new address during checkoutâit displays, saves correctly in the order and quote addresses.
However, there’s a catch: when loading an existing address from the address book, their custom attribute refuses to cooperate! It remains stubbornly invisible on the checkout page, despite being required for shipping. Even more perplexing is that editing this attribute in the Magento Admin doesn’t save the changes made manually or via database updates.
Debugging the Issue
Upon closer inspection of their code snippets, a few key areas catch our attention:
-
di.xml: The
sortOrderattribute seems misplaced underMagento\Checkout\Block\Checkout\LayoutProcessor. It should be inside the<plugin name="add_custom_field_checkout_form" type="DD\Checkoutaddress\Model\Plugin\Checkout\LayoutProcessor" />element. -
CustomerSetup.php: The function
installCustomerAddressAttributes()seems to be correctly adding the attribute, but there’s no reference to marking it as visible in the customer address form. -
require-config.js and set-shipping-infomation-mixin.js: These files seem unnecessary for the issue at hand since they deal with shipping information mixins and don’t directly impact the loading of saved addresses.
Resolving the Mystery
The core issue lies in how the custom attribute is being saved to the customer address. While the installCustomerAddressAttributes() function correctly adds the attribute, it misses an essential step: setting the used_in_forms configuration for the ‘customer_address_edit’ form.
Here’s how we can rectify this:
- Update CustomerSetup.php as follows:
public function installCustomerAddressAttributes($customerSetup) {
// ... existing code ...
$attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'mobilenumber');
$attribute->setData('used_in_forms', ['adminhtml_customer_address', 'customer_register_address', 'customer_address_edit']);
$attribute->save();
}
- Reinstall the module to apply these changes.
Verification and Conclusion
After updating the CustomerSetup.php file and reinstalling the module, you should see the custom attribute appearing in the address book on the checkout page. The attribute will now be visible and editable when loading an existing address from the address book.
Remember, it’s crucial to carefully inspect the used_in_forms configuration for each Magento 2 module you develop or customize. Ensuring that this configuration includes the correct forms is key to a smooth user experience across all aspects of your e-commerce platform.
If you’re looking for more in-depth guides on Magento development and customization, don’t forget to check out Laravel Company. We offer a wide range of resources tailored specifically for developers like yourself.