33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace Magento \Quote \Model ;
79
810use Magento \Customer \Api \AddressRepositoryInterface ;
2527use Magento \Quote \Model \Quote \Address \Rate ;
2628use Magento \Quote \Model \Quote \TotalsCollector ;
2729use Magento \Quote \Model \ResourceModel \Quote \Address as QuoteAddressResource ;
30+ use Magento \Customer \Model \Data \Address as CustomerAddress ;
2831
2932/**
3033 * Shipping method read service
@@ -38,7 +41,7 @@ class ShippingMethodManagement implements
3841 ShipmentEstimationInterface
3942{
4043 /**
41- * Quote repository.
44+ * Quote repository model
4245 *
4346 * @var CartRepositoryInterface
4447 */
@@ -226,7 +229,7 @@ public function apply($cartId, $carrierCode, $methodCode)
226229 }
227230 $ shippingMethod = $ carrierCode . '_ ' . $ methodCode ;
228231 $ shippingAddress ->setShippingMethod ($ shippingMethod );
229- $ shippingAssignments = $ quote ->getExtensionAttributes ()->getShippingAssignments ();
232+ $ shippingAssignments = ( array ) $ quote ->getExtensionAttributes ()->getShippingAssignments ();
230233 if (!empty ($ shippingAssignments )) {
231234 $ shippingAssignment = $ shippingAssignments [0 ];
232235 $ shipping = $ shippingAssignment ->getShipping ();
@@ -268,6 +271,8 @@ public function estimateByExtendedAddress($cartId, AddressInterface $address)
268271
269272 /**
270273 * @inheritDoc
274+ * @throws InputException
275+ * @throws NoSuchEntityException
271276 */
272277 public function estimateByAddressId ($ cartId , $ addressId )
273278 {
@@ -278,7 +283,7 @@ public function estimateByAddressId($cartId, $addressId)
278283 if ($ quote ->isVirtual () || 0 == $ quote ->getItemsCount ()) {
279284 return [];
280285 }
281- $ address = $ this ->addressRepository -> getById ($ addressId );
286+ $ address = $ this ->getAddress ($ addressId, $ quote );
282287
283288 return $ this ->getShippingMethods ($ quote , $ address );
284289 }
@@ -384,4 +389,24 @@ private function getDataObjectProcessor()
384389 }
385390 return $ this ->dataProcessor ;
386391 }
392+
393+ /**
394+ * Gets the address if exists for customer
395+ *
396+ * @param int $addressId
397+ * @param Quote $quote
398+ * @return CustomerAddress
399+ * @throws InputException The shipping address is incorrect.
400+ */
401+ private function getAddress (int $ addressId , Quote $ quote ): CustomerAddress
402+ {
403+ $ addresses = $ quote ->getCustomer ()->getAddresses ();
404+ foreach ($ addresses as $ address ) {
405+ if ($ addressId === (int )$ address ->getId ()) {
406+ return $ address ;
407+ }
408+ }
409+
410+ throw new InputException (__ ('The shipping address is missing. Set the address and try again. ' ));
411+ }
387412}
0 commit comments