{"id":721,"date":"2019-12-23T11:05:31","date_gmt":"2019-12-23T11:05:31","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/12\/23\/set-shipping-and-billing-address-from-order-to-customer-address-in-magento-2\/"},"modified":"2025-06-06T14:44:01","modified_gmt":"2025-06-06T09:14:01","slug":"set-shipping-and-billing-address-from-order-to-customer-address-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/set-shipping-and-billing-address-from-order-to-customer-address-in-magento-2\/","title":{"rendered":"Set Shipping And Billing Address From Order To Customer Address In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 <a href=\"https:\/\/meetanshi.com\/blog\/configure-multiple-shipping-addresses-in-magento-2\/\">multiple shipping addresses<\/a> 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 <a href=\"https:\/\/meetanshi.com\/blog\/dynamically-change-billing-address-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Dynamically Change Billing Address in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to set shipping and billing address from order to customer address in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Copy the below code at <em><strong>app\/code\/Vendor\/Module\/Helper\/Data.php<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Helper;\n\nuse Magento\\Framework\\App\\Helper\\Context;\nuse Magento\\Store\\Model\\StoreManagerInterface;\nuse Magento\\Customer\\Model\\CustomerFactory;\nuse Magento\\Sales\\Model\\OrderFactory;\nuse Magento\\Customer\\Model\\AddressFactory;\n\nclass Data extends AbstractHelper\n{\n    private $storeManager;\n    private $customerFactory;\n    private $orderFactory;\n    private $addressFactory;\n\n    public function __construct\n    (\n        Context $context,\n        StoreManagerInterface $storeManager,\n        CustomerFactory $customerFactory,\n        OrderFactory $orderFactory,\n        AddressFactory $addressFactory\n    )\n    {\n        $this->storeManager = $storeManager;\n        $this->customerFactory = $customerFactory;\n        $this->orderFactory = $orderFactory;\n        $this->addressFactory = $addressFactory;\n        parent::__construct($context);\n    }\n\n    public function setCustomerAddress($orderId)\n    {\n        $customer = $this->customerFactory->create();\n        $websiteId = $this->storeManager->getStore()->getWebsiteId();\n        $orderData = $this->orderFactory->create()->load($orderId);\n        $customer->setWebsiteId($websiteId);\n   \n        \/* Customer already exist *\/\n        $customerModel = $customer->loadByEmail($orderData->getCustomerEmail());\n        $customerId = $customerModel->getId();\n\n        \/* save billing Address *\/\n        $address = $this->addressFactory->create();\n        $address->setData($orderData->getBillingAddress()->getData());\n\n        $address->setCustomerId($customerId)\n            ->setIsDefaultBilling('1')\n            ->setIsDefaultShipping('0')\n            ->setSaveInAddressBook('1');\n        $address->save();\n\n        \/* save shipping Address *\/\n        if (!$orderData->getIsVirtual()) {\n            $address = $this->addressFactory->create();\n            $address->setData($orderData->getShippingAddress()->getData());\n\n            $address->setCustomerId($customerId)\n                ->setIsDefaultBilling('0')\n                ->setIsDefaultShipping('1')\n                ->setSaveInAddressBook('1');\n            $address->save();\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please share the solution with fellow developers via social media.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-721","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"predecessor-version":[{"id":16443,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions\/16443"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}