AddShipmentDetailsObserver.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Observer\AdminLayout;
  6. use Magento\Framework\Event\Observer;
  7. use Magento\Framework\Event\ObserverInterface;
  8. use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;
  9. /**
  10. * Change templates for Temando shipments
  11. *
  12. * @package Temando\Shipping\Observer
  13. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  14. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  15. * @link https://www.temando.com/
  16. */
  17. class AddShipmentDetailsObserver implements ObserverInterface
  18. {
  19. /**
  20. * @var ShipmentProviderInterface
  21. */
  22. private $shipmentProvider;
  23. /**
  24. * AddShipmentDetailsObserver constructor.
  25. * @param ShipmentProviderInterface $shipmentProvider
  26. */
  27. public function __construct(ShipmentProviderInterface $shipmentProvider)
  28. {
  29. $this->shipmentProvider = $shipmentProvider;
  30. }
  31. /**
  32. * Temando provides additional shipment details compared to the default carriers:
  33. * - external order reference
  34. * - external shipment reference
  35. * - origin location
  36. * - delivery location
  37. * - final recipient location
  38. * - add-ons
  39. * - documentation
  40. * - export details
  41. * Apply a custom template that includes child blocks with these data items.
  42. *
  43. * - event: layout_load_before
  44. *
  45. * Templates will only be changed if a shipment was loaded from the API.
  46. * @see \Temando\Shipping\Plugin\Shipping\Order\ShipmentLoaderPlugin::afterLoad
  47. *
  48. * @param Observer $observer
  49. * @return void
  50. */
  51. public function execute(Observer $observer)
  52. {
  53. $applicableActions = [
  54. 'adminhtml_order_shipment_view',
  55. ];
  56. $action = $observer->getData('full_action_name');
  57. if (!in_array($action, $applicableActions)) {
  58. // not the shipment details page
  59. return;
  60. }
  61. $shipment = $this->shipmentProvider->getShipment();
  62. if (!$shipment) {
  63. // no additional data to display
  64. return;
  65. }
  66. /** @var \Magento\Framework\View\Layout $layout */
  67. $layout = $observer->getData('layout');
  68. $layout->getUpdate()->addHandle('temando_order_shipment_view');
  69. $originCountry = $shipment->getOriginLocation()->getCountryCode();
  70. $destinationCountry = $shipment->getDestinationLocation()->getCountryCode();
  71. if ($originCountry !== $destinationCountry) {
  72. // add additional item data for cross border shipments
  73. $layout->getUpdate()->addHandle('temando_order_shipment_view_xb');
  74. }
  75. }
  76. }