1

How can i change the default shipping address of a current user programmatically.

  • Are you want to create a new address and set that one as default? – Nikunj Vadariya May 24 '18 at 11:18
  • No. I am having three address A,B and C where C is set as default. Now i want to set Default shipping address to A. How can i do it? – Avesh Naik May 24 '18 at 11:26
  • Check my answer, I hope it will work. – Nikunj Vadariya May 24 '18 at 11:38
  • if my answer solves your concern then there is green right tick mark, hit that tickmark for right answer that will help future readers. – Nikunj Vadariya May 24 '18 at 11:57
2

Try this code:

$YourAddressId = 1; //Your address id which you want to set as default;

$address = $objectManager->create('Magento\Customer\Model\Address')->load($YourAddressId);

$address->setCustomerId($customer->getId())
$address->setIsDefaultShipping('1');

try{
        $address->save();
}
catch (Exception $e) {
        Zend_Debug::dump($e->getMessage());
}
  • Your method is right but will it be for a current user ? This could change other users address. – Avesh Naik May 24 '18 at 11:47
  • Or we can search that address present for the current user if yes then change. what you think? – Avesh Naik May 24 '18 at 11:50
  • @avesh I have update my answer. you can pass customer id which you want to update. by $address->setCustomerId($customer->getId()) – Nikunj Vadariya May 24 '18 at 11:56
  • setIsDefaultBilling() method is not part of the Magento\Customer\Model\Address – Avesh Naik May 24 '18 at 12:01
  • What error you got ? – Nikunj Vadariya May 24 '18 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.