Magento 2 How to create, update and delete customer address using GraphQL
We all know that, when buying an item, addresses need to be added. Sometimes also need to update and delete the address. So we will teach you how to create, update and delete customer address.
Create customer address
Use the createCustomerAddress
mutation to create the customer’s address.
Syntax
mutation {
createCustomerAddress(
input: CustomerAddressInput!
) {
CustomerAddress
}
}
The following example create customer address.
Request
saveCopyzoom_out_map
mutation{
createCustomerAddress(
input: {
region: {
region: "Gujarat"
region_code: "GJ"
}
country_code: IN
street: "Ahmedabad"
telephone: "0123456789"
postcode: "380015"
city: "Ahmedabad"
firstname: "Payal"
lastname: "Patel"
default_shipping: true
default_billing: false
}
){
id
region{
region
region_code
}
country_code
street
telephone
postcode
city
firstname
lastname
default_shipping
default_billing
}
}
Response
saveCopyzoom_out_map{
"data": {
"createCustomerAddress": {
"id": 3,
"region": {
"region": "Gujarat",
"region_code": "GJ"
},
"country_code": "IN",
"street": [
"Ahmedabad"
],
"telephone": "0123456789",
"postcode": "380015",
"city": "Ahmedabad",
"firstname": "Payal",
"lastname": "Patel",
"default_shipping": true,
"default_billing": false
}
}
}
Update customer address
Use the updateCustomerAddress
mutation to update the customer’s address.
Syntax
mutation {
updateCustomerAddress(
id: Int!,
input: CustomerAddressInput
) {
CustomerAddress
}
}
The following example update customer address.
Request
saveCopyzoom_out_mapmutation {
updateCustomerAddress(
id: 3
input: { street: "Gandhinagar", city: "Gandhinagar", postcode: "380015" }
) {
id
street
city
postcode
}
}
Response
saveCopyzoom_out_map{
"data": {
"updateCustomerAddress": {
"id": 3,
"street": [
"Gandhinagar"
],
"city": "Gandhinagar",
"postcode": "380015"
}
}
}
Delete customer address
-
Use the
deleteCustomerAddress
mutation to delete the specified customer address. -
If you set it as a billing or shipping address, you can't delete it.
-
Syntax
mutation { deleteCustomerAddress( id: Int! ) { Boolean } }
-
The following call deletes a customer's address.
Request
saveCopyzoom_out_mapmutation { deleteCustomerAddress(id: 4) }
Response
saveCopyzoom_out_map{
"data": {
"deleteCustomerAddress": true
}
}
That's it!!
If you are looking for more GraphQL solutions then check out our another article which is List of GraphQL Queries and Mutations for Magento 2.
I hope this article helps you to find what you were looking for.
Bookmark it for your future reference. Do comment below if you have any other questions on that.
P.S. Do share this article with your team.