Welcome TO magento1 and magento2 store market
Get Order Information From Order ID in Magento 2 (Updated 2020)
In this code snippet, we will see how to fetch order information such as order items, payment, customer, billing, and shipping details from order id. You can get order id at the checkout success page from the checkout session object.
Get Order Information From Order ID using Repository
Magento 2 recommended using Repository to get the entity data. Below is the code snippet to get order information from the order id using the order repository.
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 |
<?php Class Codextblog { protected $orderRepository; public function __construct( \Magento\Sales\Api\OrderRepositoryInterface $orderRepository ){ $this->orderRepository = $orderRepository; } public function MyFunction() { $orderId = 2; $order = $this->orderRepository->get($orderId); echo $order->getIncrementId(); echo $order->getGrandTotal(); echo $order->getSubtotal(); //fetch whole payment information print_r($order->getPayment()->getData()); //fetch customer information echo $order->getCustomerId(); echo $order->getCustomerEmail(); echo $order->getCustomerFirstname(); echo $order->getCustomerLastname(); //fetch whole billing information print_r($order->getBillingAddress()->getData()); //Or fetch specific billing information echo $order->getBillingAddress()->getCity(); echo $order->getBillingAddress()->getRegionId(); echo $order->getBillingAddress()->getCountryId(); //fetch whole shipping information print_r($order->getShippingAddress()->getData()); //Or fetch specific shipping information echo $order->getShippingAddress()->getCity(); echo $order->getShippingAddress()->getRegionId(); echo $order->getShippingAddress()->getCountryId(); } } |
Note: For the demonstrated purpose we have used Objectmanager.Codextblog never recommend the direct use of ObjectManager.One should always use a constructor method to instant an object.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $orderid = 2; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid); //fetch whole order information print_r($order->getData()); //Or fetch specific information echo $order->getIncrementId(); echo $order->getGrandTotal(); echo $order->getSubtotal(); ?> |
Get Order Items Information
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $orderid = 2; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid); //Loop through each item and fetch data foreach ($order->getAllItems() as $item) { //fetch whole item information print_r($item->getData()); //Or fetch specific item information echo $item->getId(); echo $item->getProductType(); echo $item->getQtyOrdered(); echo $item->getPrice(); } ?> |
Get Order Payment Information
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $orderid = 2; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid); //fetch whole payment information print_r($order->getPayment()->getData()); //Or fetch specific payment information echo $order->getPayment()->getAmountPaid(); echo $order->getPayment()->getMethod(); echo $order->getPayment()->getAdditionalInformation('method_title'); ?> |
Get Order Customer Information
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $orderid = 2; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid); //fetch customer information echo $order->getCustomerId(); echo $order->getCustomerEmail(); echo $order->getCustomerFirstname(); echo $order->getCustomerLastname(); ?> |
Get Order Shipping And Billing Information
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php $orderid = 2; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid); //fetch whole billing information print_r($order->getBillingAddress()->getData()); //Or fetch specific billing information echo $order->getBillingAddress()->getCity(); echo $order->getBillingAddress()->getRegionId(); echo $order->getBillingAddress()->getCountryId(); //fetch whole shipping information print_r($order->getShippingAddress()->getData()); //Or fetch specific shipping information echo $order->getShippingAddress()->getCity(); echo $order->getShippingAddress()->getRegionId(); echo $order->getShippingAddress()->getCountryId(); ?> |