Sunday, November 7, 2021


We will see how to fetch order information such as order items, payment, customer, billing, and shipping details from the order id. You can get the order id at the checkout success page from the checkout session object.


Magento 2 recommended using Repository to get the entity data.

<?php
Class Getsolution {
 
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();   
}
}

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(); 
     
}
?>
Newer Post
Previous
This is the last post.