CollectionPoints.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\QuoteCollectionPointInterface;
  13. use Temando\Shipping\Api\Delivery\CartCollectionPointManagementInterface;
  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\QuoteCollectionPoint;
  18. use Temando\Shipping\Model\ResourceModel\Repository\CollectionPointSearchRepositoryInterface;
  19. /**
  20. * CollectionPoints
  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 CollectionPoints 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 CollectionPointSearchRepositoryInterface
  47. */
  48. private $searchRequestRepository;
  49. /**
  50. * @var CartCollectionPointManagementInterface
  51. */
  52. private $cartCollectionPointManagement;
  53. /**
  54. * @var OpeningHoursFormatter
  55. */
  56. private $openingHoursFormatter;
  57. /**
  58. * @var DistanceConverter
  59. */
  60. private $distanceConverter;
  61. /**
  62. * CollectionPoints constructor.
  63. * @param StoreManagerInterface $storeManager
  64. * @param ModuleConfigInterface $moduleConfig
  65. * @param SessionManagerInterface $checkoutSession
  66. * @param HydratorInterface $hydrator
  67. * @param CollectionPointSearchRepositoryInterface $searchRequestRepository
  68. * @param CartCollectionPointManagementInterface $cartCollectionPointManagement
  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. CollectionPointSearchRepositoryInterface $searchRequestRepository,
  78. CartCollectionPointManagementInterface $cartCollectionPointManagement,
  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->cartCollectionPointManagement = $cartCollectionPointManagement;
  88. $this->openingHoursFormatter = $openingHoursFormatter;
  89. $this->distanceConverter = $distanceConverter;
  90. }
  91. /**
  92. * Obtain collection points 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->isCollectionPointsEnabled($storeId)) {
  104. return [
  105. 'collection-points' => [],
  106. 'search-request' => [],
  107. ];
  108. }
  109. $quote = $this->checkoutSession->getQuote();
  110. $quoteAddressId = $quote->getShippingAddress()->getId();
  111. // check if customer checks out with collection points delivery option
  112. try {
  113. // a search request was performed or is pending (waiting for search input)
  114. $searchRequest = $this->searchRequestRepository->get($quoteAddressId);
  115. $searchRequestData = $this->hydrator->extract($searchRequest);
  116. } catch (LocalizedException $e) {
  117. // no search request found at all for given address
  118. $searchRequestData = [];
  119. }
  120. if (empty($searchRequestData) || !empty($searchRequestData['pending'])) {
  121. return [
  122. 'collection-points' => [],
  123. 'search-request' => $searchRequestData,
  124. ];
  125. }
  126. $collectionPoints = $this->cartCollectionPointManagement->getCollectionPoints($quote->getId());
  127. // map collection points to data array with formatted/localized opening hours
  128. $collectionPoints = array_map(function (QuoteCollectionPointInterface $collectionPoint) use ($storeId) {
  129. /** @var QuoteCollectionPoint $collectionPoint */
  130. $collectionPointData = $collectionPoint->toArray();
  131. $openingHours = $this->openingHoursFormatter->format($collectionPoint->getOpeningHours());
  132. $collectionPointData[QuoteCollectionPointInterface::OPENING_HOURS] = $openingHours;
  133. $distance = $this->distanceConverter->format($collectionPoint->getDistance(), $storeId, '%1$s%2$s', 2);
  134. $collectionPointData[QuoteCollectionPointInterface::DISTANCE] = $distance;
  135. return $collectionPointData;
  136. }, $collectionPoints);
  137. return [
  138. 'collection-points' => array_values($collectionPoints),
  139. 'search-request' => $searchRequestData,
  140. ];
  141. }
  142. }