0

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?

1 Answer 1

1

First solution worked fine except of that you don't have a catch for the try.

Second solution worked fine after fixing, the setStreet() expects an array as param and not a string, unnecessary semicolon on setIsDefaultShipping() and there is no function setSaveInAddressBook() defined in

vendor/magento/module-customer/Api/Data/AddressInterface.php

so you have to use ->setData('save_in_address_book', '1');

2
  • can you update me setStreet code ? how you have used it, i am also facing same issue Commented Sep 6, 2018 at 12:54
  • @jafarpinjar this is how you can get setStreet working $address->setStreet(['my address']);
    – CodeGenius
    Commented Sep 26, 2018 at 11:07

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.