diff --git a/app/code/Magento/Sales/Controller/AbstractController/PrintInvoice.php b/app/code/Magento/Sales/Controller/AbstractController/PrintInvoice.php index d06f0cc87360e..74eef9d9ada11 100644 --- a/app/code/Magento/Sales/Controller/AbstractController/PrintInvoice.php +++ b/app/code/Magento/Sales/Controller/AbstractController/PrintInvoice.php @@ -1,8 +1,8 @@ getRequest()->getParam('invoice_id'); if ($invoiceId) { - $invoice = $this->_objectManager->create( - \Magento\Sales\Api\InvoiceRepositoryInterface::class - )->get($invoiceId); + try { + $invoice = $this->_objectManager->create( + \Magento\Sales\Api\InvoiceRepositoryInterface::class + )->get($invoiceId); + } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { + $this->messageManager->addError(__($e->getMessage())); + /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ + $resultRedirect = $this->resultRedirectFactory->create(); + if ($this->_objectManager->get(\Magento\Customer\Model\Session::class)->isLoggedIn()) { + $resultRedirect->setPath('*/*/history'); + } else { + $resultRedirect->setPath('sales/guest/form'); + } + return $resultRedirect; + } $order = $invoice->getOrder(); } else { $orderId = (int)$this->getRequest()->getParam('order_id'); diff --git a/app/code/Magento/Sales/Controller/AbstractController/PrintShipment.php b/app/code/Magento/Sales/Controller/AbstractController/PrintShipment.php index 6d79e6ef6b323..90bf9a4c5f231 100644 --- a/app/code/Magento/Sales/Controller/AbstractController/PrintShipment.php +++ b/app/code/Magento/Sales/Controller/AbstractController/PrintShipment.php @@ -1,8 +1,8 @@ getRequest()->getParam('shipment_id'); if ($shipmentId) { - $shipment = $this->_objectManager->create(\Magento\Sales\Model\Order\Shipment::class)->load($shipmentId); + try { + $shipment = $this->_objectManager + ->create(\Magento\Sales\Model\Order\Shipment::class) + ->load($shipmentId); + } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { + /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ + $resultRedirect = $this->resultRedirectFactory->create(); + if ($this->_objectManager->get(\Magento\Customer\Model\Session::class)->isLoggedIn()) { + $resultRedirect->setPath('*/*/history'); + } else { + $resultRedirect->setPath('sales/guest/form'); + } + return $resultRedirect; + } $order = $shipment->getOrder(); } else { $orderId = (int)$this->getRequest()->getParam('order_id');