Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PrintInvoice.php
Go to the documentation of this file.
1 <?php
8 
11 
13 {
18 
22  protected $_coreRegistry;
23 
27  protected $resultPageFactory;
28 
35  public function __construct(
36  Context $context,
38  \Magento\Framework\Registry $registry,
40  ) {
41  $this->orderAuthorization = $orderAuthorization;
42  $this->_coreRegistry = $registry;
43  $this->resultPageFactory = $resultPageFactory;
44  parent::__construct($context);
45  }
46 
52  public function execute()
53  {
54  $invoiceId = (int)$this->getRequest()->getParam('invoice_id');
55  if ($invoiceId) {
56  $invoice = $this->_objectManager->create(
57  \Magento\Sales\Api\InvoiceRepositoryInterface::class
58  )->get($invoiceId);
59  $order = $invoice->getOrder();
60  } else {
61  $orderId = (int)$this->getRequest()->getParam('order_id');
62  $order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
63  }
64 
65  if ($this->orderAuthorization->canView($order)) {
66  $this->_coreRegistry->register('current_order', $order);
67  if (isset($invoice)) {
68  $this->_coreRegistry->register('current_invoice', $invoice);
69  }
71  $resultPage = $this->resultPageFactory->create();
72  $resultPage->addHandle('print');
73  return $resultPage;
74  } else {
76  $resultRedirect = $this->resultRedirectFactory->create();
77  if ($this->_objectManager->get(\Magento\Customer\Model\Session::class)->isLoggedIn()) {
78  $resultRedirect->setPath('*/*/history');
79  } else {
80  $resultRedirect->setPath('sales/guest/form');
81  }
82  return $resultRedirect;
83  }
84  }
85 }
$order
Definition: order.php:55
$invoice
__construct(Context $context, OrderViewAuthorizationInterface $orderAuthorization, \Magento\Framework\Registry $registry, PageFactory $resultPageFactory)