0

How to Get Order Data by Order ID Programmatically in Magento 2

Share
0 0
Read Time:2 Minute, 2 Second

In Magento 2, it is recommended to use dependency injection instead of directly using the object manager. Here is an example of how to get order data by order ID programmatically in Magento 2

1- By using the dependency injection:

<?php
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objManager = $bootstrap->getObjectManager();

$orderId = 100;

/** @var OrderRepositoryInterface $orderRepository */
$orderRepository = $objManager->get(OrderRepositoryInterface::class);

/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = $objManager->get(SearchCriteriaBuilder::class);
$searchCriteria = $searchCriteriaBuilder->addFilter('entity_id', $orderId)->create();

$orders = $orderRepository->getList($searchCriteria)->getItems();
$orderData = reset($orders)->getData();

print_r($orderData);

We set the Order ID that we want to load and use the OrderRepositoryInterface to load the order data. We create a search criteria to filter by the Order ID, and then use the getList method of the OrderRepositoryInterface to get a list of orders that match the search criteria. Since we are searching by Order ID, we should only have one order in the list. We use the reset function to get the first (and only) order in the list and get its data using the getData method.

In this code, we first create an instance of the Bootstrap class to load the Magento environment. Then we create an instance of the object manager to get the OrderRepositoryInterface and SearchCriteriaBuilder.

2- By using object manager : to get order data by Order ID programmatically in Magento 2, you can use the following code snippet:

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();

// Set Order Id
$orderId = 100;

// Load Order by Id
$order = $obj->create('Magento\Sales\Model\Order')->load($orderId);

// Get Order Data
$orderData = $order->getData();

// Print Order Data
print_r($orderData);

?>

In this code, you first create an instance of the Bootstrap class to load the Magento environment. Then you create an instance of the Object Manager and set the Order ID that you want to load.

Next, you load the Order by ID using the load method of the Magento\Sales\Model\Order class. Once you have the Order object, you can get its data using the getData method.

Finally, you can print the order data using the print_r function or process it as per your requirements.

About Post Author

Aqeel Hussein

Hussein is a skilled tech author/blogger with 3 years of experience, specializing in writing captivating content on a wide range of tech topics. With a passion for technology and a knack for engaging writing, Aqeel provides valuable insights and information to tech enthusiasts through his blog. Also Aqeel has PhD. in Adaptive eLearning Systems & M.S.C Software Engineer. he worked as Web Developer - PHP Developer - Associate Software engineer (Magento developer)
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %