Edit Multiple Customer Address With Magento 2 REST API
In this post, I am going to explain to you how to edit or update customer multiple addresses by customer address id using rest api in magento 2.
You just have to use the address id in order to edit the customer address.
So in the first step, we have to get the customer address id, and then we can use that id to edit the address.
Step 1: Get Customer Address ID API
By using this rest api we will get the customer address details that will help us to edit the customer address.
If a customer has five shipping addresses then each address will have an id.
Whenever we need to change the customer address then we have to edit by its address id.
Endpoint:
<host>/rest/default/V1/customers/customersID
Example:
https://wishusucess.com/rest/default/V1/customers/111
Method: GET
Headers:
- Authorization: Bearer <admin_token>
- Content-Type: application/json
Payload:
Not required
Response:
{ "id": 111, "group_id": 4, "default_billing": "281", "default_shipping": "281", "created_at": "2021-12-24 12:03:06", "updated_at": "2022-02-24 08:57:48", "created_in": "Default Store View", "email": "cs.hemantsingh@gmail.com", "firstname": "Hemant", "lastname": "Singh", "middlename": "Singh", "gender": 0, "store_id": 1, "website_id": 1, "addresses": [ { "id": 217, "customer_id": 111, "region": { "region_code": "NY", "region": "New York", "region_id": 43 }, "region_id": 43, "country_id": "US", "street": [ "Add", "Add2" ], "telephone": "1234567890", "postcode": "98761", "city": "New York", "firstname": "Hemant", "lastname": "Singh" }, { "id": 226, "customer_id": 111, "region": { "region_code": null, "region": null, "region_id": 0 }, "region_id": 0, "country_id": "IN", "street": [ "201301, Noida" ], "telephone": "01710676441", "postcode": "203301", "city": "Noida", "firstname": "Hemant", "lastname": "Singh" }, { "id": 230, "customer_id": 111, "region": { "region_code": null, "region": null, "region_id": 0 }, "region_id": 0, "country_id": "IN", "street": [ "201302, Noida" ], "telephone": "777777777", "postcode": "203302", "city": "Noida", "firstname": "Hemant", "lastname": "Singh" } ], "disable_auto_group_change": 0, "extension_attributes": { "is_subscribed": false }, "custom_attributes": [ { "attribute_code": "customer_account_status", "value": "1" }, { "attribute_code": "payment_status", "value": "1" } ] }
Step 2: Edit Customer Shipping Address By Address ID
Now by sending the payload with customer address details we can edit/update customers' multiple addresses.
I am telling you about the Rest API on you, using which you can now change the address information of the customer.
One of the drawbacks of Magento's default customer edit API is that if you edit an address, all the rest of the addresses at once are removed and converted into a single address.
So you will need to do some customization which I have given in the link below.
Endpoint:
https://wishusucess.com/rest/V1/customers/111
Method: PUT
Headers:
- Authorization: Bearer <admin_token>
- Content-Type: application/json
Payload:
{ "customer": { "firstname": "Hemant", "lastname": "Singh", "store_id": 1, "website_id": 1, "addresses": [{ "id": 217, "customerId": 111, "region": { "region_code": "UP", "region": "UttarPradesh" }, "region_id": 566, "countryId": "IN", "street": [ "Saradarpur Colony" ], "telephone": "8197565002", "postcode": "201301", "city": "Noida", "defaultShipping": false, "defaultBilling": true }] } }
Response:
{ "id": 111, "group_id": 4, "default_billing": "217", "created_at": "2021-12-24 12:03:06", "updated_at": "2022-02-26 20:01:34", "created_in": "Default Store View", "email": "cs.hemantsingh@gmail.com", "firstname": "Hemant8", "lastname": "Singh", "middlename": "Singh", "gender": 0, "store_id": 1, "website_id": 1, "addresses": [ { "id": 217, "customer_id": 111, "region": { "region_code": "UP", "region": "Uttar Pradesh", "region_id": 566 }, "region_id": 566, "country_id": "IN", "street": [ "Sardarpur Colony" ], "telephone": "8197565002", "postcode": "201301", "city": "Noida", "firstname": "Hemant", "lastname": "Singh", "default_billing": true } ], "disable_auto_group_change": 0, "extension_attributes": { "is_subscribed": false }, "custom_attributes": [ { "attribute_code": "customer_account_status", "value": "1" }, { "attribute_code": "payment_status", "value": "1" } ] }
Update Address API by Address ID
Using this REST API in Magento 2, you can edit multiple addresses at once.
If there are 5 addresses added to a customer account, then all five customers' addresses can update the shipping address simultaneously with respect to the address ID.
This API can be used to change the shipping address information address of any customer.
First, you have to download the customer edit address by address id rest api from Wishusucess GitHub repository then install it into your store, and then follow the below endpoint.
Endpoint:
https://wishusucess.com/rest/V1/addresses
Method: POST
Headers:
- Authorization: Bearer <admin_token>
- Content-Type: application/json
Payload:
{
"address": {
"id": "7",
"customer_id": "3",
"firstname": "Hemant",
"lastname": "Singh",
"street": [
"Street 1"
],
"telephone": "01234 56789",
"postcode": "201301",
"city": "Noida",
"defaultBilling": true,
"country_id": "IN"
}
}
Response:
{ "id": 302, "customer_id": 111, "region": { "region_code": "UP", "region": "Uttar Pradesh", "region_id": 566 }, "region_id": 566, "country_id": "IN", "street": [ "Street 1" ], "telephone": "01234 56789", "postcode": "201301", "city": "Noida", "firstname": "Hemant", "lastname": "Singh" }
Conclusion:
We can edit or update customer address information by using the above rest api in Magento 2.
Firstly we have to get the address id then we can use that id to edit/update customer addresses by address id.
Read more: Magento 2.4 Installation Guide: How to Install Magento 2.4.2