4

I am trying to get the order information in my custom module from the front store, it is saying Requested entity doesn\'t exist.

But I have an order and order ID: 000000013.

/**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    protected $orderRepository;


    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        array $data = []
    )
    {
        $this->orderRepository = $orderRepository;
        parent::__construct($context, $data);
    }


    public function getOrder($orderId,$email){

        //$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
       // $orderInformation = $objectManager->create('Magento\Sales\Model\Order')->load($orderId);

       try{
           $order = $this->orderRepository->get($orderId);

           if(trim($order->getCustomerEmail()) == trim($email)){
               return $order;
           }else{
               return $order;
           }
       }catch (\Exception $e){
            return $order;
       }

    }

Even though I debugged see below pic.

enter image description here

I tried 2 ways (API and Model Class) but returning the same error.

Any help on this.

3
  • Have you debug, got value for $order->getCustomerEmail()? Commented Jan 12, 2017 at 11:44
  • @Rakesh, Yes I tried debugging, there is no value.
    – Bojjaiah
    Commented Jan 12, 2017 at 11:47
  • @Rakesh see updated my thread with a screenshot which is the debugging image.
    – Bojjaiah
    Commented Jan 12, 2017 at 11:52

5 Answers 5

8

\Magento\Sales\Api\OrderRepositoryInterface::get receive order id, not increment_id. Use getList, if you need to search order by increment_id

/**
 * @param OrderRepositoryInterface $orderRepository
 * @param SearchCriteriaBuilder $searchCriteriaBuilder
 */
public function __construct(
    OrderRepositoryInterface $orderRepository,
    SearchCriteriaBuilder $searchCriteriaBuilder
) {
    $this->orderRepository = $orderRepository;
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
 * Get order by increment id.
 *
 * @param string $incrementId
 * @return OrderInterface
 * @throws NoSuchEntityException
 */
public function getOrderByIncrementId($incrementId)
{
    $searchCriteria = $this->searchCriteriaBuilder->addFilter(
        OrderInterface::INCREMENT_ID,
        $incrementId
    )->create();

    $result = $this->orderRepository->getList($searchCriteria);

    if (empty($result->getItems())) {
        throw new NoSuchEntityException(__('No such order.'));
    }

    $orders = $result->getItems();

    return reset($orders);
}
3
  • let's try and come back you
    – Bojjaiah
    Commented Jan 12, 2017 at 12:09
  • Can you give me a example?
    – Bojjaiah
    Commented Jan 12, 2017 at 12:16
  • i added source code example to answer
    – Max
    Commented Jan 12, 2017 at 12:33
10

I had the same error showing on Admin > Sales > Orders when trying to access a list of all orders.

It turned out to be a discrepancy between two tables sales_order and sales_order_grid. Some records in sales_order_grid didn't have corresponding records in sales_order table. Luckily, it was my dev environment so I could easily remove offending records. So if you get this error (Requested entity doesn't exist) it's most likely a data integrity issue.

3
  • 1
    This was my issue. I had one record in sales_order_grid that didn't exist in sales_order. Removed the record and the error was gone (this was in my dev env) Commented Aug 25, 2020 at 16:07
  • That was the solution for me. I naively deleted some rows from sales_order to test an unrelated feature. Removing the same lines from sales_order_grid fixed it
    – Badong
    Commented Nov 3, 2020 at 10:17
  • are there anyway to find that order id?
    – huykon225
    Commented Apr 29, 2021 at 7:05
1

I had similar issue but noticed that parameter for get method in $this->orderRepository->get($orderId); needs to be entity_id field from sales_order not the increment_id.

1
         try
         {
          $orderId =   preg_replace('/000+/','',$this->getRequest()->getPost('order_id'));
          
          $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          $order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);
        
        
     

        
                 if($order->getEntityId()==$orderId)
                      {
                          $order_id= " # " . $this->getRequest()->getPost('order_id');
                      }
           header('Content-Type: application/json');
           
            $json_result = array('heading'=>'ORDER TRACKING INFORMATION','span_order_id_text'=>'Order ID','order_id'=>$order_id,'span_order_date_text'=>'Order Date','order_date'=>$order->getCreatedAt(),'span_order_status_text'=>'Order Status','order_status'=>$order->getStatus(),'status'=>'SUCCESS');
           echo json_encode($json_result);  
          
         
        
         }
        catch (\Magento\Framework\Exception\NoSuchEntityException $e){
             header('Content-Type: application/json');
              $json_result = array('heading'=>'No Such Order,Try Again Later...','status'=>'ERROR');
           echo json_encode($json_result);
         }
      
0

Execute this in your database if it's not in the production environment.

DELETE FROM sales_order_grid
WHERE entity_id NOT IN (SELECT entity_id FROM sales_order);

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.