ShippingConfiguration.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\Model\QuoteManagement;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\InstantPurchase\Model\ShippingMethodChoose\DeferredShippingMethodChooserInterface;
  9. use Magento\InstantPurchase\Model\ShippingMethodChoose\DeferredShippingMethodChooserPool;
  10. use Magento\Quote\Api\Data\ShippingMethodInterface;
  11. use Magento\Quote\Model\Quote;
  12. use Magento\Quote\Model\Quote\Address;
  13. /**
  14. * Configure shipping method for instant purchase
  15. *
  16. * @api May be used for pluginization.
  17. * @since 100.2.0
  18. */
  19. class ShippingConfiguration
  20. {
  21. /**
  22. * @var DeferredShippingMethodChooserPool
  23. */
  24. private $deferredShippingMethodChooserPool;
  25. /**
  26. * ShippingConfiguration constructor.
  27. * @param DeferredShippingMethodChooserPool $deferredShippingMethodChooserPool
  28. */
  29. public function __construct(
  30. DeferredShippingMethodChooserPool $deferredShippingMethodChooserPool
  31. ) {
  32. $this->deferredShippingMethodChooserPool = $deferredShippingMethodChooserPool;
  33. }
  34. /**
  35. * Sets shipping information to quote.
  36. *
  37. * @param Quote $quote
  38. * @param ShippingMethodInterface $shippingMethod
  39. * @return Quote
  40. * @throws LocalizedException if shipping can not be configured for a quote.
  41. * @since 100.2.0
  42. */
  43. public function configureShippingMethod(
  44. Quote $quote,
  45. ShippingMethodInterface $shippingMethod
  46. ): Quote {
  47. if ($quote->isVirtual()) {
  48. return $quote;
  49. }
  50. $shippingAddress = $quote->getShippingAddress();
  51. $shippingMethodCode = $this->getShippingMethodCodeToUse($shippingAddress, $shippingMethod);
  52. $shippingAddress->setShippingMethod($shippingMethodCode);
  53. return $quote;
  54. }
  55. /**
  56. * Build quote specific shipping method code based on shipping method.
  57. *
  58. * @param Address $address
  59. * @param ShippingMethodInterface $shippingMethod
  60. * @return string
  61. * @throws LocalizedException if shipping code can not be detected.
  62. */
  63. private function getShippingMethodCodeToUse(
  64. Address $address,
  65. ShippingMethodInterface $shippingMethod
  66. ): string {
  67. if ($shippingMethod->getCarrierCode() === DeferredShippingMethodChooserInterface::CARRIER) {
  68. return $this->resolveDeferredShippingMethodChoose($address, $shippingMethod);
  69. } else {
  70. return $this->getCorrespondingShippingRateCode($address, $shippingMethod);
  71. }
  72. }
  73. /**
  74. * Detects real shipping method code.
  75. *
  76. * @param Address $address
  77. * @param ShippingMethodInterface $shippingMethod
  78. * @return string
  79. * @throws LocalizedException if shipping method not applicable to quote.
  80. */
  81. private function getCorrespondingShippingRateCode(
  82. Address $address,
  83. ShippingMethodInterface $shippingMethod
  84. ): string {
  85. $address->setCollectShippingRates(true);
  86. $address->collectShippingRates();
  87. $shippingRates = $address->getAllShippingRates();
  88. foreach ($shippingRates as $shippingRate) {
  89. if ($shippingRate->getCarrier() === $shippingMethod->getCarrierCode()
  90. &&
  91. $shippingRate->getMethod() === $shippingMethod->getMethodCode()
  92. ) {
  93. return $shippingRate->getCode();
  94. }
  95. }
  96. throw new LocalizedException(__('Specified shipping method is not available.'));
  97. }
  98. /**
  99. * Detect real shipping method based on provided strategy and shipping address with quote data.
  100. *
  101. * @param Address $address
  102. * @param ShippingMethodInterface $shippingMethod
  103. * @return string
  104. * @throws LocalizedException if appropriate shipping method is not available
  105. */
  106. private function resolveDeferredShippingMethodChoose(
  107. Address $address,
  108. ShippingMethodInterface $shippingMethod
  109. ): string {
  110. $deferredShippingMethodChooser = $this->deferredShippingMethodChooserPool->get(
  111. $shippingMethod->getMethodCode()
  112. );
  113. $shippingMethodCode = $deferredShippingMethodChooser->choose($address);
  114. if (empty($shippingMethodCode)) {
  115. throw new LocalizedException(__('Appropriate shipping method is not available.'));
  116. }
  117. return $shippingMethodCode;
  118. }
  119. }