Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save a default address programmatically in Magento 2?

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).

like image 396
Pallavi Avatar asked Dec 08 '22 22:12

Pallavi


1 Answers

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());
    }
like image 81
Vinoth kumar Avatar answered May 12 '23 05:05

Vinoth kumar