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,13 +202,7 @@ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddre
189
202
return [];
190
203
}
191
204
192
- return $ this ->getEstimatedRates (
193
- $ quote ,
194
- $ address ->getCountryId (),
195
- $ address ->getPostcode (),
196
- $ address ->getRegionId (),
197
- $ address ->getRegion ()
198
- );
205
+ return $ this ->getShippingMethods ($ quote , $ address );
199
206
}
200
207
201
208
/**
@@ -210,7 +217,7 @@ public function estimateByExtendedAddress($cartId, AddressInterface $address)
210
217
if ($ quote ->isVirtual () || 0 == $ quote ->getItemsCount ()) {
211
218
return [];
212
219
}
213
- return $ this ->getShippingMethods ($ quote , $ address-> getData () );
220
+ return $ this ->getShippingMethods ($ quote , $ address );
214
221
}
215
222
216
223
/**
@@ -227,13 +234,7 @@ public function estimateByAddressId($cartId, $addressId)
227
234
}
228
235
$ address = $ this ->addressRepository ->getById ($ addressId );
229
236
230
- return $ this ->getEstimatedRates (
231
- $ quote ,
232
- $ address ->getCountryId (),
233
- $ address ->getPostcode (),
234
- $ address ->getRegionId (),
235
- $ address ->getRegion ()
236
- );
237
+ return $ this ->getShippingMethods ($ quote , $ address );
237
238
}
238
239
239
240
/**
@@ -244,30 +245,39 @@ public function estimateByAddressId($cartId, $addressId)
244
245
* @param string $postcode
245
246
* @param int $regionId
246
247
* @param string $region
248
+ * @param \Magento\Framework\Api\ExtensibleDataInterface|null $address
247
249
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
250
+ * @deprecated
248
251
*/
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 );
252
+ protected function getEstimatedRates (
253
+ \Magento \Quote \Model \Quote $ quote ,
254
+ $ country ,
255
+ $ postcode ,
256
+ $ regionId ,
257
+ $ region ,
258
+ $ address = null
259
+ ) {
260
+ if (!$ address ) {
261
+ $ address = $ this ->getAddressFactory ()->create ()
262
+ ->setCountryId ($ country )
263
+ ->setPostcode ($ postcode )
264
+ ->setRegionId ($ regionId )
265
+ ->setRegion ($ region );
266
+ }
267
+ return $ this ->getShippingMethods ($ quote , $ address );
258
268
}
259
269
260
270
/**
261
271
* Get list of available shipping methods
262
272
* @param \Magento\Quote\Model\Quote $quote
263
- * @param array $addressData
273
+ * @param \Magento\Framework\Api\ExtensibleDataInterface $address
264
274
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[]
265
275
*/
266
- private function getShippingMethods (Quote $ quote , array $ addressData )
276
+ private function getShippingMethods (Quote $ quote , $ address )
267
277
{
268
278
$ output = [];
269
279
$ shippingAddress = $ quote ->getShippingAddress ();
270
- $ shippingAddress ->addData ($ addressData );
280
+ $ shippingAddress ->addData ($ this -> extractAddressData ( $ address ) );
271
281
$ shippingAddress ->setCollectShippingRates (true );
272
282
273
283
$ this ->totalsCollector ->collectAddressTotals ($ quote , $ shippingAddress );
@@ -279,4 +289,51 @@ private function getShippingMethods(Quote $quote, array $addressData)
279
289
}
280
290
return $ output ;
281
291
}
292
+
293
+ /**
294
+ * Get transform address interface into Array
295
+ * @param \Magento\Framework\Api\ExtensibleDataInterface $address
296
+ * @return array
297
+ */
298
+ private function extractAddressData ($ address )
299
+ {
300
+ $ className = \Magento \Customer \Api \Data \AddressInterface::class;
301
+ if ($ address instanceof \Magento \Quote \Api \Data \AddressInterface) {
302
+ $ className = \Magento \Quote \Api \Data \AddressInterface::class;
303
+ } elseif ($ address instanceof EstimateAddressInterface) {
304
+ $ className = EstimateAddressInterface::class;
305
+ }
306
+ return $ this ->getDataObjectProcessor ()->buildOutputDataArray (
307
+ $ address ,
308
+ $ className
309
+ );
310
+ }
311
+
312
+ /**
313
+ * Gets the data object processor
314
+ * @return \Magento\Framework\Reflection\DataObjectProcessor
315
+ * @deprecated
316
+ */
317
+ private function getDataObjectProcessor ()
318
+ {
319
+ if ($ this ->dataProcessor === null ) {
320
+ $ this ->dataProcessor = ObjectManager::getInstance ()
321
+ ->get (DataObjectProcessor::class);
322
+ }
323
+ return $ this ->dataProcessor ;
324
+ }
325
+
326
+ /**
327
+ * Gets the address factory
328
+ * @return AddressInterfaceFactory
329
+ * @deprecated
330
+ */
331
+ private function getAddressFactory ()
332
+ {
333
+ if ($ this ->addressFactory === null ) {
334
+ $ this ->addressFactory = ObjectManager::getInstance ()
335
+ ->get (AddressInterfaceFactory::class);
336
+ }
337
+ return $ this ->addressFactory ;
338
+ }
282
339
}
0 commit comments