Change the Customer Address Field Order in Magento 2

Many times it happens in Magento 2 that the customer has a requirement that swap customer address fields many times up and down, then suddenly we do not understand how to do it.

For this, I am going to tell you a very easy way, by adopting which you can easily change the order of the fields of the customer's address from anywhere to anywhere.

If you want to bring the field of the telephone number to the top, then how will you change its sort order or what will have to be done to change its position, I am going to tell you all this.

 

Way 1: Swap Customer Address Fields

If you want to change the delivery address or shipping address field of the customer, then for that you have to first know the ID of that particular field.

Then the short order of that ID has to be known, after that you can easily change its order by running a command in the database.

Example: Change customer telephone order on checkout page delivery address.

You have to find out the id of a telephone number in your eav_attribute database table.

Swap Customer Address Fields

Or just run the below command.

SELECT * FROM `eav_attribute` WHERE `attribute_code` LIKE '%telephone%';

Now you will get the result and you can easily check the id of that field.

Now go to the 'customer_eav_attribute' table and check that attribute_id.

or you can run the below command to get the result.

SELECT * FROM `customer_eav_attribute` WHERE `attribute_id` = 34;

Change Sort order of telephone

Now update the 'sort_order' field in customer_eav_attribute to change the delivery order telephone number.

UPDATE `customer_eav_attribute` SET `sort_order` = '50' WHERE `customer_eav_attribute`.`attribute_id` = 34;

Similarly, you can change the order of any fields like name, last name, region_id, city, etc.

 

Recommended Post: Reduce Website Bounce Rate: Top 17 Amazing Tips in 2022

 

Ways 2: Change Via checkout_index_index.xml

Now all these changes can also be done from the checkout index index.xml file of Magento 2.

For that, you have to go to that file and add the sort order then you have to run some upgrade and deploy commands and when you check by clearing the cache, you will see that the sort order of the address field has changed.

Example:

<item name="country_id" xsi:type="array">
<item name="sortOrder" xsi:type="string">100</item>
</item>
<item name="region_id" xsi:type="array">
<item name="sortOrder" xsi:type="string">101</item>
</item>

If instead, you will give a sort order lower than 50, then fields will be displayed before the street:

<item name="country_id" xsi:type="array">
<item name="sortOrder" xsi:type="string">1</item>
</item>
<item name="region_id" xsi:type="array">
<item name="sortOrder" xsi:type="string">2</item>
</item>

 

Way 3: Swap Customer Address Fields Via Plugin

This is the 3rd option that you can use to change the sort order of customer delivery address fields to anywhere in Magento 2.

You have to create a plugin that will change the behavior of the method.

Magento\Checkout\Block\Checkout\LayoutProcessor
class Reorder
{

public function afterProcess($subject, $jsLayout)
{

$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
['children']['shippingAddress']['children']['shipping-address-fieldset']
['children']['street']['sortOrder'] = 100;

return $jsLayout;
}
}

 

Related Posts:

How to Add Product Image Zoom Functionality in Magento 2

Magento Reindexing is Taking Too Much Time: Solution

 

Reference

Magento Stackexchange.com