CollectQuote.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Model\Cart;
  7. class CollectQuote
  8. {
  9. /**
  10. * @var \Magento\Customer\Model\Session
  11. */
  12. protected $customerSession;
  13. /**
  14. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  15. */
  16. protected $customerRepository;
  17. /**
  18. * @var \Magento\Customer\Api\AddressRepositoryInterface
  19. */
  20. protected $addressRepository;
  21. /**
  22. * @var \Magento\Quote\Api\Data\EstimateAddressInterfaceFactory
  23. */
  24. protected $estimatedAddressFactory;
  25. /**
  26. * @var \Magento\Quote\Api\ShippingMethodManagementInterface
  27. */
  28. protected $shippingMethodManager;
  29. /**
  30. * @var \Magento\Quote\Api\CartRepositoryInterface
  31. */
  32. protected $quoteRepository;
  33. /**
  34. * @param \Magento\Customer\Model\Session $customerSession
  35. * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  36. * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  37. * @param \Magento\Quote\Api\Data\EstimateAddressInterfaceFactory $estimatedAddressFactory
  38. * @param \Magento\Quote\Api\ShippingMethodManagementInterface $shippingMethodManager
  39. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  40. * @codeCoverageIgnore
  41. */
  42. public function __construct(
  43. \Magento\Customer\Model\Session $customerSession,
  44. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
  45. \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
  46. \Magento\Quote\Api\Data\EstimateAddressInterfaceFactory $estimatedAddressFactory,
  47. \Magento\Quote\Api\ShippingMethodManagementInterface $shippingMethodManager,
  48. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  49. ) {
  50. $this->customerSession = $customerSession;
  51. $this->customerRepository = $customerRepository;
  52. $this->addressRepository = $addressRepository;
  53. $this->estimatedAddressFactory = $estimatedAddressFactory;
  54. $this->shippingMethodManager = $shippingMethodManager;
  55. $this->quoteRepository = $quoteRepository;
  56. }
  57. /**
  58. * @param \Magento\Quote\Model\Quote $quote
  59. * @return void
  60. */
  61. public function collect(\Magento\Quote\Model\Quote $quote)
  62. {
  63. if ($this->customerSession->isLoggedIn()) {
  64. $customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
  65. if ($defaultShipping = $customer->getDefaultShipping()) {
  66. $address = $this->addressRepository->getById($defaultShipping);
  67. if ($address) {
  68. /** @var \Magento\Quote\Api\Data\EstimateAddressInterface $estimatedAddress */
  69. $estimatedAddress = $this->estimatedAddressFactory->create();
  70. $estimatedAddress->setCountryId($address->getCountryId());
  71. $estimatedAddress->setPostcode($address->getPostcode());
  72. $estimatedAddress->setRegion((string)$address->getRegion()->getRegion());
  73. $estimatedAddress->setRegionId($address->getRegionId());
  74. $this->shippingMethodManager->estimateByAddress($quote->getId(), $estimatedAddress);
  75. $this->quoteRepository->save($quote);
  76. }
  77. }
  78. }
  79. }
  80. }