Magento 2 Change shipping address programmatically

  • Magento
Magento 2 Change shipping address programmatically

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

<?php
namespace 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *