Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassPrintShippingLabel.php
Go to the documentation of this file.
1 <?php
8 
17 use Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory as ShipmentCollectionFactory;
20 
24 class MassPrintShippingLabel extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
25 {
31  const ADMIN_RESOURCE = 'Magento_Sales::shipment';
32 
36  protected $labelGenerator;
37 
41  protected $fileFactory;
42 
46  protected $collectionFactory;
47 
52 
61  public function __construct(
62  Context $context,
67  ShipmentCollectionFactory $shipmentCollectionFactory
68  ) {
69  $this->fileFactory = $fileFactory;
70  $this->collectionFactory = $collectionFactory;
71  $this->shipmentCollectionFactory = $shipmentCollectionFactory;
72  $this->labelGenerator = $labelGenerator;
73  parent::__construct($context, $filter);
74  }
75 
83  protected function massAction(AbstractCollection $collection)
84  {
85  $labelsContent = [];
86  $shipments = $this->shipmentCollectionFactory->create()->setOrderFilter(['in' => $collection->getAllIds()]);
87 
88  if ($shipments->getSize()) {
90  foreach ($shipments as $shipment) {
91  $labelContent = $shipment->getShippingLabel();
92  if ($labelContent) {
93  $labelsContent[] = $labelContent;
94  }
95  }
96  }
97 
98  if (!empty($labelsContent)) {
99  $outputPdf = $this->labelGenerator->combineLabelsPdf($labelsContent);
100  return $this->fileFactory->create(
101  'ShippingLabels.pdf',
102  $outputPdf->render(),
104  'application/pdf'
105  );
106  }
107 
108  $this->messageManager->addError(__('There are no shipping labels related to selected orders.'));
109  return $this->resultRedirectFactory->create()->setPath('sales/order/');
110  }
111 }
__construct(Context $context, Filter $filter, CollectionFactory $collectionFactory, FileFactory $fileFactory, LabelGenerator $labelGenerator, ShipmentCollectionFactory $shipmentCollectionFactory)
__()
Definition: __.php:13
$shipments
foreach($order->getItems() as $orderItem) $shipment