Magento2 – Save Value for Custom Customer Address Attribute

custom-attributescustomer-addressmagento2

I've added a custom shipping and billing address input field on checkout via layoutProcessor. The field shows up fine but when I submit the order the value of the custom attribute is not saved as the actual field value but rather as a attribute_codeā¸ˇvalue which is wrong.

I'm saving the custom attribute in quote_address, sales_order_address and customer_address_entity tabels.

extension_attributes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
        <attribute code="custom_foo" type="string"/>
    </extension_attributes>
    <extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
        <attribute code="custom_foo" type="string"/>
    </extension_attributes>
    <extension_attributes for="Magento\Sales\Api\Data\OrderAddressInterface">
        <attribute code="custom_foo" type="string"/>
    </extension_attributes>
    <extension_attributes for="Magento\Checkout\Api\Data\ShippingInformationInterface">
        <attribute code="custom_foo" type="string"/>
    </extension_attributes>
</config>

fieldset.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
    <scope id="global">
        <fieldset id="sales_convert_order_address">
            <field name="custom_foo">
                <aspect name="to_quote_address"/>
            </field>
        </fieldset>
        <fieldset id="sales_convert_quote_address">
            <field name="custom_foo">
                <aspect name="to_customer_address"/>
                <aspect name="to_order_address"/>
            </field>
        </fieldset>
        <fieldset id="sales_copy_order_billing_address">
            <field name="custom_foo">
                <aspect name="to_order"/>
            </field>
        </fieldset>
        <fieldset id="order_address">
            <field name="custom_foo">
                <aspect name="to_customer_address"/>
            </field>
        </fieldset>
    </scope>
</config>

LayoutProcessor.php (an example for shippingAddress field config)

$customFooConfig = array(
    'config'            => [
        'customScope' => 'shippingAddress.custom_attributes',
        'customEntry' => null,
        'template'    => 'ui/form/field',
        'elementTmpl' => 'ui/form/element/input',
        'id'          => 'custom-foo',
    ],
    'label'             => 'Custom Foo',
    'value'             => '',
    'dataScope'         => 'shippingAddress.custom_attributes.custom-foo',
    'provider'          => 'checkoutProvider',
    'id'                => 'custom-foo',
    'sortOrder'         => 1,
    'customEntry'       => null,
    'visible'           => true,
    'validation'        => [
        'required-entry' => true,
    ],
);

db_schema.xml

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="customer_address_entity">
        <column xsi:type="varchar" name="custom_foo" nullable="true" length="255" comment="Custom Foo"/>
    <table name="quote_address">
        <column xsi:type="varchar" name="custom_foo" nullable="true" length="255" comment="Custom Foo"/>
    </table>
    <table name="sales_order_address">
        <column xsi:type="varchar" name="custom_foo" nullable="true" length="255" comment="Custom Foo"/>
    </table>
</schema>

I have followed Magento 2 Guide – Adding New Checkout Filed step by step and the field is posting custom and extension attributes correctly. I've also created a new customer address attribute for editing the custom_foo attribute on customer account/addresses page which is working fine.

The only issue is that on order submit the custom_foo value is being saved as a key/par value which is the structure of customAttributes property. Am I missing something in the xml configuration(s) or should I parse the custom or extension attributes on quote/order submit and set the to address each time?

Best Answer

Issue was resolved with Magento 2.4.3-p1. What caused it is still unknown but it works as expected with the newer version of Magento.