This article shows how you can get order information by loading the order by order id or order increment id.
We will load the order by order id and also with order increment id.
Then, we fetch the following information:
– Order details
– Order Items Information
– Payment information of the Order
– Billing Address of the Order
– Shipping Address of the Order
I will be showing how you can fetch the order information by both ways, i.e.
– Using Dependency Injection (DI)
– Using Object Manager
Using Dependency Injection (DI)
In this example, I have used a custom module named Chapagain_HelloWorld.
Here’s the Block class:
<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
/**
* @var \Magento\Sales\Model\OrderRepository
*/
protected $orderRepository;
/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;
public function __construct(
\Magento\Sales\Model\OrderRepository $orderRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Backend\Block\Template\Context $context,
array $data = []
)
{
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
parent::__construct($context, $data);
}
public function getOrderById($id) {
return $this->orderRepository->get($id);
}
public function getOrderByIncrementId($incrementId) {
$this->searchCriteriaBuilder->addFilter('increment_id', $incrementId);
$order = $this->orderRepository
->getList($this->searchCriteriaBuilder->create())
->getItems();
return $order;
}
}
?>
Here’s the template file code:
<?php
$incrementId = '000000051';
$order = $block->getOrderByIncrementId($incrementId);
//var_dump($order);
foreach ($order as $key => $value) {
var_dump($value->getData());
}
$orderId = '47';
$order = $block->getOrderById($orderId);
//var_dump($order->getData());
// Get Order Information
echo $order->getEntityId() . '<br>';
echo $order->getIncrementId() . '<br>';
echo $order->getState() . '<br>';
echo $order->getStatus() . '<br>';
echo $order->getStoreId() . '<br>';
echo $order->getGrandTotal() . '<br>';
echo $order->getSubtotal() . '<br>';
echo $order->getTotalQtyOrdered() . '<br>';
echo $order->getOrderCurrencyCode() . '<br>';
// Get Customer Information from the Order
echo $order->getCustomerId() . '<br>';
echo $order->getCustomerEmail() . '<br>';
echo $order->getCustomerFirstname() . '<br>';
echo $order->getCustomerLastname() . '<br>';
// Get Order Items
$orderItems = $order->getAllItems();
foreach ($orderItems as $item) {
//var_dump($item->getData());
echo $item->getItemId() . '<br>';
echo $item->getOrderId() . '<br>';
echo $item->getStoreId() . '<br>';
echo $item->getProductId() . '<br>';
print_r($item->getProductOptions()) . '<br>';
echo $item->getSku() . '<br>';
echo $item->getName() . '<br>';
echo $item->getQtyOrdered() . '<br>';
echo $item->getPrice() . '<br>';
}
// Get Order Payment
$payment = $order->getPayment();
//var_dump($payment->getData());
echo $payment->getMethod() . '<br>'; // payment method code
echo $payment->getAmountPaid() . '<br>';
echo $payment->getAmountOrdered() . '<br>';
echo $payment->getAdditionalInformation()['method_title'] . '<br>';
// Get Billing Information
$billingAddress = $order->getBillingAddress();
//var_dump($billingAddress->getData());
echo $billingAddress->getFirstname() . '<br>';
echo $billingAddress->getLastname() . '<br>';
echo $billingAddress->getEmail() . '<br>';
echo $billingAddress->getTelephone() . '<br>';
print_r($billingAddress->getStreet()) . '<br>';
echo $billingAddress->getCity() . '<br>';
echo $billingAddress->getRegion() . '<br>';
echo $billingAddress->getCountryId() . '<br>';
// Get Shipping Information
$shippingAddress = $order->getShippingAddress();
//var_dump($shippingAddress->getData());
echo $shippingAddress->getFirstname() . '<br>';
echo $shippingAddress->getLastname() . '<br>';
echo $shippingAddress->getEmail() . '<br>';
echo $shippingAddress->getTelephone() . '<br>';
print_r($shippingAddress->getStreet()) . '<br>';
echo $shippingAddress->getCity() . '<br>';
echo $shippingAddress->getRegion() . '<br>';
echo $shippingAddress->getCountryId() . '<br>';
?>
Using Object Manager
The above code of using Dependency Injection (DI) is the recommended way. If you ever need to use Object Manager instead, then you can try the following code to fetch the order information by loading the order by order ID or increment ID.
<?php
$incrementId = '000000051';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderRepository = $objectManager->create('\Magento\Sales\Model\OrderRepository');
$searchCriteriaBuilder = $objectManager->create('\Magento\Framework\Api\SearchCriteriaBuilder');
$searchCriteriaBuilder->addFilter('increment_id', $incrementId);
$order = $orderRepository->getList($searchCriteriaBuilder->create())->getItems();
//var_dump($order);
foreach ($order as $key => $value) {
var_dump($value->getData());
}
$orderId = '47';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);
//var_dump($order->getData());
// Get Order Information
echo $order->getEntityId() . '<br>';
echo $order->getIncrementId() . '<br>';
echo $order->getState() . '<br>';
echo $order->getStatus() . '<br>';
echo $order->getStoreId() . '<br>';
echo $order->getGrandTotal() . '<br>';
echo $order->getSubtotal() . '<br>';
echo $order->getTotalQtyOrdered() . '<br>';
echo $order->getOrderCurrencyCode() . '<br>';
// Get Customer Information from the Order
echo $order->getCustomerId() . '<br>';
echo $order->getCustomerEmail() . '<br>';
echo $order->getCustomerFirstname() . '<br>';
echo $order->getCustomerLastname() . '<br>';
// Get Order Items
$orderItems = $order->getAllItems();
foreach ($orderItems as $item) {
//var_dump($item->getData());
echo $item->getItemId() . '<br>';
echo $item->getOrderId() . '<br>';
echo $item->getStoreId() . '<br>';
echo $item->getProductId() . '<br>';
print_r($item->getProductOptions()) . '<br>';
echo $item->getSku() . '<br>';
echo $item->getName() . '<br>';
echo $item->getQtyOrdered() . '<br>';
echo $item->getPrice() . '<br>';
}
// Get Order Payment
$payment = $order->getPayment();
//var_dump($payment->getData());
echo $payment->getMethod() . '<br>'; // payment method code
echo $payment->getAmountPaid() . '<br>';
echo $payment->getAmountOrdered() . '<br>';
echo $payment->getAdditionalInformation()['method_title'] . '<br>';
// Get Billing Information
$billingAddress = $order->getBillingAddress();
//var_dump($billingAddress->getData());
echo $billingAddress->getFirstname() . '<br>';
echo $billingAddress->getLastname() . '<br>';
echo $billingAddress->getEmail() . '<br>';
echo $billingAddress->getTelephone() . '<br>';
print_r($billingAddress->getStreet()) . '<br>';
echo $billingAddress->getCity() . '<br>';
echo $billingAddress->getRegion() . '<br>';
echo $billingAddress->getCountryId() . '<br>';
// Get Shipping Information
$shippingAddress = $order->getShippingAddress();
//var_dump($shippingAddress->getData());
echo $shippingAddress->getFirstname() . '<br>';
echo $shippingAddress->getLastname() . '<br>';
echo $shippingAddress->getEmail() . '<br>';
echo $shippingAddress->getTelephone() . '<br>';
print_r($shippingAddress->getStreet()) . '<br>';
echo $shippingAddress->getCity() . '<br>';
echo $shippingAddress->getRegion() . '<br>';
echo $shippingAddress->getCountryId() . '<br>';
?>
Hope this helps. Thanks.