PrepareMyShipmentInfoObserver.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Observer\CustomerLayout;
  6. use Magento\Framework\Event\Observer;
  7. use Magento\Framework\Event\ObserverInterface;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Sales\Api\Data\OrderInterface;
  10. use Temando\Shipping\Model\ResourceModel\Repository\ShipmentReferenceRepositoryInterface;
  11. use Temando\Shipping\Model\ResourceModel\Repository\ShipmentRepositoryInterface;
  12. use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;
  13. use Temando\Shipping\Model\Shipping\Carrier;
  14. use Temando\Shipping\ViewModel\Shipment\Location;
  15. /**
  16. * Change order info template for temando shipments in customer account.
  17. *
  18. * @package Temando\Shipping\Observer
  19. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  20. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  21. * @link https://www.temando.com/
  22. */
  23. class PrepareMyShipmentInfoObserver implements ObserverInterface
  24. {
  25. /**
  26. * @var ShipmentProviderInterface
  27. */
  28. private $shipmentProvider;
  29. /**
  30. * @var ShipmentReferenceRepositoryInterface
  31. */
  32. private $shipmentReferenceRepository;
  33. /**
  34. * @var ShipmentRepositoryInterface
  35. */
  36. private $shipmentRepository;
  37. /**
  38. * @var Location
  39. */
  40. private $viewModel;
  41. /**
  42. * PrepareMyShipmentInfoObserver constructor.
  43. * @param ShipmentProviderInterface $shipmentProvider
  44. * @param ShipmentReferenceRepositoryInterface $shipmentReferenceRepository
  45. * @param ShipmentRepositoryInterface $shipmentRepository
  46. * @param Location $viewModel
  47. */
  48. public function __construct(
  49. ShipmentProviderInterface $shipmentProvider,
  50. ShipmentReferenceRepositoryInterface $shipmentReferenceRepository,
  51. ShipmentRepositoryInterface $shipmentRepository,
  52. Location $viewModel
  53. ) {
  54. $this->shipmentProvider = $shipmentProvider;
  55. $this->shipmentReferenceRepository = $shipmentReferenceRepository;
  56. $this->shipmentRepository = $shipmentRepository;
  57. $this->viewModel = $viewModel;
  58. }
  59. /**
  60. * Temando provides additional order details compared to the default carriers:
  61. * - collection point address.
  62. * Apply a custom template that displays these data items.
  63. *
  64. * - event: layout_generate_blocks_after
  65. *
  66. * @param Observer $observer
  67. * @return void
  68. */
  69. public function execute(Observer $observer)
  70. {
  71. $action = $observer->getData('full_action_name');
  72. if ($action !== 'sales_order_shipment') {
  73. return;
  74. }
  75. /** @var \Magento\Framework\View\Layout $layout */
  76. $layout = $observer->getData('layout');
  77. $infoBlock = $layout->getBlock('sales.order.info');
  78. if (!$infoBlock instanceof \Magento\Sales\Block\Order\Info) {
  79. return;
  80. }
  81. $order = $infoBlock->getOrder();
  82. if (!$order instanceof OrderInterface || !$order->getData('shipping_method')) {
  83. // wrong type, virtual or corrupt order
  84. return;
  85. }
  86. $shippingMethod = $order->getShippingMethod(true);
  87. if ($shippingMethod->getData('carrier_code') !== Carrier::CODE) {
  88. return;
  89. }
  90. // add first available sales shipment with external shipment to registry
  91. foreach ($order->getShipmentsCollection() as $salesShipment) {
  92. $this->shipmentProvider->setSalesShipment($salesShipment);
  93. try {
  94. $shipmentReference = $this->shipmentReferenceRepository->getByShipmentId($salesShipment->getEntityId());
  95. $shipment = $this->shipmentRepository->getById($shipmentReference->getExtShipmentId());
  96. $this->shipmentProvider->setShipment($shipment);
  97. } catch (LocalizedException $exception) {
  98. continue;
  99. }
  100. break;
  101. }
  102. $infoBlock->setTemplate('Temando_Shipping::order/shipment/info.phtml');
  103. $infoBlock->setData('viewModel', $this->viewModel);
  104. }
  105. }