0

Can anyone mention the Jason payload to add new address in existing customer.

yes its a shipping address

How to add new Address using REST API, While placing an order.

1
  • Arsalan, if any answer helps you then you mark accepted to those answer, so it will helpful for future readers, you can mark accept answer by clicking (✓) next to answer. Jan 17, 2019 at 6:24

3 Answers 3

1
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');
        $address = $addresss->create();

        $address->setCustomerId($customerId)
            ->setFirstname($firstName)
            ->setLastname($lastName)
            ->setCountryId($countryCode)
            ->setPostcode($postCode)
            ->setCity($city)
            ->setTelephone($telephone)
            ->setFax($fax)
            ->setCompany($company)
            ->setStreet($street)
            ->setIsDefaultBilling('0')
            ->setIsDefaultShipping('0')
            ->setSaveInAddressBook('1');

        if($address->save()){
            return true;
        }else{
            return false;
        }

{ "data": { "cust_id": x, "email": "[email protected]", "first_name": "xxx", "last_name": "xxx", "region_code": "xxx", "country_id": "xx", "street": [ "road4" ], "telephone": "xxxxxxx", "postcode": "xxxx", "city": "xxxxx", "company":"", "fax":"", "default_shipping": "1", "default_billing": "1" } }

1

First make a GET request to {{host}}/rest/{{store_code}}/V1/customers/me.

If the addresses nodes contains an array of address objects these need to be sent with the next request otherwise the addresses will be deleted from the customer's account. Tok keep the existing addresses they must be sent with the ID that was returned in the original request. You should also send all the existing data in the address object otherwise the address will be updated.

Next make a PUT request to the same URL with a request body like:

{
    "customer": {
        "email": "[email protected]",
        "firstname":"John",
        "lastname":"Doe",
        "website_id": 1,
        "addresses": [
            {
                "firstname": "John",
                "lastname": "Doe",
                "street": [
                  "123 Some Street",
                  "Additional street if necessary"
                ],
                "city": "Some City",
                "postcode": "AB1 2CD",
                "country_id": "GB",
                "telephone": "077777777777",
                "default_billing": true,
                "default_shipping": true,
            }
        ]
    }
}
0

As I understand form your question you need to add shipping address while placing an order from REST API

You can use shipping-information API

Get & put Shipping Information.

http://magento-host/index.php/rest/V1/carts/mine/shipping-information
  • method : POST
  • Authorization : Bearer <customer token>
  • body data : json

{
    "addressInformation": {
        "shippingAddress": {
            "region": "MH",
            "region_id": 0,
            "country_id": "IN",
            "street": [
                "221,Baker-street (e)"
            ],
            "company": "Lumos",
            "telephone": "12345678",
            "postcode": "400001",
            "city": "Mumbai",
            "firstname": "Aditya",
            "lastname": "Shah",
            "email": "[email protected]",
            "prefix": "address_",
            "region_code": "MH",
            "sameAsBilling": 1
        },
        "billingAddress": {
            "region": "MH",
            "region_id": 0,
            "country_id": "IN",
            "street": [
                "221,Baker-street (e)"
            ],
            "company": "Lumos",
            "telephone": "12345678",
            "postcode": "4000001",
            "city": "Mumbai",
            "firstname": "Aditya",
            "lastname": "Shah",
            "email": "[email protected]",
            "prefix": "address_",
            "region_code": "MH"
        },
        "shipping_method_code": "flatrate",
        "shipping_carrier_code": "flatrate"
    }
}

5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.