Set Shipping And Billing Address From Order To Customer Address In Magento 2
The post gives the method to set shipping and billing address from order to customer address in Magento 2 store. Whether it is a guest checkout or a registered customer, you may require this method to set the addresses.
Magento 2 store allowing guest checkout can make it easier for the users to register by auto account creation. However, the account sign up requires address which can be fetched from the shipping or billing address used for the checkout. Additionally, Magento 2 also provides the option for users to add multiple shipping addresses to their account. This can be useful for users who frequently send gifts or purchase items for different people or locations. You may also love to read our blog post on How to Dynamically Change Billing Address in Magento 2.
Moreover, it may so happen that a registered customer is using a different shipping address than that used while sign up. The below method allows saving that addresses also in the customer.
Method to set shipping and billing address from order to customer address in Magento 2:
Copy the below code at app/code/Vendor/Module/Helper/Data.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?php namespace Vendor\Module\Helper; use Magento\Framework\App\Helper\Context; use Magento\Store\Model\StoreManagerInterface; use Magento\Customer\Model\CustomerFactory; use Magento\Sales\Model\OrderFactory; use Magento\Customer\Model\AddressFactory; class Data extends AbstractHelper { private $storeManager; private $customerFactory; private $orderFactory; private $addressFactory; public function __construct ( Context $context, StoreManagerInterface $storeManager, CustomerFactory $customerFactory, OrderFactory $orderFactory, AddressFactory $addressFactory ) { $this->storeManager = $storeManager; $this->customerFactory = $customerFactory; $this->orderFactory = $orderFactory; $this->addressFactory = $addressFactory; parent::__construct($context); } public function setCustomerAddress($orderId) { $customer = $this->customerFactory->create(); $websiteId = $this->storeManager->getStore()->getWebsiteId(); $orderData = $this->orderFactory->create()->load($orderId); $customer->setWebsiteId($websiteId); /* Customer already exist */ $customerModel = $customer->loadByEmail($orderData->getCustomerEmail()); $customerId = $customerModel->getId(); /* save billing Address */ $address = $this->addressFactory->create(); $address->setData($orderData->getBillingAddress()->getData()); $address->setCustomerId($customerId) ->setIsDefaultBilling('1') ->setIsDefaultShipping('0') ->setSaveInAddressBook('1'); $address->save(); /* save shipping Address */ if (!$orderData->getIsVirtual()) { $address = $this->addressFactory->create(); $address->setData($orderData->getShippingAddress()->getData()); $address->setCustomerId($customerId) ->setIsDefaultBilling('0') ->setIsDefaultShipping('1') ->setSaveInAddressBook('1'); $address->save(); } } } |
That’s it.
Any doubts on the topic can be mentioned in the Comments section below and I’d be glad to help you out.
Please share the solution with fellow developers via social media.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How To Reorder Product Tabs In Magento 2
Solved – Undefined index: jobs Error On Running Cron In Magento 2.X.X
Next