3

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.

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

\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);
}
  • let's try and come back you – Bojjaiah Jan 12 '17 at 12:09
  • Can you give me a example? – Bojjaiah Jan 12 '17 at 12:16
  • i added source code example to answer – Max Jan 12 '17 at 12:33
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.

0

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.

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.