Search code examples
magento2magento2.2

How to save a new address attribute in checkout


I am trying to save geolocation along with the customer address.

I have added Let & Lng using the install script

    $customerSetup->addAttribute('customer_address', 'latitude', [
        'type' => 'varchar',
        'label' => 'Latitude',
        'input' => 'text',
        'required' => false,
        'visible' => true,
        'visible_on_front' => true,
        'user_defined' => false,
        'sort_order' => 43,
        'position' => 43,
        'system' => 0,
    ]);

    $attributeLat = $customerSetup->getEavConfig()->getAttribute('customer_address', 'latitude')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address','customer_address'],
        ]);
    $attributeLat->save();
    //latitude - End

    $customerSetup->addAttribute('customer_address', 'longitude', [
        'type' => 'varchar',
        'label' => 'Longitude',
        'input' => 'text',
        'required' => false,
        'visible' => true,
        'visible_on_front' => true,
        'user_defined' => false,
        'sort_order' => 43,
        'position' => 43,
        'system' => 0,
    ]);

    $attributeLng = $customerSetup->getEavConfig()->getAttribute('customer_address', 'longitude')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address','customer_address'],
        ]);
    $attributeLng->save();
    //longitude - End}

I can update values in the admin backend without an any issue.

In the checkout page form fields for "latitude" & "longitude" are appearing. But values don't save along with the customer address.

I am using Magento CE 2.2.3


Solution

  • You need to add etc/extension_attributes.xml for Magento\Customer\Api\Data\AddressInterface:

    <extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
            <attribute code="longitude" type="string" />
        </extension_attributes>
    

    Add etc/fieldset.xml:

     <fieldset id="sales_convert_quote_address">
                <field name="longitude">
                    <aspect name="to_customer_address" />
                    <aspect name="to_order_address" />
                </field>
            </fieldset>
    

    Add plugin in di.xml for Magento\Customer\Model\Address where you need to save your custom attribute in the beforeUpdateData function

    <type name="Magento\Customer\Model\Address"> 
            <plugin disabled="false" name="vendor_plugin_quote_model_address" sortOrder="10" 
            type="Vendor\Module\Plugin\Customer\Model\Address"/>
        </type>
    
      public function beforeUpdateData(
            \Magento\Customer\Model\Address $subject,
            \Magento\Customer\Api\Data\AddressInterface $address
        )
    

    And then you should be able to see your attribute saved on the Customer Address.