How do I save customer default addresses programmatically in Magento version 2?
I have extended editpost controller for the same but not getting how to save default address (new address and update too).
I found a solution to add customer address programmatically.
$addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');
$address = $addresss->create();
$address->setCustomerId($customer->getId())
->setFirstname('Mav')
->setLastname('rick')
->setCountryId('HR')
//->setRegionId('1') //state/province, only needed if the country is USA
->setPostcode('31000')
->setCity('Osijek')
->setTelephone('0038511223344')
->setFax('0038511223355')
->setCompany('GMI')
->setStreet('NO:12 Lake View')
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
try{
$address->save();
}
catch (Exception $e) {
Zend_Debug::dump($e->getMessage());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With