PickupLocations.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\CustomerData;
  6. use Magento\Customer\CustomerData\SectionSourceInterface;
  7. use Magento\Framework\EntityManager\HydratorInterface;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Framework\Session\SessionManagerInterface;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. use Temando\Shipping\Api\Data\Delivery\QuotePickupLocationInterface;
  13. use Temando\Shipping\Api\Delivery\CartPickupLocationManagementInterface;
  14. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  15. use Temando\Shipping\Model\Delivery\DistanceConverter;
  16. use Temando\Shipping\Model\Delivery\OpeningHoursFormatter;
  17. use Temando\Shipping\Model\Delivery\QuotePickupLocation;
  18. use Temando\Shipping\Model\ResourceModel\Repository\PickupLocationSearchRepositoryInterface;
  19. /**
  20. * PickupLocations
  21. *
  22. * @package Temando\Shipping\CustomerData
  23. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  24. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * @link https://www.temando.com/
  26. */
  27. class PickupLocations implements SectionSourceInterface
  28. {
  29. /**
  30. * @var StoreManagerInterface
  31. */
  32. private $storeManager;
  33. /**
  34. * @var ModuleConfigInterface
  35. */
  36. private $moduleConfig;
  37. /**
  38. * @var SessionManagerInterface|\Magento\Checkout\Model\Session
  39. */
  40. private $checkoutSession;
  41. /**
  42. * @var HydratorInterface
  43. */
  44. private $hydrator;
  45. /**
  46. * @var PickupLocationSearchRepositoryInterface
  47. */
  48. private $searchRequestRepository;
  49. /**
  50. * @var CartPickupLocationManagementInterface
  51. */
  52. private $cartPickupLocationManagement;
  53. /**
  54. * @var OpeningHoursFormatter
  55. */
  56. private $openingHoursFormatter;
  57. /**
  58. * @var DistanceConverter
  59. */
  60. private $distanceConverter;
  61. /**
  62. * PickupLocations constructor.
  63. * @param StoreManagerInterface $storeManager
  64. * @param ModuleConfigInterface $moduleConfig
  65. * @param SessionManagerInterface $checkoutSession
  66. * @param HydratorInterface $hydrator
  67. * @param PickupLocationSearchRepositoryInterface $searchRequestRepository
  68. * @param CartPickupLocationManagementInterface $cartPickupLocationManagement
  69. * @param OpeningHoursFormatter $openingHoursFormatter
  70. * @param DistanceConverter $distanceConverter
  71. */
  72. public function __construct(
  73. StoreManagerInterface $storeManager,
  74. ModuleConfigInterface $moduleConfig,
  75. SessionManagerInterface $checkoutSession,
  76. HydratorInterface $hydrator,
  77. PickupLocationSearchRepositoryInterface $searchRequestRepository,
  78. CartPickupLocationManagementInterface $cartPickupLocationManagement,
  79. OpeningHoursFormatter $openingHoursFormatter,
  80. DistanceConverter $distanceConverter
  81. ) {
  82. $this->storeManager = $storeManager;
  83. $this->moduleConfig = $moduleConfig;
  84. $this->checkoutSession = $checkoutSession;
  85. $this->hydrator = $hydrator;
  86. $this->searchRequestRepository = $searchRequestRepository;
  87. $this->cartPickupLocationManagement = $cartPickupLocationManagement;
  88. $this->openingHoursFormatter = $openingHoursFormatter;
  89. $this->distanceConverter = $distanceConverter;
  90. }
  91. /**
  92. * Obtain pickup locations data for display in checkout, shipping method step.
  93. *
  94. * @return string[]
  95. */
  96. public function getSectionData()
  97. {
  98. try {
  99. $storeId = $this->storeManager->getStore()->getId();
  100. } catch (NoSuchEntityException $exception) {
  101. $storeId = null;
  102. }
  103. if (!$this->moduleConfig->isEnabled($storeId) || !$this->moduleConfig->isClickAndCollectEnabled($storeId)) {
  104. return [
  105. 'pickup-locations' => [],
  106. 'search-request' => []
  107. ];
  108. }
  109. $quote = $this->checkoutSession->getQuote();
  110. $quoteAddressId = $quote->getShippingAddress()->getId();
  111. try {
  112. $searchRequest = $this->searchRequestRepository->get($quoteAddressId);
  113. $searchRequest = $this->hydrator->extract($searchRequest);
  114. $pickupLocations = $this->cartPickupLocationManagement->getPickupLocations($quote->getId());
  115. } catch (LocalizedException $e) {
  116. $searchRequest = [];
  117. $pickupLocations = [];
  118. }
  119. // map pickup locations to data array with formatted/localized opening hours
  120. $pickupLocations = array_map(function (QuotePickupLocationInterface $pickupLocation) use ($storeId) {
  121. /** @var QuotePickupLocation $pickupLocation */
  122. $pickupLocationData = $pickupLocation->toArray();
  123. $openingHours = $this->openingHoursFormatter->format($pickupLocation->getOpeningHours());
  124. $pickupLocationData[QuotePickupLocationInterface::OPENING_HOURS] = $openingHours;
  125. $distance = $this->distanceConverter->format($pickupLocation->getDistance(), $storeId);
  126. $pickupLocationData[QuotePickupLocationInterface::DISTANCE] = $distance;
  127. return $pickupLocationData;
  128. }, $pickupLocations);
  129. return [
  130. 'pickup-locations' => array_values($pickupLocations),
  131. 'search-request' => $searchRequest
  132. ];
  133. }
  134. }