PrepareMyOrderInfoObserver.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\Sales\Api\Data\OrderInterface;
  9. use Temando\Shipping\Model\Shipping\Carrier;
  10. use Temando\Shipping\ViewModel\Order\Location;
  11. /**
  12. * Change order info template for temando orders in customer account.
  13. *
  14. * @package Temando\Shipping\Observer
  15. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  16. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. * @link https://www.temando.com/
  18. */
  19. class PrepareMyOrderInfoObserver implements ObserverInterface
  20. {
  21. /**
  22. * @var Location
  23. */
  24. private $viewModel;
  25. /**
  26. * PrepareMyOrderInfoObserver constructor.
  27. * @param Location $viewModel
  28. */
  29. public function __construct(Location $viewModel)
  30. {
  31. $this->viewModel = $viewModel;
  32. }
  33. /**
  34. * Temando provides additional order details compared to the default carriers:
  35. * - collection point address
  36. * - pickup location address
  37. * Apply a custom template that displays these data items.
  38. *
  39. * - event: layout_generate_blocks_after
  40. *
  41. * @param Observer $observer
  42. * @return void
  43. */
  44. public function execute(Observer $observer)
  45. {
  46. $applicableActions = [
  47. 'sales_order_view',
  48. 'sales_order_invoice',
  49. 'sales_order_creditmemo',
  50. ];
  51. $action = $observer->getData('full_action_name');
  52. if (!in_array($action, $applicableActions)) {
  53. return;
  54. }
  55. /** @var \Magento\Framework\View\Layout $layout */
  56. $layout = $observer->getData('layout');
  57. $infoBlock = $layout->getBlock('sales.order.info');
  58. if (!$infoBlock instanceof \Magento\Sales\Block\Order\Info) {
  59. return;
  60. }
  61. $order = $infoBlock->getOrder();
  62. if (!$order instanceof OrderInterface || !$order->getData('shipping_method')) {
  63. // wrong type, virtual or corrupt order
  64. return;
  65. }
  66. $shippingMethod = $order->getShippingMethod(true);
  67. if ($shippingMethod->getData('carrier_code') !== Carrier::CODE) {
  68. return;
  69. }
  70. $infoBlock->setTemplate('Temando_Shipping::order/info.phtml');
  71. $infoBlock->setData('viewModel', $this->viewModel);
  72. }
  73. }