Register Customer using API
webapi.xml
<route url="/V1/customers" method="POST">
<service class="Magento\Customer\Api\AccountManagementInterface" method="createAccount"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
url http://<magento-host>/rest/V1/customers
url action :: POST
Body > raw > JSON(application/json)
{
"customer":{
"email":"Test@example.com",
"firstname":"Aditya",
"lastname":"Shah",
"websiteId":"1",
"addresses":[
{
"customer_id":"1",
"firstname":"Aditya",
"lastname":"Shah",
"company":"ABC Manufacturing",
"telephone":"555-555-5555",
"city":"Boston",
"region":"Massachusetts",
"postcode":"02115",
"country_id":"US",
"street":[
"404 Main Street",
"PO Box 321"
]
}
]
},
"password": "Account@123"
}
You need to call below api for customer address update
API : xyz.com/rest/V1/customers/2
Header:
Authorization :"Bearer ishamls4xu7pnwhlvup141502tjm3cl2"
Content-Type :"application/json"
Method : PUT
Parameter :
{
"id": 2,
"group_id": 1,
"default_billing": "1",
"default_shipping": "2",
"created_at": "2017-04-26 14:31:22",
"updated_at": "2018-09-01 08:08:47",
"created_in": "Default Store View",
"email": "abc@xyz.com",
"firstname": "ABC",
"lastname": "XYZ",
"store_id": 1,
"website_id": 1,
"addresses": [
{
"id": 1,
"customer_id": 2,
"region": {
"region_code": null,
"region": null,
"region_id": 0
},
"region_id": 0,
"country_id": "MY",
"street": [
"No 545 Jalan balau 27/13",
"sinar link Taman rinting"
],
"company": "Mahnazfood",
"telephone": "04040404040404",
"fax": "01010101101010101",
"postcode": "81750",
"city": "Masai",
"firstname": "ABC",
"lastname": "XYZ",
"default_billing": true
},
{
"id": 2,
"customer_id": 2,
"region": {
"region_code": null,
"region": null,
"region_id": 0
},
"region_id": 0,
"country_id": "MY",
"street": [
"Colony#3 Block#126/F",
"Address 2"
],
"company": "Prashant",
"telephone": "9999999999",
"fax": "00000000000000",
"postcode": "45000",
"city": "Lahore",
"firstname": "Test",
"lastname": "Test",
"default_shipping": true
}
],
"disable_auto_group_change": 0,
"extension_attributes": {
"is_subscribed": false
}
}
Note: Pass address Id in parameter so it will update existing address,
if you not pass address id in parameter it will create new address for
customer
Best Answer
In Magento 1, any class which extended from
Varien_Object
had access to a methodgetData
which would return the data property of the class as an array.In Magento 2, there is no
Varien_Object
and nogetData
method. Data Models still have a data property, however, but it is of protected visibility and there is no accessor method for the whole array.The only way to get at this property, then, is to use the
__toArray
method, in order to tell Magento to create an array representation of the data property.This method is defined in
\Magento\Framework\Api\AbstractSimpleObject
, so you will only be able to use it if this class is in your model's inheritance tree.A word of warning before copy-pasting the above, however: The code snippet above depends on whether your Customer Data Model also extends from
\Magento\Framework\Api\AbstractSimpleObject
.The default, as defined in
vendor/magento/module-customer/etc/di.xml
specifies\Magento\Customer\Model\Data\Customer
as the concrete implementation, which does include\Magento\Framework\Api\AbstractSimpleObject
in it's inheritance tree, which is why the code snippet above works.