Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreateLabel.php
Go to the documentation of this file.
1 <?php
8 
10 
12 {
18  const ADMIN_RESOURCE = 'Magento_Sales::shipment';
19 
23  protected $shipmentLoader;
24 
28  protected $labelGenerator;
29 
35  public function __construct(
36  Action\Context $context,
37  \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader,
38  \Magento\Shipping\Model\Shipping\LabelGenerator $labelGenerator
39  ) {
40  $this->shipmentLoader = $shipmentLoader;
41  $this->labelGenerator = $labelGenerator;
42  parent::__construct($context);
43  }
44 
50  public function execute()
51  {
52  $response = new \Magento\Framework\DataObject();
53  try {
54  $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id'));
55  $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id'));
56  $this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment'));
57  $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
58  $shipment = $this->shipmentLoader->load();
59  $this->labelGenerator->create($shipment, $this->_request);
60  $shipment->save();
61  $this->messageManager->addSuccess(__('You created the shipping label.'));
62  $response->setOk(true);
63  } catch (\Magento\Framework\Exception\LocalizedException $e) {
64  $response->setError(true);
65  $response->setMessage($e->getMessage());
66  } catch (\Exception $e) {
67  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
68  $response->setError(true);
69  $response->setMessage(__('An error occurred while creating shipping label.'));
70  }
71 
72  $this->getResponse()->representJson($response->toJson());
73  }
74 }
$response
Definition: 404.php:11
__()
Definition: __.php:13
__construct(Action\Context $context, \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader, \Magento\Shipping\Model\Shipping\LabelGenerator $labelGenerator)
Definition: CreateLabel.php:35
foreach($order->getItems() as $orderItem) $shipment