Get Order Information From Order ID in Magento 2 (Updated 2020)

order_information_from_order_id_magento_2

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.

<?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.

Get Order Information From Order ID

<?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

<?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(); 
    
}
?>
Web Hosting


Get Order Payment Information

<?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

<?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

<?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();

?>

If you liked this post, then please like us on Facebook and follow us on Twitter.

Want to ask a question or leave a comment?

Leave a Comment

(18 Comments)

  • Carlos

    Hi, I need code gift card of the Order Can I help me?

  • nou

    find this complex, please explain more further about where those files should be placed, called, etc, etc…

  • Saurabh

    How can we get the item image in order of data

    • Chirag

      You first need to get the product id $_item->getProductId(); and then you need to load product to get its image

  • Zack

    How to Load all Orders for Specified Customer’s billing address Phone Number

    • Chirag

      You have to do join between sales_order and sales_order_address table and filter out data by customer_id and telephone

  • Leonardo

    How can I get telephone number?
    Thanks

    • Chirag

      You can get telephone using below code.

      $order->getShippingAddress()->getTelephone();
      
  • Savvas Radevic

    get(‘Magento\Framework\App\ResourceConnection’);
    //$connection = $resource->getConnection();
    $orderCollection = $objectManager->create(‘Magento\Sales\Model\Order’)->getCollection();

    //var_dump($orderCollection->getData());
    $jsonData = json_encode($orderCollection->getData());

    echo $jsonData;

    • Chirag

      What you mean by above code? Can you please explain?

  • Gaurav

    getConnection(‘core_read’);

    /**
    * Retrieve the write connection
    */
    $writeConnection = $resource->getConnection(‘core_write’);

    $categoryId = 14;

    $filteredOrderIds = array();

    $query = ‘SELECT * FROM sales_flat_order_item’;

    $results = $readConnection->fetchAll($query);
    foreach ($results as $orders)
    {
    $orderID = $orders[‘order_id’];
    $prodID = $orders[‘product_id’];
    $product = Mage::getModel(‘catalog/product’);
    $product->setId($prodID);
    $categoryIds = $product->getResource()->getCategoryIds($product);
    if (in_array($categoryId, $categoryIds))
    {
    $filteredOrderIds[] = $orders[‘order_id’];
    // echo “”;print_r($filteredOrderIds);
    }
    // $fileName = ‘example.csv’;
    // header(‘Content-Type: application/excel’);
    // header(‘Content-Disposition: attachment; filename=”‘ . $fileName . ‘”‘);

    // echo “”;print_r($orders);die(); # code…
    }
    $file = fopen(‘php://output’,’w’);
    foreach ($filteredOrderIds as $line)
    {
    fputcsv($file,explode(‘,’,$line));
    // echo “”;print_r($line);
    }

    fclose($file);

    //echo “”;print_r($filteredOrderIds); # code…

    //$filteredOrderIds[] = $orderId;

    ?>

    How to Get Order details in CSV?

  • Rohan

    Very useful information….

  • Ramanathan

    How to get order totals?

    • Chirag

      You can get order totals using below code

      <?php
      echo $order->getGrandTotal();
      echo $order->getSubtotal();
      echo $order->getDiscountAmount();
      
      • Ramanathan

        Okay, But I need to get order totals in one object like $quote->getShippingAddress()->getTotals()

        • Admin

          You can get it in one object like this

          $this->order->create()->load($orderId)->getShippingAddress()->getOrder()->getGrandTotal();
          
  • Vikram

    How to get order comments?

    • Chirag Dodia

      You can get order comments using below code

      $orderid = 2;
      $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
      
       foreach ($order->getStatusHistoryCollection() as $status) {
                  if ($status->getComment()) {
                         $orderComment[]=$status->getComment()
                  }
              }
      
  • All the comments are goes into moderation before approval. Irrelevant comment with links directly goes to spam. Your email address will not be published.

    Was this post helpful? Please support Us!

    Follow us on twitter or Like us on facebook.

     


    To Avoid Spam Downloads, We Want Your Email

    We will send you download link right
    away. Please submit form below.
    SEND ME DOWNLOAD LINK
    Close

    Increase Your
    Magento 2
    Knowledge

    Get Weekly Tutorial
    to your Inbox
    Subscribe Me
    close-link
    Subscribe Here