ShippingAddressManagement.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Exception\InputException;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Psr\Log\LoggerInterface as Logger;
  11. /**
  12. * Quote shipping address write service object.
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class ShippingAddressManagement implements \Magento\Quote\Model\ShippingAddressManagementInterface
  16. {
  17. /**
  18. * Quote repository.
  19. *
  20. * @var \Magento\Quote\Api\CartRepositoryInterface
  21. */
  22. protected $quoteRepository;
  23. /**
  24. * Logger.
  25. *
  26. * @var Logger
  27. */
  28. protected $logger;
  29. /**
  30. * Validator.
  31. *
  32. * @var QuoteAddressValidator
  33. */
  34. protected $addressValidator;
  35. /**
  36. * @var \Magento\Customer\Api\AddressRepositoryInterface
  37. */
  38. protected $addressRepository;
  39. /**
  40. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  41. */
  42. protected $scopeConfig;
  43. /**
  44. * @var Quote\TotalsCollector
  45. */
  46. protected $totalsCollector;
  47. /**
  48. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  49. * @param QuoteAddressValidator $addressValidator
  50. * @param Logger $logger
  51. * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  52. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  53. * @param Quote\TotalsCollector $totalsCollector
  54. *
  55. */
  56. public function __construct(
  57. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  58. QuoteAddressValidator $addressValidator,
  59. Logger $logger,
  60. \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
  61. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  62. \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
  63. ) {
  64. $this->quoteRepository = $quoteRepository;
  65. $this->addressValidator = $addressValidator;
  66. $this->logger = $logger;
  67. $this->addressRepository = $addressRepository;
  68. $this->scopeConfig = $scopeConfig;
  69. $this->totalsCollector = $totalsCollector;
  70. }
  71. /**
  72. * @inheritDoc
  73. * @SuppressWarnings(PHPMD.NPathComplexity)
  74. */
  75. public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address)
  76. {
  77. /** @var \Magento\Quote\Model\Quote $quote */
  78. $quote = $this->quoteRepository->getActive($cartId);
  79. if ($quote->isVirtual()) {
  80. throw new NoSuchEntityException(
  81. __('The Cart includes virtual product(s) only, so a shipping address is not used.')
  82. );
  83. }
  84. $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
  85. $sameAsBilling = $address->getSameAsBilling() ? 1 : 0;
  86. $customerAddressId = $address->getCustomerAddressId();
  87. $this->addressValidator->validateForCart($quote, $address);
  88. $quote->setShippingAddress($address);
  89. $address = $quote->getShippingAddress();
  90. if ($customerAddressId === null) {
  91. $address->setCustomerAddressId(null);
  92. }
  93. if ($customerAddressId) {
  94. $addressData = $this->addressRepository->getById($customerAddressId);
  95. $address = $quote->getShippingAddress()->importCustomerAddressData($addressData);
  96. } elseif ($quote->getCustomerId()) {
  97. $address->setEmail($quote->getCustomerEmail());
  98. }
  99. $address->setSameAsBilling($sameAsBilling);
  100. $address->setSaveInAddressBook($saveInAddressBook);
  101. $address->setCollectShippingRates(true);
  102. try {
  103. $address->save();
  104. } catch (\Exception $e) {
  105. $this->logger->critical($e);
  106. throw new InputException(__('The address failed to save. Verify the address and try again.'));
  107. }
  108. return $quote->getShippingAddress()->getId();
  109. }
  110. /**
  111. * @inheritDoc
  112. */
  113. public function get($cartId)
  114. {
  115. /** @var \Magento\Quote\Model\Quote $quote */
  116. $quote = $this->quoteRepository->getActive($cartId);
  117. if ($quote->isVirtual()) {
  118. throw new NoSuchEntityException(
  119. __('The Cart includes virtual product(s) only, so a shipping address is not used.')
  120. );
  121. }
  122. /** @var \Magento\Quote\Model\Quote\Address $address */
  123. return $quote->getShippingAddress();
  124. }
  125. }