Magento 2 Get Order Details Programmatically– It is very common while working with orders in Magento 2 we need to deal with the associated details about the order. Here in this article we are going to explain how you can get the important details such as- Load Order by Id(Increment Id), get Order Status, Get order Shipping Address, Billing Address, Shipping And Payment Method Details.
Magento 2 Get Order Details Programmatically | Payment Method | Shipping | Example
Let us go step by step to understand the Order Details in Magento 2-
- 1. Load Order By Id
- 2. Load Order By Increment Id
First of all let us load order object
Load Order By Id
You can load order by order id using object manager simply as below-
Load Order By Order Id Example:
$orderId = 9999; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId); echo $order->getGrandTotal(); |
Load Order By Increment Id
Now let us load order by increment id in Magento 2-
Load Order By Increment Id Example:
$$orderIncrementId = 109834772; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\Order') ->loadByIncrementId($orderIncrementId); echo $order->getGrandTotal(); |
Get Order Status & State In Magento 2
You can get order status & state from Order Id simply as below-
Get Order Status & Label From Order Id Example:
$orderId = 9999; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId); echo $state = $order->getState(); echo "<br/>"; echo $orderStatus = $order->getStatusLabel(); |
Get Order Items Collection
Now let us get order items collection All Visible Items, All Items from order object-
Get Order items | All Items | All Visible Example:
$orderId = 9999; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId); $orderItemsCollection = $order->getAllVisibleItems(); //*** get All Items ******// //$orderItemsCollection = $order->getAllItems(); foreach $orderItemsCollection as $item){ $productId = $item->Id(); $productSku = $item->sku(); $productName = $item->getName(); } |
Get Shipping & Billing Address From Order
You can get the shipping & Billing Address Details form order using order id simply as below-
Shipping & Billing Info From Order Example:
$orderId = 9999; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId); $shippingAddress = $order->getShippingAddress(); $billingAddress = $order->getBillingAddress(); echo $shippingAddress->getStreet(); echo $shippingAddress->getTelephone(); |
Get Shipping Method
You can get the shipping Method form order using order id simply as below-
Shipping Method Info from Order Example:
$orderId = 9999; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId); $shippingMethod = $order->getShippingMethod(); |
Get Payment Method
You can get the Payment Method form order using order id simply as below-
Payment Method Info from Order Example:
$orderId = 9999; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId); $paymentMethod = $order->getPaymentMethod(); |
Get Quote From Order
You can get the quote From Order using quote Id.
Payment Method Info from Order Example:
$orderId = 9999; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId); $quoteId = $order->getQuoteId(); $quoteFactory = $objectManager->create('\Magento\Quote\Model\QuoteFactory'); $quote = $quoteFactory->create()->load($quoteId); echo $quote->getSubTotal(); |
Get Shipment Collection From Order
You can get shipment collection from order object simply as below-
Get Shipment Id By Order Id Example:
$orderId = 9999; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId); $shipmentCollection = $order->getShipmentsCollection(); print_r($shipmentCollection->getData()); // or foreach($shipmentCollection as $shipment){ $shipmentID = $shipment->getId(); $shipmentIncrementID = $shipment->getIncrementId(); } |
There can be multiple shipments against an order so you can get shipment id, shipment increment id and other details as in above example.