In this article, you going to see how to achieve in Magento 2 change shipping address programmatically. we going to see in detail step by step.

Let’s start with the topic of Magento 2 set shipping address programmatically a step by step :

To achieve this we going to use Address Repository Interface so that it became easy to change.

Or to set default shipping address programmatically.

We need Customer detail that is Id to set or a change shipping address and address id you want to assign it.

The most important point is firstly on the page load get the existing address by a method called getById of Address Repository Interface of the set customer.

<?phpnamespace Techone\SetShippingAddress\Model;use Psr\Log\LoggerInterface;use Magento\Customer\Api\AddressRepositoryInterface;use Magento\Customer\Api\CustomerRepositoryInterface;class DefaultShipping{ /** * @var CustomerRepositoryInterface */ private $customerRepository; /** * @var AddressRepositoryInterface */ private $addressRepository; /** * @var LoggerInterface */ private $logger; public function __construct( CustomerRepositoryInterface $customerRepository, AddressRepositoryInterface $addressRepository, LoggerInterface $logger ) { $this->customerRepository = $customerRepository; $this->addressRepository = $addressRepository; $this->logger = $logger; } /** * assign default shipping address * * @param int $customerId * @param int $addressId * @return void */ public function setShippingAddress(int $customerId, int $addressId) { try { $address = $this->addressRepository->getById($addressId)->setCustomerId($customerId); $address->setIsDefaultShipping(true); $this->addressRepository->save($address); } catch (\Exception $exception) { $this->logger->critical($exception); } }}

To set or change the current address id to default address for the given or existing customer use method.

The method called setting default shipping address $address->setIsDefaultShipping(true).

The Ultimate Cheat Sheet on Magento 2 Get Shipping Method Title From Quote

This method is called by using the required parameter.

$customerId = 11;$addressId = 6;$result = $this->setShippingAddress($customerId, $addressId);

When you execute this above method, you going to see that the address is get updated.

To cross check you can check in my account section of the frontend.

To set default billing address Magento 2

Similarly we can achieves for setting up default billing address by passing parameter of customer id and address id.

$result = $this->setBillingAddress($customerId, $addressId);

Using above code you can similarly achieve magento 2 set default billing address programmatically.

Another way you set or change the shipping address or billing address for a specific customer using object manager in Magento 2.

$AddressId = 1; //Your address id which you want to set as default;$address = $objectManager->create('Magento\Customer\Model\Address')->load($AddressId);$address->setCustomerId($customer->getId())$address->setIsDefaultShipping('1');try{ $address->save();}catch (Exception $e) { Zend_Debug::dump($e->getMessage());}

In the above code as you see, we passing the addressid parameter using the object manager to load the existing address for a customer.

Using the above code, you can similar way do magento 2 set quote shipping address programmatically

Suppose you want to set shipping method to the quote, then you have get shipping details using below code

$method = $quote->getShippingAddress()->getShippingMethod();$rate = $quote->getShippingAddress()->getShippingRateByCode($method);

then with the help of quote address interface \Magento\Quote\Api\Data\CartInterface and use the below code

public function setShippingMethod($method);

To customer we pass the customer id parameter, so that for that particular customer id address id get u

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.