AddOrderDetailsObserver.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\CreditmemoInterface;
  10. use Magento\Sales\Api\Data\InvoiceInterface;
  11. use Magento\Sales\Api\Data\OrderInterface;
  12. use Temando\Shipping\Model\Shipping\Carrier;
  13. /**
  14. * Change order info template for Temando orders
  15. *
  16. * @package Temando\Shipping\Observer
  17. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  18. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. * @link https://www.temando.com/
  20. */
  21. class AddOrderDetailsObserver implements ObserverInterface
  22. {
  23. /**
  24. * @var Registry
  25. */
  26. private $registry;
  27. /**
  28. * AddPickupTabObserver constructor.
  29. * @param Registry $registry
  30. */
  31. public function __construct(Registry $registry)
  32. {
  33. $this->registry = $registry;
  34. }
  35. /**
  36. * @return OrderInterface
  37. */
  38. private function getOrder()
  39. {
  40. $creditmemo = $this->registry->registry('current_creditmemo');
  41. if ($creditmemo instanceof CreditmemoInterface) {
  42. return $creditmemo->getOrder();
  43. }
  44. $invoice = $this->registry->registry('current_invoice');
  45. if ($invoice instanceof InvoiceInterface) {
  46. return $invoice->getOrder();
  47. }
  48. return $this->registry->registry('current_order');
  49. }
  50. /**
  51. * Temando provides additional order details compared to the default carriers:
  52. * - external order reference
  53. * - collection point address
  54. * Apply a custom template that includes these – locally available – data items.
  55. *
  56. * - event: layout_load_before
  57. *
  58. * @param Observer $observer
  59. * @return void
  60. */
  61. public function execute(Observer $observer)
  62. {
  63. $applicableActions = [
  64. 'sales_order_view',
  65. 'sales_order_invoice_new',
  66. 'sales_order_invoice_view',
  67. 'sales_order_creditmemo_new',
  68. 'sales_order_creditmemo_view',
  69. ];
  70. $action = $observer->getData('full_action_name');
  71. if (!in_array($action, $applicableActions)) {
  72. // not the order details page
  73. return;
  74. }
  75. /** @var \Magento\Sales\Model\Order $order */
  76. $order = $this->getOrder();
  77. if (!$order instanceof OrderInterface || !$order->getData('shipping_method')) {
  78. return;
  79. }
  80. $shippingMethod = $order->getShippingMethod(true);
  81. if ($shippingMethod->getData('carrier_code') !== Carrier::CODE) {
  82. return;
  83. }
  84. /** @var \Magento\Framework\View\Layout $layout */
  85. $layout = $observer->getData('layout');
  86. $layout->getUpdate()->addHandle('temando_order_info');
  87. }
  88. }