Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CollectQuote.php
Go to the documentation of this file.
1 <?php
7 
9 {
13  protected $customerSession;
14 
19 
23  protected $addressRepository;
24 
29 
34 
38  protected $quoteRepository;
39 
49  public function __construct(
50  \Magento\Customer\Model\Session $customerSession,
53  \Magento\Quote\Api\Data\EstimateAddressInterfaceFactory $estimatedAddressFactory,
54  \Magento\Quote\Api\ShippingMethodManagementInterface $shippingMethodManager,
56  ) {
57  $this->customerSession = $customerSession;
58  $this->customerRepository = $customerRepository;
59  $this->addressRepository = $addressRepository;
60  $this->estimatedAddressFactory = $estimatedAddressFactory;
61  $this->shippingMethodManager = $shippingMethodManager;
62  $this->quoteRepository = $quoteRepository;
63  }
64 
69  public function collect(\Magento\Quote\Model\Quote $quote)
70  {
71  if ($this->customerSession->isLoggedIn()) {
72  $customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
73  if ($defaultShipping = $customer->getDefaultShipping()) {
74  $address = $this->addressRepository->getById($defaultShipping);
75  if ($address) {
77  $estimatedAddress = $this->estimatedAddressFactory->create();
78  $estimatedAddress->setCountryId($address->getCountryId());
79  $estimatedAddress->setPostcode($address->getPostcode());
80  $estimatedAddress->setRegion((string)$address->getRegion()->getRegion());
81  $estimatedAddress->setRegionId($address->getRegionId());
82  $this->shippingMethodManager->estimateByAddress($quote->getId(), $estimatedAddress);
83  $this->quoteRepository->save($quote);
84  }
85  }
86  }
87  }
88 }
$customer
Definition: customers.php:11
$quote
$address
Definition: customer.php:38
__construct(\Magento\Customer\Model\Session $customerSession, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Customer\Api\AddressRepositoryInterface $addressRepository, \Magento\Quote\Api\Data\EstimateAddressInterfaceFactory $estimatedAddressFactory, \Magento\Quote\Api\ShippingMethodManagementInterface $shippingMethodManager, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository)