PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 22, 2022

[FIXED] How to save a new address attribute in checkout

 August 22, 2022     magento2, magento2.2     No comments   

Issue

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.



Answered By - AdrianaCatoiu
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,205,137

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing