CollectRatesPlugin.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Plugin\Shipping;
  6. use Magento\Framework\Exception\LocalizedException;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Quote\Model\Quote\Address\RateCollectorInterface;
  9. use Magento\Quote\Model\Quote\Address\RateRequest;
  10. use Temando\Shipping\Model\Checkout\RateRequest\Extractor;
  11. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  12. use Temando\Shipping\Model\ResourceModel\Repository\CollectionPointSearchRepositoryInterface;
  13. use Temando\Shipping\Model\ResourceModel\Repository\PickupLocationSearchRepositoryInterface;
  14. use Temando\Shipping\Model\Shipping\Carrier;
  15. /**
  16. * CollectRatesPlugin
  17. *
  18. * @package Temando\Shipping\Plugin
  19. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  20. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  21. * @link https://www.temando.com/
  22. */
  23. class CollectRatesPlugin
  24. {
  25. /**
  26. * @var ModuleConfigInterface
  27. */
  28. private $moduleConfig;
  29. /**
  30. * @var Extractor
  31. */
  32. private $rateRequestExtractor;
  33. /**
  34. * @var CollectionPointSearchRepositoryInterface
  35. */
  36. private $collectionPointSearchRequestRepository;
  37. /**
  38. * @var PickupLocationSearchRepositoryInterface
  39. */
  40. private $pickupLocationSearchRequestRepository;
  41. /**
  42. * CollectRatesPlugin constructor.
  43. * @param ModuleConfigInterface $moduleConfig
  44. * @param Extractor $rateRequestExtractor
  45. * @param CollectionPointSearchRepositoryInterface $collectionPointSearchRequestRepository
  46. * @param PickupLocationSearchRepositoryInterface $pickupLocationSearchRequestRepository
  47. */
  48. public function __construct(
  49. ModuleConfigInterface $moduleConfig,
  50. Extractor $rateRequestExtractor,
  51. CollectionPointSearchRepositoryInterface $collectionPointSearchRequestRepository,
  52. PickupLocationSearchRepositoryInterface $pickupLocationSearchRequestRepository
  53. ) {
  54. $this->moduleConfig = $moduleConfig;
  55. $this->rateRequestExtractor = $rateRequestExtractor;
  56. $this->collectionPointSearchRequestRepository = $collectionPointSearchRequestRepository;
  57. $this->pickupLocationSearchRequestRepository = $pickupLocationSearchRequestRepository;
  58. }
  59. /**
  60. * Check if collection point delivery option was chosen.
  61. *
  62. * @param int $addressId
  63. * @return bool
  64. */
  65. private function isCollectionPointDelivery($addressId)
  66. {
  67. try {
  68. $collectionPointSearch = $this->collectionPointSearchRequestRepository->get($addressId);
  69. $isCollectionPointDelivery = (bool) $collectionPointSearch->getShippingAddressId();
  70. } catch (NoSuchEntityException $exception) {
  71. $isCollectionPointDelivery = false;
  72. }
  73. return $isCollectionPointDelivery;
  74. }
  75. /**
  76. * Check if store pickup delivery option was chosen.
  77. *
  78. * @param int $addressId
  79. * @return bool
  80. */
  81. private function isPickupLocationDelivery($addressId)
  82. {
  83. try {
  84. $pickupLocationSearch = $this->pickupLocationSearchRequestRepository->get($addressId);
  85. $isPickupLocationDelivery = (bool) $pickupLocationSearch->getShippingAddressId();
  86. } catch (NoSuchEntityException $exception) {
  87. $isPickupLocationDelivery = false;
  88. }
  89. return $isPickupLocationDelivery;
  90. }
  91. /**
  92. * Disable other carriers if current shipping destination is
  93. * a collection point or store pickup location.
  94. *
  95. * @param RateCollectorInterface $subject
  96. * @param RateRequest $rateRequest
  97. * @return null
  98. */
  99. public function beforeCollectRates(RateCollectorInterface $subject, RateRequest $rateRequest)
  100. {
  101. try {
  102. $quote = $this->rateRequestExtractor->getQuote($rateRequest);
  103. $shippingAddress = $this->rateRequestExtractor->getShippingAddress($rateRequest);
  104. } catch (LocalizedException $exception) {
  105. return null;
  106. }
  107. $isCheckoutEnabled = $this->moduleConfig->isEnabled($quote->getStoreId());
  108. $isDeliveryLocationEnabled = $this->moduleConfig->isCollectionPointsEnabled($quote->getStoreId())
  109. || $this->moduleConfig->isClickAndCollectEnabled($quote->getStoreId());
  110. if (!$isCheckoutEnabled || !$isDeliveryLocationEnabled) {
  111. // certainly no collection point or click&collect delivery
  112. return null;
  113. }
  114. $addressId = $shippingAddress->getId();
  115. $isCollectionPointDelivery = $this->isCollectionPointDelivery($addressId);
  116. $isPickupLocationDelivery = $this->isPickupLocationDelivery($addressId);
  117. if ($isCollectionPointDelivery || $isPickupLocationDelivery) {
  118. $rateRequest->setLimitCarrier(Carrier::CODE);
  119. }
  120. return null;
  121. }
  122. }