How to Get Order Information By Order Id in Magento 2
The Magento 2 developers know the crazy client requirements 😄
If you have been there too, you might know what I’m talking about. But for newbies who are yet to face them, this post might be helpful!
It gives you the programmatic solution to get order information by order ID in Magento 2.
You can use this solution in different scenarios like restrict order based on the delivery address, offer a specific discount based on the order amount, offer a free product when a customer buys a particular item, etc.
With the below solution, you can get the order details like order items, order amount, billing and shipping addresses, order payment method, billing details, quantity, and the customer name.
Method to Get Order Information By Order Id in Magento 2:
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 |
 $orderId = 123;  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();  $order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);  // Get Order Information  $order->getEntityId();  $order->getIncrementId();  $order->getState();  $order->getStatus();  $order->getStoreId();  $order->getGrandTotal();  $order->getSubtotal();  $order->getTotalQtyOrdered();  $order->getOrderCurrencyCode();  // get customer details  $custLastName = $orders->getCustomerLastname();  $custFirsrName = $orders->getCustomerFirstname();  // get Billing details   $billingaddress = $order->getBillingAddress();  $billingcity = $billingaddress->getCity();     $billingstreet = $billingaddress->getStreet();  $billingpostcode = $billingaddress->getPostcode();  $billingtelephone = $billingaddress->getTelephone();  $billingstate_code = $billingaddress->getRegionCode();  // get shipping details  $shippingaddress = $order->getShippingAddress();      $shippingcity = $shippingaddress->getCity();  $shippingstreet = $shippingaddress->getStreet();  $shippingpostcode = $shippingaddress->getPostcode();     $shippingtelephone = $shippingaddress->getTelephone();  $shippingstate_code = $shippingaddress->getRegionCode();  $grandTotal = $order->getGrandTotal();  $subTotal = $order->getSubtotal();  // fetch specific payment information  $amount = $order->getPayment()->getAmountPaid();  $paymentMethod = $order->getPayment()->getMethod();  $info = $order->getPayment()->getAdditionalInformation('method_title');  // Get Order Items  $orderItems = $order->getAllItems();  foreach ($orderItems as $item) {   $item->getItemId();   $item->getOrderId();   $item->getStoreId();   $item->getProductId();   print_r($item->getProductOptions());   $item->getSku();   $item->getName();   $item->getQtyOrdered();   $item->getPrice();  } |
You may want to bookmark this post for future reference!
If you have any doubts about the implementation of the above method, do not hesitate to use the Comments section below. I’d be happy to help.
I’d be very grateful if you help share this helpful post on social media to fellow developers!
Thanks!
Still need help? Hire our Adobe-certified Magento experts.
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.
6 Comments
Hi Sanjay,
I love your tutorials.
I have a scenarios here. I have added an offline payment gateway in magento2 which will show after the order is above $2000. I want to populate the order id, product details, product aty and shipping address after placing the order into wordpress gravity form. I am able to redirect the user to the gravity form page but I want to take the values of the order I have mentioned above with the URL is it possible?
Any help is appreciated.
Hey, please Refer our Blog
https://meetanshi.com/blog/get-order-data-from-magento-2-sales_order_place_after-event/
Get Values of the order using this blog and send it to your Gravity Form.
Great article, would I be able to get the categories and subcategories by product?
Hello Caetano,
Thanks for the appreciation!
I will post the solution in the future.
Do subscribe to Meetanshi’s blog posts to get notified for the same.
Thank You.
How to get transaction id on success page?
Hello Jitendra,
The transaction ID is saved in additional information which you can get using:
$order->getPayment()->getAdditionalInformation()
Thanks