PickupEmailObserver.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Observer;
  6. use Magento\Framework\DataObject;
  7. use Magento\Framework\Event\Observer;
  8. use Magento\Framework\Event\ObserverInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Sales\Model\Order\Email\Sender\OrderSender;
  11. use Temando\Shipping\Model\Delivery\OpeningHoursFormatter;
  12. use Temando\Shipping\Model\PickupInterface;
  13. use Temando\Shipping\Model\PickupProviderInterface;
  14. use Temando\Shipping\Model\ResourceModel\Repository\OrderPickupLocationRepositoryInterface;
  15. use Temando\Shipping\ViewModel\Order\Location;
  16. use Temando\Shipping\ViewModel\Pickup\PickupView;
  17. /**
  18. * Temando Pickup Email Observer
  19. *
  20. * @package Temando\Shipping\Observer
  21. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  22. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  23. * @link https://www.temando.com/
  24. */
  25. class PickupEmailObserver implements ObserverInterface
  26. {
  27. /**
  28. * @var PickupProviderInterface
  29. */
  30. private $pickupProvider;
  31. /**
  32. * @var PickupView
  33. */
  34. private $pickupViewModel;
  35. /**
  36. * @var OrderPickupLocationRepositoryInterface
  37. */
  38. private $pickupLocationRepository;
  39. /**
  40. * @var Location
  41. */
  42. private $locationViewModel;
  43. /**
  44. * @var OpeningHoursFormatter
  45. */
  46. private $hoursFormatter;
  47. /**
  48. * PickupEmailObserver constructor.
  49. * @param PickupProviderInterface $pickupProvider
  50. * @param PickupView $pickupViewModel
  51. * @param OrderPickupLocationRepositoryInterface $pickupLocationRepository
  52. * @param Location $locationViewModel
  53. * @param OpeningHoursFormatter $hoursFormatter
  54. */
  55. public function __construct(
  56. PickupProviderInterface $pickupProvider,
  57. PickupView $pickupViewModel,
  58. OrderPickupLocationRepositoryInterface $pickupLocationRepository,
  59. Location $locationViewModel,
  60. OpeningHoursFormatter $hoursFormatter
  61. ) {
  62. $this->pickupProvider = $pickupProvider;
  63. $this->pickupViewModel = $pickupViewModel;
  64. $this->pickupLocationRepository = $pickupLocationRepository;
  65. $this->locationViewModel = $locationViewModel;
  66. $this->hoursFormatter = $hoursFormatter;
  67. }
  68. /**
  69. * @param Observer $observer
  70. *
  71. * @return void
  72. */
  73. public function execute(Observer $observer)
  74. {
  75. $sender = $observer->getData('sender');
  76. if (!$sender instanceof OrderSender) {
  77. // not interested in other sales entities but orders
  78. return;
  79. }
  80. /** @var DataObject $transport */
  81. $transport = $observer->getData('transportObject');
  82. /** @var \Magento\Sales\Model\Order $order */
  83. $order = $transport->getData('order');
  84. if (!$order || !$order->getShippingAddress()) {
  85. return;
  86. }
  87. $pickup = $this->pickupProvider->getPickup();
  88. if ($pickup instanceof PickupInterface) {
  89. $location = $pickup->getPickupLocation();
  90. $openingHours = $this->hoursFormatter->format($location->getOpeningHours());
  91. // pickup action emails
  92. $transport->setData('is_pickup_order', true);
  93. $transport->setData('pickupAddress', $this->pickupViewModel->getPickupLocationAddressHtml());
  94. $transport->setData('pickup', $pickup);
  95. $transport->setData('openingHours', $openingHours);
  96. return;
  97. }
  98. try {
  99. // pickup order confirmation mail
  100. $pickup = $this->pickupLocationRepository->get($order->getShippingAddress()->getId());
  101. $transport->setData('is_pickup_order', (bool)$pickup->getPickupLocationId());
  102. $transport->setData('pickupAddress', $this->locationViewModel->getDeliveryLocationAddressHtml($order));
  103. } catch (NoSuchEntityException $e) {
  104. // regular order confirmation mail
  105. $transport->setData('is_pickup_order', false);
  106. }
  107. }
  108. }