diff --git a/app/code/Magento/Sales/Controller/AbstractController/PrintInvoice.php b/app/code/Magento/Sales/Controller/AbstractController/PrintInvoice.php index d06f0cc87360e..fbcc80e626337 100644 --- a/app/code/Magento/Sales/Controller/AbstractController/PrintInvoice.php +++ b/app/code/Magento/Sales/Controller/AbstractController/PrintInvoice.php @@ -53,9 +53,21 @@ public function execute() { $invoiceId = (int)$this->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..ed9df9f802584 100644 --- a/app/code/Magento/Sales/Controller/AbstractController/PrintShipment.php +++ b/app/code/Magento/Sales/Controller/AbstractController/PrintShipment.php @@ -53,7 +53,18 @@ public function execute() { $shipmentId = (int)$this->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');