How to add a new address (shipping or billing or another) via Magento 2 REST API?
-
14-04-2021 - |
Вопрос
There are API's available to fetch and delete address of a customer but not create a new one.
GET /V1/customers/addresses/:addressId
GET /V1/customers/me/billingAddress
GET /V1/customers/:customerId/billingAddress
GET /V1/customers/me/shippingAddress
GET /V1/customers/:customerId/shippingAddress
DELETE /V1/addresses/:addressId
POST Api's are not available publicly?
Решение
I was having a similar problem and was digging into the AccountManagement class to find the proper payload. I noticed that the createAccount function checks if a customer ID exists and loads the stored data.
By performing a POST /V1/customers/:id
, I was able to add another address with the following payload:
{ "customer":
{
"email":"testing2@testing.com",
"firstname":"test",
"lastname":"test",
"websiteId": 0,
"addresses": [
{
"id": 9,
"firstname": "test",
"lastname": "test",
"company": "test technology",
"street": [
"Test Street 9",
"Tiny House 9"
],
"city": "City Nine",
"region_id": 12,
"region": "California",
"postcode": "91790",
"country_id": "US",
"telephone": "1234567890"
},
{
"firstname": "test",
"lastname": "test",
"company": "test technology",
"street": [
"Test Street 10",
"Tiny House 10"
],
"city": "City Ten",
"region_id": 12,
"region": "California",
"postcode": "91790",
"country_id": "US",
"telephone": "1234567890"
}
]
}
By post method how did you do this actually when i am trying it is showing me error of missing route did you write webapi.xml code for this route.
Another Way:
add endp point app/code/YourCompany/Customer/etc/webapi.xml
<route url="/V1/addresses" method="POST">
<service class="Magento\Customer\Api\AddressRepositoryInterface" method="save"/>
<resources>
<resource ref="Magento_Customer::manage"/>
</resources>
</route>
Body:
{
"address": {
"customer_id": "363",
"defaultShipping": true,
"defaultBilling": true,
"region": "Adabor - Dhaka",
"country_id": "BD",
"street": [ "304, Tejgaon IA" ],
"postcode": "1208",
"city": "Dhaka",
"firstname": "Matin",
"lastname": "Api Adrss",
"telephone": "01717676441",
"countryId": "BD"
}
}
If Update Existing address add addressID on body preload:
"id": 323,
"customer_id": "363",
Post: http://yourstore.com/rest/V1/addresses [admin integration token]
[get CustomerID http://yourstore.com/rest/V1/customers/me getMethod customer token]
Другие советы
It should be possible to add/update customer addresses using customer repository API:
PUT /V1/customers/:id (for admin)
PUT /V1/customers/me (for customer)
I'm using Magento 2.2, PUT and POST both do not work for adding a new address to an existing account. I get the following error:
"A customer with the same email already exists in an associated website"
Is adding an address to an existing Magento customer only supported in version 2.3 and later?
/V1/customers/me
This endpoint is available only for customer and with ajax.