AddShipmentNewComponentObserver.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Magento\Framework\Registry;
  9. use Magento\Sales\Api\Data\OrderInterface;
  10. use Magento\Sales\Api\Data\ShipmentInterface;
  11. use Temando\Shipping\Model\Shipping\Carrier;
  12. /**
  13. * Add OrderShip component
  14. *
  15. * @package Temando\Shipping\Observer
  16. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  17. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  18. * @link https://www.temando.com/
  19. */
  20. class AddShipmentNewComponentObserver implements ObserverInterface
  21. {
  22. /**
  23. * @var Registry
  24. */
  25. private $registry;
  26. /**
  27. * AddPickupTabObserver constructor.
  28. * @param Registry $registry
  29. */
  30. public function __construct(Registry $registry)
  31. {
  32. $this->registry = $registry;
  33. }
  34. /**
  35. * Temando replaces the default packaging popup. Add the OrderShip component
  36. * to the "content" container of the New Shipment page as well as additional
  37. * order details.
  38. *
  39. * - event: layout_load_before
  40. *
  41. * @param Observer $observer
  42. * @return void
  43. */
  44. public function execute(Observer $observer)
  45. {
  46. $applicableActions = [
  47. 'adminhtml_order_shipment_new',
  48. ];
  49. $action = $observer->getData('full_action_name');
  50. if (!in_array($action, $applicableActions)) {
  51. // not the new shipment page
  52. return;
  53. }
  54. /** @var \Magento\Sales\Model\Order\Shipment $shipment */
  55. $shipment = $this->registry->registry('current_shipment');
  56. if (!$shipment instanceof ShipmentInterface) {
  57. // no additional data to display
  58. return;
  59. }
  60. $order = $shipment->getOrder();
  61. if (!$order instanceof OrderInterface) {
  62. return;
  63. }
  64. if ($order->getIsVirtual() || !$order->getData('shipping_method')) {
  65. return;
  66. }
  67. $shippingMethod = $order->getShippingMethod(true);
  68. if ($shippingMethod->getData('carrier_code') !== Carrier::CODE) {
  69. return;
  70. }
  71. /** @var \Magento\Framework\View\Layout $layout */
  72. $layout = $observer->getData('layout');
  73. // display additional order details
  74. $layout->getUpdate()->addHandle('temando_order_info');
  75. // add OrderShip component
  76. $layout->getUpdate()->addHandle('temando_order_ship_component');
  77. }
  78. }