I have tried 2 kind of solutions
//First kind----------------------------------
$addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');
$address = $addresss->create();
$address->setCustomerId($customer->getId())
->setFirstname('vinoth')
->setLastname('kumar')
->setCountryId('IN')
//->setRegionId('1') //state/province, only needed if the country is USA
->setPostcode('31000')
->setCity('Osijek')
->setTelephone('0038511223344')
->setFax('0038511223355')
->setCompany('GMI')
->setStreet('Kersov')
->setIsDefaultBilling('1')
->setIsDefaultShipping('0')
->setSaveInAddressBook('1');
try{
$address->save(); echo'asdasd'; exit; //Not saving data. Refreshes the page. Not printing the echo.
}
// Second kind ---------------------------------------
public function __construct(
\Magento\Directory\Model\Region $region
\Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepositoryInt
) {
$this->_region = $region;
$this->_addressDataFactory = $addressDataFactory;
$this->_addressRepository = $addressRepositoryInt;
}
public function updateAddress($custId){ echo $custId;
$directory = $this->_region->create();
$addressIntFct = $this->_addressDataFactory->create();
$addressIntFct->setCustomerId('id here')
->setFirstname('firstName')
->setLastname('lastName')
->setCountryId('US')
->setPostcode(98089)
->setCity('city')
->setTelephone('999999999')
->setFax('999999999')
->setCompany('company')
->setStreet('street')
->setRegionId('region id here')
->setIsDefaultBilling(1)
->setIsDefaultShipping(0);
->setSaveInAddressBook('1');
try{
$this->_addressRepository->save($addressIntFct); echo'asdasd'; exit; //Not saving data. Refreshes the page. Not printing the echo.
}catch (Exception $e) {
Zend_Debug::dump($e->getMessage());
}
}
No exception or errors are in log file.
Any idea why its not working?