Monday, July 4, 2022
HomeMagentoMagento 2 get Order details by Increment Id programmatically

Magento 2 get Order details by Increment Id programmatically

In this article, you going to see how to get Magento 2 get order details by increment Id programmatically in a detail and a step by step.

If you are looking for this topic then you are at a right place.

Lets start with the topic Magento 2 get order details programmatically.

By using Increment Id, you can get order data.

To fetch the order data using increment id, you have to use function called getList() function.

The get list function can be used by using interface as below.

Magento\Sales\Api\OrderRepositoryInterface interface

Use the below code snippet to fetch the order data using increment Id.

<?php
namespace VendorName\ModuleName\PathtoClass;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;

class OrderData
{
    /**
     * @var SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
    * @var OrderRepositoryInterface
    */
    private $orderRepository;

    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(
        SearchCriteriaBuilder $searchCriteriaBuilder,
        OrderRepositoryInterface $orderRepository,
        LoggerInterface $logger
    ) {
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->orderRepository = $orderRepository;
        $this->logger = $logger;
    }

    /**
     * Get Order data by Order Increment Id
     *
     * @param $incrementId
     * @return \Magento\Sales\Api\Data\OrderInterface[]|null
     */
    public function getOrderIdByIncrementId($incrementId)
    {
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('increment_id', $incrementId)->create();
        $orderData = null;
        try {
            $order = $this->orderRepository->getList($searchCriteria);
            if ($order->getTotalCount()) {
                $orderData = $order->getItems();
            }
        } catch (Exception $exception)  {
            $this->logger->critical($exception->getMessage());
        }
        return $orderData;
    }
}

Below is code snippet for calling a function using increment id to get order data.

$orderIncrementId = 000000001; // order increment_id
$order = $this->getOrderIdByIncrementId($orderIncrementId);

foreach ($order as $orderData) {
    $orderId = (int)$orderData->getId();
    var_dump($orderData);
}

At the end, you will get the order Object for the var_dump.

After adding the code for get order data, execute the below command to get out put.

Magento 2 media image attribute not showing

Conclusion :

Finally, we have done with the topic completely.

With the help of above code snippet one can do operation for Magento 2 get order details by increment id programmatically.

I hope you like this article and if you have any query, please comment below.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular