0

How would I get a order's info from a quote id?

if I have quote id of 253 and once I load that quote using..

$quote = $this->quoteFactory->create()->load("253");

How do i get the order which belongs to this?

This would happen after the order has been placed

I've tried this but it doesn't work.

$reserved_order_id = (int)$quote['reserved_order_id']); //01191
$order = $this->orderRepository->get($reserved_order_id);

however i get an error of The entity that was requested doesn't exist. Verify the entity and try again yet i know that order exists. perhaps i shouldn't use $quote['reserved_order_id']

Update

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $collection = $objectManager->create('Magento\Sales\Model\Order');
        $orderInfo = $collection->loadByIncrementId($order_id);
        $orderId = $orderInfo ->getId();
        $order = $this->orderRepository->get($orderId);

1 Answer 1

2

Try below code.

In Magento 2, you can use the \Magento\Sales\Api\OrderRepositoryInterface class to retrieve an order using a quote ID. Here's an example of how you can use this class to load an order by quote ID:

<?php

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

// Quote ID
$quoteId = 1;

// Load order by quote ID
$orderRepository = $obj->create('\Magento\Sales\Api\OrderRepositoryInterface');
$order = $orderRepository->getByQuoteId($quoteId);

// Use the order object
echo $order->getIncrementId();

You can also use \Magento\Quote\Model\QuoteFactory class to load the quote and then use getId() method to get the order Id.

<?php
use Magento\Quote\Model\QuoteFactory;

$quoteFactory = $this->quoteFactory->create();
$quote = $quoteFactory->load($quoteId);
$orderId = $quote->getReservedOrderId();

Let me know if any query.

THANKS.

11
  • hi, $orderId is not found -> error: The entity that was requested doesn't exist. Verify the entity and try again.
    – X3R0
    Jan 18, 2023 at 13:40
  • 1
    Your error said you quoteId not exits. So can you try other quoteId and check. Jan 18, 2023 at 13:50
  • 1
    Yes, but without order id in DB as per magento standard it throw error which you facing. Jan 18, 2023 at 14:22
  • 1
    Great bro. Thanks for accepting answer. Jan 18, 2023 at 14:33
  • 1
    Welcome brother Jan 18, 2023 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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