How to get order data by order increment id programmatically Magento 2?

You can get the Order data by order increment id in Magento 2.

Using the Magento\Sales\Api\OrderRepositoryInterface interface, you need to use the getList() function to fetch order data by order increment id.

Use below code snippet to fetch order data by order increment id in Magento 2

<?php
namespace Path\To\Class;

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
     */
    private $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;
    }
}

Call the function with order Increment id

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

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

You got the Order object for the var_dump in the above output.

How to get order data by order id programmatically in magento 2?

Magento  2 Fetch order details by Order ID. With the help of Magento\Sales\Api\OrderRepositoryInterface, You can retrieve order-related data programmatically.

Order Entity generated after the Customer Placed Order successfully from the Storefront or Admin Can Place an order from the Magento Back Panel.

Get Order Data By Order Id to retrieve order information using the OrderRepositoryInterface way. Continue reading “How to get order data by order id programmatically in magento 2?”

How to Get Payment Method Title/Code from order in Magento 2?

You can get the payment method title from the Order in Magento 2 by the order repository interface.

Load an Order by API OrderRepositoryInterface with Order ID, Get Payment Object, and fetch Payment-related stuff from the Payment Data.

You can get the payment method title of an order by below code snippets below,

<?php
namespace Rbj\Order\Model;

use Magento\Sales\Api\OrderRepositoryInterface;

class Data
{
    public function __construct(
        private readonly OrderRepositoryInterface $orderRepository
    ) {
    }

    public function getPaymentData()
    {
        $orderId = 1;
        $order = $this->orderRepository->get($orderId);
        $payment = $order->getPayment();
        $method = $payment->getMethodInstance();
        echo $method->getTitle(); // Cash On Delivery
        echo $method->getCode(); // cashondelivery
    }

}

Using the above method you can get any payment method title or code.