Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Info.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Shipping\Model;
8 
10 
12 {
18  protected $_trackingInfo = [];
19 
25  protected $_shippingData;
26 
30  protected $_orderFactory;
31 
36 
40  protected $_trackFactory;
41 
46 
55  public function __construct(
56  \Magento\Shipping\Helper\Data $shippingData,
57  \Magento\Sales\Model\OrderFactory $orderFactory,
59  \Magento\Shipping\Model\Order\TrackFactory $trackFactory,
60  \Magento\Shipping\Model\ResourceModel\Order\Track\CollectionFactory $trackCollectionFactory,
61  array $data = []
62  ) {
63  $this->_shippingData = $shippingData;
64  $this->_orderFactory = $orderFactory;
65  $this->shipmentRepository = $shipmentRepository;
66  $this->_trackFactory = $trackFactory;
67  $this->_trackCollectionFactory = $trackCollectionFactory;
68  parent::__construct($data);
69  }
70 
77  public function loadByHash($hash)
78  {
79  /* @var $helper \Magento\Shipping\Helper\Data */
81  $data = $helper->decodeTrackingHash($hash);
82  if (!empty($data)) {
83  $this->setData($data['key'], $data['id']);
84  $this->setProtectCode($data['hash']);
85 
86  if ($this->getOrderId() > 0) {
87  $this->getTrackingInfoByOrder();
88  } elseif ($this->getShipId() > 0) {
89  $this->getTrackingInfoByShip();
90  } else {
91  $this->getTrackingInfoByTrackId();
92  }
93  }
94  return $this;
95  }
96 
102  public function getTrackingInfo()
103  {
104  return $this->_trackingInfo;
105  }
106 
112  protected function _initOrder()
113  {
115  $order = $this->_orderFactory->create()->load($this->getOrderId());
116 
117  if (!$order->getId() || $this->getProtectCode() !== $order->getProtectCode()) {
118  return false;
119  }
120 
121  return $order;
122  }
123 
129  protected function _initShipment()
130  {
131  /* @var $model Shipment */
132  $ship = $this->shipmentRepository->get($this->getShipId());
133  if (!$ship->getEntityId() || $this->getProtectCode() !== $ship->getProtectCode()) {
134  return false;
135  }
136 
137  return $ship;
138  }
139 
145  public function getTrackingInfoByOrder()
146  {
147  $shipTrack = [];
148  $order = $this->_initOrder();
149  if ($order) {
150  $shipments = $order->getShipmentsCollection();
151  foreach ($shipments as $shipment) {
152  $increment_id = $shipment->getIncrementId();
153  $tracks = $this->_getTracksCollection($shipment);
154 
155  $trackingInfos = [];
156  foreach ($tracks as $track) {
157  $trackingInfos[] = $track->getNumberDetail();
158  }
159  $shipTrack[$increment_id] = $trackingInfos;
160  }
161  }
162  $this->_trackingInfo = $shipTrack;
163  return $this->_trackingInfo;
164  }
165 
171  public function getTrackingInfoByShip()
172  {
173  $shipTrack = [];
174  $shipment = $this->_initShipment();
175  if ($shipment) {
176  $increment_id = $shipment->getIncrementId();
178 
179  $trackingInfos = [];
180  foreach ($tracks as $track) {
181  $trackingInfos[] = $track->getNumberDetail();
182  }
183  $shipTrack[$increment_id] = $trackingInfos;
184  }
185  $this->_trackingInfo = $shipTrack;
186  return $this->_trackingInfo;
187  }
188 
194  public function getTrackingInfoByTrackId()
195  {
197  $track = $this->_trackFactory->create()->load($this->getTrackId());
198  if ($track->getId() && $this->getProtectCode() === $track->getProtectCode()) {
199  $this->_trackingInfo = [[$track->getNumberDetail()]];
200  }
201  return $this->_trackingInfo;
202  }
203 
209  {
210  $tracks = $this->_trackCollectionFactory->create()->setShipmentFilter($shipment->getId());
211 
212  if ($shipment->getId()) {
213  foreach ($tracks as $track) {
214  $track->setShipment($shipment);
215  }
216  }
217  return $tracks;
218  }
219 }
$helper
Definition: iframe.phtml:13
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$track
Definition: details.phtml:12
$order
Definition: order.php:55
_getTracksCollection(Shipment $shipment)
Definition: Info.php:208
$shipments
__construct(\Magento\Shipping\Helper\Data $shippingData, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository, \Magento\Shipping\Model\Order\TrackFactory $trackFactory, \Magento\Shipping\Model\ResourceModel\Order\Track\CollectionFactory $trackCollectionFactory, array $data=[])
Definition: Info.php:55
setData($key, $value=null)
Definition: DataObject.php:72
foreach($order->getItems() as $orderItem) $shipment