How to Dynamically Change Billing Address in Magento 2
In this blog post, I have provided a complete solution to dynamically change billing address in Magento 2.
The billing address defines a physical location/address where the invoices or bills of the online orders are sent to the customers. In special cases, you may require to dynamically change the billing address of specific customers after placing the orders on your Magento 2 store.
Suppose you are running an online store on Magento 2 and have tied up with another company to deliver some essential products to their employees. In that case, you may require to dynamically change the shipping address of such orders to that of the company where you want to send the invoices. This process can be made easier if your e-commerce platform allows for the management of multiple shipping addresses, as you can easily add and switch between different addresses for your orders
To do that, you might need to define customers’ attributes and change the billing addresses of customers having specific attributes. You can refer to the programmatic solution provided here for that.
Steps to Dynamically Change Billing Address in Magento 2
In order to add dynamic billing address to orders in Magento 2, you may first require to register a new module by creating registration.php file at app/code/Vendor/Module/ directory and adding the following code:
1 2 3 4 5 6 7 8 9 10 11 |
app/code/Vendor/Module/registration.php <?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register( ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__ ); |
1 2 3 4 5 6 7 |
app/code/Vendor/Module/etc/module.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_Module" setup_version="1.0.0"> </module> </config> |
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
app/code/Vendor/Extension/etc/events.xml <event name="checkout_onepage_controller_success_action"> <observer name="vendor_extension_order_place_after " instance="Vendor\Extension\Observer\AfterPlaceOrder"/> </event> 4) app/code/Vendor/Extension/Observer/AfterPlaceOrder.php <?php namespace Vendor\Extension\Observer; use Exception; use Magento\Framework\DataObject; use Magento\Framework\Event\Observer as EventObserver; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\AlreadyExistsException; use Magento\Framework\Message\ManagerInterface; use Magento\Sales\Model\Order; use Magento\Sales\Model\OrderFactory; use Magento\Sales\Model\ResourceModel\OrderFactory as OrderResourceModelFactory; use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory; /** * Class AfterPlaceOrder */ class AfterPlaceOrder implements ObserverInterface { /** * @var RegionCollectionFactory */ private $regionCollectionFactory; /** * @var OrderResourceModelFactory */ private $orderResourceModelFactory; /** * @var OrderFactory */ protected $orderModel; /** * @var ManagerInterface */ private $messageManager; /** * PlaceOrder constructor. * @param ManagerInterface $messageManager * @param OrderResourceModelFactory $orderResourceModelFactory * @param RegionCollectionFactory $regionCollectionFactory * @param OrderFactory $orderModel */ public function __construct( ManagerInterface $messageManager, OrderResourceModelFactory $orderResourceModelFactory, RegionCollectionFactory $regionCollectionFactory, OrderFactory $orderModel ) { $this->regionCollectionFactory = $regionCollectionFactory; $this->orderResourceModelFactory = $orderResourceModelFactory; $this->orderModel = $orderModel; $this->messageManager = $messageManager; } /** * @param EventObserver $observer * @throws Exception */ public function execute(EventObserver $observer) { try { /** * @var Order $order */ $order = $observer->getEvent()->getOrder(); if ($order == null) { $orders = $observer->getEvent()->getOrders(); foreach ($orders as $order) { $this->setBillingAddress($order); } } else { $this->setBillingAddress($order); } } catch (Exception $e) { $this->messageManager->addErrorMessage($e->getMessage()); } } /** * @param $order * @throws AlreadyExistsException */ public function setBillingAddress($order) { $regionInfo = $this->getRegionInfo('South Australia'); $order->getBillingAddress()->setStreet('street line'); $order->getBillingAddress()->setCity('melbourne'); $order->getBillingAddress()->setPostcode('3002'); $order->getBillingAddress()->setCountryId('AU'); $order->getBillingAddress()->setRegionId($regionInfo->getRegionId()); $order->getBillingAddress()->setRegionCode($regionInfo->getRegionCode()); $order->getBillingAddress()->setRegion('South Australia'); $order->getBillingAddress()->setTelephone('12456465'); $this->orderResourceModelFactory->create()->save($order); } /** * @param string $regionName * @return DataObject */ public function getRegionInfo(string $regionName) { return $this->regionCollectionFactory->create() ->addRegionNameFilter($regionName) ->getFirstItem(); } } |
Magento 2 store owners can use this programmatic solution to change the billing address of orders dynamically as per their requirements.
Also read:
- How to Add or Remove Address Field from PDF in Magento 2
- How to Change Shipping Price on Address Field Change in Magento 2 Custom Shipping Method
- How to Change Number of Lines in Street Address in Magento 2
If you still have any doubts or queries regarding the solution, feel free to mention them in the comments section below.
I would be happy to help. 😊
Also, do not forget to share this Magento 2 solution with your Magento friends via social media.
Thanks for reading. 😃
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
2 Comments
how to do the same on the checkout page? before placing an order?
Hello Akash,
You’ll need to call controller before placing an order, in order to change billing address.
Thank you