4

How can I update customer address by address id in Magento 2 programmatically ?

Arnaud
  • 320
  • 4
  • 11
srg
  • 455
  • 3
  • 10
  • 27

2 Answers2

9

If you know the address id ($addressId) you can use the address repository to load and save updated addresses:

/**
 * @var \Magento\Customer\Api\AddressRepositoryInterface
 */
protected $addressRepository;

/**
 * Sync constructor.
 * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
 */
public function __construct(
    \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
) {
    $this->addressRepository = $addressRepository;
}

public function changeAddress($addressId)
{
    /** @var \Magento\Customer\Api\Data\AddressInterface $address */
    $address = $this->addressRepository->getById($addressId);
    $address->setCity('customCity'); // Update city
    $address->setCountryId('UK'); // Update country id 
    // update what ever you want
    $this->addressRepository->save($address);
}
Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83
  • @srg I'm glad to help you :) – Siarhey Uchukhlebau Aug 08 '17 at 12:25
  • How to check if address id exist or not? – Jafar Pinjar Aug 30 '18 at 14:33
  • getById is not working for me – Jafar Pinjar Aug 31 '18 at 09:38
  • @jafarpinjar it will work when you use a try-catch construction – Siarhey Uchukhlebau Aug 31 '18 at 15:12
  • no Siarhey, i have used it, its not working for me, here is the link for my issue https://magento.stackexchange.com/questions/240352/how-to-update-customer-address-using-custom-api-in-magento2 – Jafar Pinjar Aug 31 '18 at 15:26
  • I am getting this error, Invalid value of "1" provided for the regionId field. – Jafar Pinjar Sep 01 '18 at 06:12
  • region and region id is not working – Jafar Pinjar Sep 01 '18 at 09:06
  • @SiarheyUchukhlebau, This code is not working when I tried to update the data. After this save operation, I'm trying to get the city value, it returns the updated value only. But not reflecting in db and frontend. – Ramya Nov 16 '20 at 05:23
  • @Ramya It will change only the customer address, not shipping or billing address in existing orders. – Siarhey Uchukhlebau Nov 16 '20 at 11:04
  • @jafarpinjar When you are changing the country_id field you must manually change the region_id from the list of corresponding values specific for that country. – Siarhey Uchukhlebau Nov 16 '20 at 11:06
  • @SiarheyUchukhlebau, If I want to update the shipping and billing address means, how do I proceed?. The customer address was saved at customer_address_entity table at the time of registartion. But that table data with id (29) and the shipping and billing address edit id also 29. So, I'm trying to update the same address only – Ramya Nov 16 '20 at 11:15
  • What happens if $addressId does not exist? I get exception, but it does not mean. – Dhaduk Mitesh May 26 '22 at 09:47
  • @DhadukMitesh Hi. In case address does not exist you must catch the NoSuchEntityException and create a new address entity from scratch (using the address factory). Then you can proceed using code from the answer. If you want to get more detailed answer, please write you own question with code you are using and give me a link. I will try to help you :) – Siarhey Uchukhlebau May 26 '22 at 09:52
  • @SiarheyUchukhlebau Thanks...!!! I got the non exists address id using the repository but it was directly through exception. Generally, If no record is found then the model/factory return null. So I just confirm with you. – Dhaduk Mitesh May 26 '22 at 12:34
3

If you want to Update address by addressID with region details

Use below code

$adddress_id = 12;
$obj = \Magento\Framework\App\ObjectManager::getInstance();
$address = $obj->create('\Magento\Customer\Model\Address')->load($adddress_id);
$address->setTelephone('7894561230')
        ->setCountryId('US')
        ->setPostcode('12345-6789)
        ->setCity('boston')
        ->setRegionId(57)
        ->setRegionCode('TX')
        ->setRegion('Texas')
        ->setStreet('102 san diago') //you can also pass array for street
        ->setSaveInAddressBook('1')
        ->setIsDefaultShipping('1')
        ->save();

NOTE: You should not use the ObjectManager directly!

Tirth Patel
  • 1,039
  • 8
  • 26