13
13
use Magento \Quote \Api \Data \EstimateAddressInterface ;
14
14
use Magento \Quote \Api \ShipmentEstimationInterface ;
15
15
use Magento \Quote \Model \Quote ;
16
+ use Magento \Framework \Reflection \DataObjectProcessor ;
17
+ use Magento \Framework \App \ObjectManager ;
18
+ use Magento \Customer \Api \Data \AddressInterfaceFactory ;
16
19
17
20
/**
18
21
* Shipping method read service.
@@ -49,6 +52,16 @@ class ShippingMethodManagement implements
49
52
*/
50
53
protected $ totalsCollector ;
51
54
55
+ /**
56
+ * @var \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor
57
+ */
58
+ private $ dataProcessor ;
59
+
60
+ /**
61
+ * @var AddressInterfaceFactory $addressFactory
62
+ */
63
+ private $ addressFactory ;
64
+
52
65
/**
53
66
* Constructs a shipping method read service object.
54
67
*
@@ -189,12 +202,9 @@ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddre
189
202
return [];
190
203
}
191
204
192
- return $ this ->getEstimatedRates (
205
+ return $ this ->getShippingMethods (
193
206
$ quote ,
194
- $ address ->getCountryId (),
195
- $ address ->getPostcode (),
196
- $ address ->getRegionId (),
197
- $ address ->getRegion ()
207
+ $ address
198
208
);
199
209
}
200
210
@@ -210,7 +220,7 @@ public function estimateByExtendedAddress($cartId, AddressInterface $address)
210
220
if ($ quote ->isVirtual () || 0 == $ quote ->getItemsCount ()) {
211
221
return [];
212
222
}
213
- return $ this ->getShippingMethods ($ quote , $ address-> getData () );
223
+ return $ this ->getShippingMethods ($ quote , $ address );
214
224
}
215
225
216
226
/**
@@ -227,13 +237,7 @@ public function estimateByAddressId($cartId, $addressId)
227
237
}
228
238
$ address = $ this ->addressRepository ->getById ($ addressId );
229
239
230
- return $ this ->getEstimatedRates (
231
- $ quote ,
232
- $ address ->getCountryId (),
233
- $ address ->getPostcode (),
234
- $ address ->getRegionId (),
235
- $ address ->getRegion ()
236
- );
240
+ return $ this ->getShippingMethods ($ quote , $ address );
237
241
}
238
242
239
243
/**
@@ -244,30 +248,41 @@ public function estimateByAddressId($cartId, $addressId)
244
248
* @param string $postcode
245
249
* @param int $regionId
246
250
* @param string $region
251
+ * @param \Magento\Framework\Api\ExtensibleDataInterface|null $address
247
252
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
253
+ * @deprecated
248
254
*/
249
- protected function getEstimatedRates (\Magento \Quote \Model \Quote $ quote , $ country , $ postcode , $ regionId , $ region )
250
- {
251
- $ data = [
252
- EstimateAddressInterface::KEY_COUNTRY_ID => $ country ,
253
- EstimateAddressInterface::KEY_POSTCODE => $ postcode ,
254
- EstimateAddressInterface::KEY_REGION_ID => $ regionId ,
255
- EstimateAddressInterface::KEY_REGION => $ region
256
- ];
257
- return $ this ->getShippingMethods ($ quote , $ data );
255
+ protected function getEstimatedRates (
256
+ \Magento \Quote \Model \Quote $ quote ,
257
+ $ country ,
258
+ $ postcode ,
259
+ $ regionId ,
260
+ $ region ,
261
+ $ address = null
262
+ ) {
263
+ if (!$ address ) {
264
+ $ address = $ this ->getAddressFactory ()->create ()
265
+ ->setCountryId ($ country )
266
+ ->setPostcode ($ postcode )
267
+ ->setRegionId ($ regionId )
268
+ ->setRegion ($ region );
269
+ }
270
+ return $ this ->getShippingMethods ($ quote , $ address );
258
271
}
259
272
260
273
/**
261
274
* Get list of available shipping methods
262
275
* @param \Magento\Quote\Model\Quote $quote
263
- * @param array $addressData
276
+ * @param \Magento\Framework\Api\ExtensibleDataInterface|null $address
264
277
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[]
265
278
*/
266
- private function getShippingMethods (Quote $ quote , array $ addressData )
279
+ private function getShippingMethods (Quote $ quote , $ address )
267
280
{
268
281
$ output = [];
269
282
$ shippingAddress = $ quote ->getShippingAddress ();
270
- $ shippingAddress ->addData ($ addressData );
283
+ if ($ address ) {
284
+ $ shippingAddress ->addData ($ this ->extractAddressData ($ address ));
285
+ }
271
286
$ shippingAddress ->setCollectShippingRates (true );
272
287
273
288
$ this ->totalsCollector ->collectAddressTotals ($ quote , $ shippingAddress );
@@ -279,4 +294,51 @@ private function getShippingMethods(Quote $quote, array $addressData)
279
294
}
280
295
return $ output ;
281
296
}
297
+
298
+ /**
299
+ * Get transform address interface into Array
300
+ * @param \Magento\Framework\Api\ExtensibleDataInterface $address
301
+ * @return array
302
+ */
303
+ private function extractAddressData ($ address )
304
+ {
305
+ $ className = \Magento \Customer \Api \Data \AddressInterface::class;
306
+ if ($ address instanceof \Magento \Quote \Api \Data \AddressInterface) {
307
+ $ className = \Magento \Quote \Api \Data \AddressInterface::class;
308
+ } elseif ($ address instanceof EstimateAddressInterface) {
309
+ $ className = EstimateAddressInterface::class;
310
+ }
311
+ return $ this ->getDataObjectProcessor ()->buildOutputDataArray (
312
+ $ address ,
313
+ $ className
314
+ );
315
+ }
316
+
317
+ /**
318
+ * Gets the data object processor
319
+ * @return \Magento\Framework\Reflection\DataObjectProcessor
320
+ * @deprecated
321
+ */
322
+ private function getDataObjectProcessor ()
323
+ {
324
+ if ($ this ->dataProcessor === null ) {
325
+ $ this ->dataProcessor = ObjectManager::getInstance ()
326
+ ->get (DataObjectProcessor::class);
327
+ }
328
+ return $ this ->dataProcessor ;
329
+ }
330
+
331
+ /**
332
+ * Gets the address factory
333
+ * @return AddressInterfaceFactory
334
+ * @deprecated
335
+ */
336
+ private function getAddressFactory ()
337
+ {
338
+ if ($ this ->addressFactory === null ) {
339
+ $ this ->addressFactory = ObjectManager::getInstance ()
340
+ ->get (AddressInterfaceFactory::class);
341
+ }
342
+ return $ this ->addressFactory ;
343
+ }
282
344
}
0 commit comments