How to Display Billing And Shipping Address by Order ID in Magento 2
Looking for a method to display billing and shipping address by order ID in Magento 2? Follow me… 🤞
Magento 2, being a highly flexible platform, makes it possible to create unique online shopping experiences! From changing the product page layout to offering customized products, the possibilities are immense. There can be multiple instances where you require to display billing or shipping address to the customers in Magento 2 frontend.
Let’s say, you want to acknowledge the customers on a successful order placement with their order details. Here, you may require to show the billing/shipping address by order increment ID on the Magento 2 Order Success Page.
In this blog post, I am going to share how to exactly do that!
Earlier, my colleague shared the method to get order information by order ID in Magento 2. I am going to use a similar approach to display billing and shipping addresses to customers in Magento 2.
Method to Display Billing and Shipping Address by Order ID in Magento 2
In order to display billing and shipping address by order increment ID in Magento 2 phtml file, we are going to create a new object model and load the order ID. Next, we will use the default functions such as getBillingAddress()
and getShippingAddress()
to fetch the required information.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$orderid = 5; $order = $objectManager->create('Magento\Sales\Model\Order')->load($orderid); // pass orderId $firstname = $order->getBillingAddress()->getFirstname(); $lastname = $order->getBillingAddress()->getLastname(); $billingaddress_tmp = $order->getBillingAddress(); $shippingaddress_tmp = $order->getShippingAddress(); echo "Dear $customer_name, "; echo "Your Billing Address is: $billingaddress_tmp "; echo "Your Shipping Address is: $shippingaddress_tmp "; echo "Thank you for ordering!"; |
That’s Easy! Isn’t that? 😛
I hope this blog post will help you to display billing and shipping address by order ID in Magento 2. ✨
If you still have any doubts or queries, feel free to ask in the comments. 😊
Thanks for reading! 🙌
Siddharth Nandava
Siddharth Nandava is an enthusiastic Jr Magento developer at Meetanshi. Apart from the work, you can find him learning new things and spending quality time with his family.
Prev
What is ChatGPT? OpenAI’s Chatbot Covered in a Nutshell
Magento 2.4.6 Release – Key Highlights & Features
Next