3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Quote \Model ;
7
9
8
10
use Magento \Customer \Api \AddressRepositoryInterface ;
25
27
use Magento \Quote \Model \Quote \Address \Rate ;
26
28
use Magento \Quote \Model \Quote \TotalsCollector ;
27
29
use Magento \Quote \Model \ResourceModel \Quote \Address as QuoteAddressResource ;
30
+ use Magento \Customer \Model \Data \Address as CustomerAddress ;
28
31
29
32
/**
30
33
* Shipping method read service
@@ -38,7 +41,7 @@ class ShippingMethodManagement implements
38
41
ShipmentEstimationInterface
39
42
{
40
43
/**
41
- * Quote repository.
44
+ * Quote repository model
42
45
*
43
46
* @var CartRepositoryInterface
44
47
*/
@@ -226,7 +229,7 @@ public function apply($cartId, $carrierCode, $methodCode)
226
229
}
227
230
$ shippingMethod = $ carrierCode . '_ ' . $ methodCode ;
228
231
$ shippingAddress ->setShippingMethod ($ shippingMethod );
229
- $ shippingAssignments = $ quote ->getExtensionAttributes ()->getShippingAssignments ();
232
+ $ shippingAssignments = ( array ) $ quote ->getExtensionAttributes ()->getShippingAssignments ();
230
233
if (!empty ($ shippingAssignments )) {
231
234
$ shippingAssignment = $ shippingAssignments [0 ];
232
235
$ shipping = $ shippingAssignment ->getShipping ();
@@ -268,6 +271,8 @@ public function estimateByExtendedAddress($cartId, AddressInterface $address)
268
271
269
272
/**
270
273
* @inheritDoc
274
+ * @throws InputException
275
+ * @throws NoSuchEntityException
271
276
*/
272
277
public function estimateByAddressId ($ cartId , $ addressId )
273
278
{
@@ -278,7 +283,7 @@ public function estimateByAddressId($cartId, $addressId)
278
283
if ($ quote ->isVirtual () || 0 == $ quote ->getItemsCount ()) {
279
284
return [];
280
285
}
281
- $ address = $ this ->addressRepository -> getById ($ addressId );
286
+ $ address = $ this ->getAddress ($ addressId, $ quote );
282
287
283
288
return $ this ->getShippingMethods ($ quote , $ address );
284
289
}
@@ -384,4 +389,24 @@ private function getDataObjectProcessor()
384
389
}
385
390
return $ this ->dataProcessor ;
386
391
}
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
+ }
387
412
}
0 commit comments