/** * @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); } } } }
/** * {@inheritDoc} */ protected function _beforeToHtml() { 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($this->getQuote()->getId(), $estimatedAddress); $this->quoteRepository->save($this->getQuote()); } } } return parent::_beforeToHtml(); }
/** * Get default shipping rates * * @return array */ private function getDefaultShippingRates() { $output = []; $addressKey = null; if ($this->checkoutSession->getQuote()->getId()) { $quote = $this->quoteRepository->get($this->checkoutSession->getQuote()->getId()); /** @var \Magento\Quote\Api\Data\EstimateAddressInterface $estimatedAddress */ $estimatedAddress = $this->estimatedAddressFactory->create(); $address = $quote->getShippingAddress(); if ($address && ($address->getCountryId() || $address->getPostcode() || $address->getRegion() || $address->getRegionId())) { $estimatedAddress->setCountryId($address->getCountryId()); $estimatedAddress->setPostcode($address->getPostcode()); $estimatedAddress->setRegion($address->getRegion()); $estimatedAddress->setRegionId($address->getRegionId()); } else { $estimatedAddress->setCountryId($this->directoryHelper->getDefaultCountry()); } $rates = $this->shippingMethodManager->estimateByAddress($quote->getId(), $estimatedAddress); foreach ($rates as $rate) { $output[] = $rate->__toArray(); } if ($address->getCustomerAddressId()) { $addressKey = 'customer-address' . $address->getCustomerAddressId(); } } return ['key' => $addressKey, 'data' => $output]; }