0

I want to get order id by order increment id. I have this code but it is returning empty value:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$incrId = 100005363;
$collection = $objectManager->create('Magento\Sales\Model\Order'); 
$orderInfo = $collection->loadByIncrementId($incrId);
$orderId = $orderInfo->getOrderId();
echo $orderId;

What is the wrong with code or I am doing some wrong approach?

2

Try below Code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$incrId = 100005363;
$collection = $objectManager->create('Magento\Sales\Model\Order'); 
$orderInfo = $collection->loadByIncrementId($incrId);
$orderId = $orderInfo ->getId();
echo $orderId;  
1

You don't want to get the collection of order instead use the order interface Magento\Sales\Api\Data\OrderInterface to get only one product object

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$incrId = "100005363";
$orderInterface = $objectManager->create('Magento\Sales\Api\Data\OrderInterface'); 
$order = $orderInterface->loadByIncrementId($incrId);
$orderId = $order->getId();
echo $orderId;

Use dependency injection instead of objectManager

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.