Skip to content

Commit 6cd2dc4

Browse files
committed
MAGETWO-55117: ShippingMethodManagement::estimateByAddressId set not full address details
- merging the usage of all addresses types into one channel and deprecating incomplete protected method but still making it safe to use for other possible classes that might extend it
1 parent e7e4a88 commit 6cd2dc4

File tree

1 file changed

+87
-25
lines changed

1 file changed

+87
-25
lines changed

app/code/Magento/Quote/Model/ShippingMethodManagement.php

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Magento\Quote\Api\Data\EstimateAddressInterface;
1414
use Magento\Quote\Api\ShipmentEstimationInterface;
1515
use Magento\Quote\Model\Quote;
16+
use Magento\Framework\Reflection\DataObjectProcessor;
17+
use Magento\Framework\App\ObjectManager;
18+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1619

1720
/**
1821
* Shipping method read service.
@@ -49,6 +52,16 @@ class ShippingMethodManagement implements
4952
*/
5053
protected $totalsCollector;
5154

55+
/**
56+
* @var \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor
57+
*/
58+
private $dataProcessor;
59+
60+
/**
61+
* @var AddressInterfaceFactory $addressFactory
62+
*/
63+
private $addressFactory;
64+
5265
/**
5366
* Constructs a shipping method read service object.
5467
*
@@ -189,12 +202,9 @@ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddre
189202
return [];
190203
}
191204

192-
return $this->getEstimatedRates(
205+
return $this->getShippingMethods(
193206
$quote,
194-
$address->getCountryId(),
195-
$address->getPostcode(),
196-
$address->getRegionId(),
197-
$address->getRegion()
207+
$address
198208
);
199209
}
200210

@@ -210,7 +220,7 @@ public function estimateByExtendedAddress($cartId, AddressInterface $address)
210220
if ($quote->isVirtual() || 0 == $quote->getItemsCount()) {
211221
return [];
212222
}
213-
return $this->getShippingMethods($quote, $address->getData());
223+
return $this->getShippingMethods($quote, $address);
214224
}
215225

216226
/**
@@ -227,13 +237,7 @@ public function estimateByAddressId($cartId, $addressId)
227237
}
228238
$address = $this->addressRepository->getById($addressId);
229239

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);
237241
}
238242

239243
/**
@@ -244,30 +248,41 @@ public function estimateByAddressId($cartId, $addressId)
244248
* @param string $postcode
245249
* @param int $regionId
246250
* @param string $region
251+
* @param \Magento\Framework\Api\ExtensibleDataInterface|null $address
247252
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
253+
* @deprecated
248254
*/
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);
258271
}
259272

260273
/**
261274
* Get list of available shipping methods
262275
* @param \Magento\Quote\Model\Quote $quote
263-
* @param array $addressData
276+
* @param \Magento\Framework\Api\ExtensibleDataInterface|null $address
264277
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[]
265278
*/
266-
private function getShippingMethods(Quote $quote, array $addressData)
279+
private function getShippingMethods(Quote $quote, $address)
267280
{
268281
$output = [];
269282
$shippingAddress = $quote->getShippingAddress();
270-
$shippingAddress->addData($addressData);
283+
if ($address) {
284+
$shippingAddress->addData($this->extractAddressData($address));
285+
}
271286
$shippingAddress->setCollectShippingRates(true);
272287

273288
$this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
@@ -279,4 +294,51 @@ private function getShippingMethods(Quote $quote, array $addressData)
279294
}
280295
return $output;
281296
}
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+
}
282344
}

0 commit comments

Comments
 (0)