Warning: code samples below are provided as examples only and require further customization according to your business needs.
When you are trying to get Magento 2 order collection programmatically, you may meet advice to use an object manager. But actually, Magento prohibits the direct use of the ObjectManager in your code and has strict rules on how to use this service class. We highly recommend familiarizing yourself with them because you need to avoid objectManager when you are trying to get order content. Instead, use dependency injection.
Helpful tip: Display order details on the order grid with the Extended Order Grid extension. Add new columns and filters in just one click, show customer, product, shipping, or billing attributes, and organize the grid most effectively with the drag-and-drop.
To get Magento 2 order collection by following the best coding practices, create the code like this:
private $order;
public function __construct(
...
\Magento\Sales\Api\Data\OrderInterface $order,
...
) {
$this->order = $order;
}
public function getOrderId()
{
$order = $this->order->loadByIncrementId('100005363');
return $order->getId();
}
This code will help you get order collection by order ID in Magento 2 store without using the object manager. You can use this solution in different scenarios like restrict order based on the delivery address, offer a specific discount based on the order amount, provide users with a free product when a customer buys a particular item, etc.
For more detailed information about dependency injection and how to use it, read the official Magento documentation.
See related articles:
We would like to inform you that our Privacy Policy has been amended.Please, read and accept the new terms.
We use cookies to help improve our services, make personal offers, and enhance your experience. If you do not accept optional cookies below, your experience may be affected. If you want to know more, please read the Cookie Policy
Please select and accept your Cookies Group
Necessary cookies enable core functionality of the website. Without these cookies the website can not function properly. They help to make a website usable by enabling basic functionality.
Marketing cookies are used to track and collect visitors actions on the website. Cookies store user data and behaviour information, which allows advertising services to target more audience groups. Also more customized user experience can be provided according to collected information.
A set of cookies to collect information and report about website usage statistics without personally identifying individual visitors to Google.
Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.
Cookie Name | Cookie Provider | Cookie Description | Cookie Lifetime | Cookie Type |
---|---|---|---|---|
Login and Registration Form