customerSession = $customerSession; $this->customerRepository = $customerRepository; $this->addressRepository = $addressRepository; $this->estimatedAddressFactory = $estimatedAddressFactory; $this->shippingMethodManager = $shippingMethodManager; $this->quoteRepository = $quoteRepository; } /** * @param \Magento\Quote\Model\Quote $quote * @return void */ public function collect(\Magento\Quote\Model\Quote $quote) { if ($this->customerSession->isLoggedIn()) { $customer = $this->customerRepository->getById($this->customerSession->getCustomerId()); if ($defaultShipping = $customer->getDefaultShipping()) { $address = $this->addressRepository->getById($defaultShipping); if ($address) { /** @var \Magento\Quote\Api\Data\EstimateAddressInterface $estimatedAddress */ $estimatedAddress = $this->estimatedAddressFactory->create(); $estimatedAddress->setCountryId($address->getCountryId()); $estimatedAddress->setPostcode($address->getPostcode()); $estimatedAddress->setRegion((string)$address->getRegion()->getRegion()); $estimatedAddress->setRegionId($address->getRegionId()); $this->shippingMethodManager->estimateByAddress($quote->getId(), $estimatedAddress); $this->quoteRepository->save($quote); } } } } }