Requested entity doesn\'t exist in Magento 2
-
10-05-2021 - |
Pergunta
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.
I tried 2 ways (API and Model Class) but returning the same error.
Any help on this.
Solução
\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);
}
Outras dicas
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.
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
.
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);
}