Skip to content

Commit 23f116f

Browse files
committed
MAGETWO-57098: [Backport] Issue with fetching shipping charges based on city for 2.0.x
- Added request information for collect rates
1 parent aa0718b commit 23f116f

12 files changed

+456
-173
lines changed

app/code/Magento/Checkout/view/frontend/web/js/model/address-converter.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,25 @@ define(
116116

117117
addressToEstimationAddress: function (address) {
118118
var estimatedAddressData = {
119-
country_id: address.countryId,
120-
region: address.region,
121-
region_id: address.regionId,
122-
postcode: address.postcode
119+
'street': address.street,
120+
'city': address.city,
121+
'region_id': address.regionId,
122+
'region': address.region,
123+
'country_id': address.countryId,
124+
'postcode': address.postcode,
125+
'email': address.email,
126+
'customer_id': address.customerId,
127+
'firstname': address.firstname,
128+
'lastname': address.lastname,
129+
'middlename': address.middlename,
130+
'prefix': address.prefix,
131+
'suffix': address.suffix,
132+
'vat_id': address.vatId,
133+
'company': address.company,
134+
'telephone': address.telephone,
135+
'fax': address.fax,
136+
'custom_attributes': address.customAttributes
137+
123138
};
124139
return this.formAddressDataToQuoteAddress(estimatedAddressData);
125140
}

app/code/Magento/Checkout/view/frontend/web/js/model/shipping-rate-processor/new-address.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,25 @@ define(
2525
serviceUrl = resourceUrlManager.getUrlForEstimationShippingMethodsForNewAddress(quote),
2626
payload = JSON.stringify({
2727
address: {
28-
country_id: address.countryId,
29-
region_id: address.regionId,
30-
region: address.region,
31-
postcode: address.postcode
28+
'street': address.street,
29+
'city': address.city,
30+
'region_id': address.regionId,
31+
'region': address.region,
32+
'country_id': address.countryId,
33+
'postcode': address.postcode,
34+
'email': address.email,
35+
'customer_id': address.customerId,
36+
'firstname': address.firstname,
37+
'lastname': address.lastname,
38+
'middlename': address.middlename,
39+
'prefix': address.prefix,
40+
'suffix': address.suffix,
41+
'vat_id': address.vatId,
42+
'company': address.company,
43+
'telephone': address.telephone,
44+
'fax': address.fax,
45+
'custom_attributes': address.customAttributes,
46+
'save_in_address_book': address.saveInAddressBook
3247
}
3348
}
3449
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Quote\Api;
7+
8+
/**
9+
* Interface GuestShipmentEstimationInterface
10+
* @api
11+
*/
12+
interface GuestShipmentEstimationInterface extends ShipmentEstimationInterface
13+
{
14+
15+
}

app/code/Magento/Quote/Api/GuestShippingMethodManagementInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function getList($cartId);
2727
* @param string $cartId The shopping cart ID.
2828
* @param \Magento\Quote\Api\Data\EstimateAddressInterface $address The estimate address
2929
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
30+
* @deprecated
3031
*/
3132
public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddressInterface $address);
3233
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Quote\Api;
7+
8+
use Magento\Quote\Api\Data\AddressInterface;
9+
10+
/**
11+
* Interface ShipmentManagementInterface
12+
* @api
13+
*/
14+
interface ShipmentEstimationInterface
15+
{
16+
/**
17+
* Estimate shipping by address and return list of available shipping methods
18+
* @param mixed $cartId
19+
* @param AddressInterface $address
20+
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods
21+
*/
22+
public function estimateByExtendedAddress($cartId, AddressInterface $address);
23+
}

app/code/Magento/Quote/Api/ShippingMethodManagementInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface ShippingMethodManagementInterface
1717
* @param int $cartId The shopping cart ID.
1818
* @param \Magento\Quote\Api\Data\EstimateAddressInterface $address The estimate address
1919
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
20+
* @deprecated
2021
*/
2122
public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddressInterface $address);
2223

app/code/Magento/Quote/Model/GuestCart/GuestShippingMethodManagement.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
namespace Magento\Quote\Model\GuestCart;
88

9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Quote\Api\Data\AddressInterface;
11+
use Magento\Quote\Api\GuestShipmentEstimationInterface;
12+
use Magento\Quote\Api\ShipmentEstimationInterface;
913
use Magento\Quote\Api\ShippingMethodManagementInterface;
1014
use Magento\Quote\Model\QuoteIdMask;
1115
use Magento\Quote\Model\QuoteIdMaskFactory;
@@ -15,7 +19,8 @@
1519
*/
1620
class GuestShippingMethodManagement implements
1721
\Magento\Quote\Api\GuestShippingMethodManagementInterface,
18-
\Magento\Quote\Model\GuestCart\GuestShippingMethodManagementInterface
22+
\Magento\Quote\Model\GuestCart\GuestShippingMethodManagementInterface,
23+
GuestShipmentEstimationInterface
1924
{
2025
/**
2126
* @var ShippingMethodManagementInterface
@@ -27,6 +32,11 @@ class GuestShippingMethodManagement implements
2732
*/
2833
private $quoteIdMaskFactory;
2934

35+
/**
36+
* @var ShipmentEstimationInterface
37+
*/
38+
private $shipmentEstimationManagement;
39+
3040
/**
3141
* Constructs a shipping method read service object.
3242
*
@@ -80,4 +90,30 @@ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddre
8090
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
8191
return $this->shippingMethodManagement->estimateByAddress($quoteIdMask->getQuoteId(), $address);
8292
}
93+
94+
/**
95+
* @inheritdoc
96+
*/
97+
public function estimateByExtendedAddress($cartId, AddressInterface $address)
98+
{
99+
/** @var $quoteIdMask QuoteIdMask */
100+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
101+
102+
return $this->getShipmentEstimationManagement()
103+
->estimateByExtendedAddress((int) $quoteIdMask->getQuoteId(), $address);
104+
}
105+
106+
/**
107+
* Get shipment estimation management service
108+
* @return ShipmentEstimationInterface
109+
* @deprecated
110+
*/
111+
private function getShipmentEstimationManagement()
112+
{
113+
if ($this->shipmentEstimationManagement === null) {
114+
$this->shipmentEstimationManagement = ObjectManager::getInstance()
115+
->get(ShipmentEstimationInterface::class);
116+
}
117+
return $this->shipmentEstimationManagement;
118+
}
83119
}

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
*/
66
namespace Magento\Quote\Model;
77

8+
use Magento\Framework\Exception\CouldNotSaveException;
89
use Magento\Framework\Exception\InputException;
9-
use Magento\Framework\Exception\StateException;
1010
use Magento\Framework\Exception\NoSuchEntityException;
11-
use Magento\Framework\Exception\CouldNotSaveException;
11+
use Magento\Framework\Exception\StateException;
12+
use Magento\Quote\Api\Data\AddressInterface;
13+
use Magento\Quote\Api\Data\EstimateAddressInterface;
14+
use Magento\Quote\Api\ShipmentEstimationInterface;
15+
use Magento\Quote\Model\Quote;
1216

1317
/**
1418
* Shipping method read service.
1519
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1620
*/
1721
class ShippingMethodManagement implements
1822
\Magento\Quote\Api\ShippingMethodManagementInterface,
19-
\Magento\Quote\Model\ShippingMethodManagementInterface
23+
\Magento\Quote\Model\ShippingMethodManagementInterface,
24+
ShipmentEstimationInterface
2025
{
2126
/**
2227
* Quote repository.
@@ -185,6 +190,21 @@ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddre
185190
);
186191
}
187192

193+
/**
194+
* @inheritdoc
195+
*/
196+
public function estimateByExtendedAddress($cartId, AddressInterface $address)
197+
{
198+
/** @var \Magento\Quote\Model\Quote $quote */
199+
$quote = $this->quoteRepository->getActive($cartId);
200+
201+
// no methods applicable for empty carts or carts with virtual products
202+
if ($quote->isVirtual() || 0 == $quote->getItemsCount()) {
203+
return [];
204+
}
205+
return $this->getShippingMethods($quote, $address->getData());
206+
}
207+
188208
/**
189209
* {@inheritDoc}
190210
*/
@@ -219,14 +239,29 @@ public function estimateByAddressId($cartId, $addressId)
219239
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
220240
*/
221241
protected function getEstimatedRates(\Magento\Quote\Model\Quote $quote, $country, $postcode, $regionId, $region)
242+
{
243+
$data = [
244+
EstimateAddressInterface::KEY_COUNTRY_ID => $country,
245+
EstimateAddressInterface::KEY_POSTCODE => $postcode,
246+
EstimateAddressInterface::KEY_REGION_ID => $regionId,
247+
EstimateAddressInterface::KEY_REGION => $region
248+
];
249+
return $this->getShippingMethods($quote, $data);
250+
}
251+
252+
/**
253+
* Get list of available shipping methods
254+
* @param \Magento\Quote\Model\Quote $quote
255+
* @param array $addressData
256+
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[]
257+
*/
258+
private function getShippingMethods(Quote $quote, array $addressData)
222259
{
223260
$output = [];
224261
$shippingAddress = $quote->getShippingAddress();
225-
$shippingAddress->setCountryId($country);
226-
$shippingAddress->setPostcode($postcode);
227-
$shippingAddress->setRegionId($regionId);
228-
$shippingAddress->setRegion($region);
262+
$shippingAddress->addData($addressData);
229263
$shippingAddress->setCollectShippingRates(true);
264+
230265
$this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
231266
$shippingRates = $shippingAddress->getGroupedAllShippingRates();
232267
foreach ($shippingRates as $carrierRates) {

app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestShippingMethodManagementTest.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77

88
namespace Magento\Quote\Test\Unit\Model\GuestCart;
99

10+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
11+
use Magento\Quote\Model\QuoteIdMask;
12+
use Magento\Quote\Api\Data\AddressInterface;
13+
use Magento\Quote\Api\ShipmentEstimationInterface;
14+
use Magento\Quote\Api\Data\ShippingMethodInterface;
15+
use Magento\Quote\Model\GuestCart\GuestShippingMethodManagement;
16+
1017
class GuestShippingMethodManagementTest extends \PHPUnit_Framework_TestCase
1118
{
1219
/**
13-
* @var \Magento\Quote\Api\GuestShippingMethodManagementInterface
20+
* @var GuestShippingMethodManagement
1421
*/
1522
private $model;
1623

@@ -25,19 +32,24 @@ class GuestShippingMethodManagementTest extends \PHPUnit_Framework_TestCase
2532
private $quoteIdMaskFactoryMock;
2633

2734
/**
28-
* @var \PHPUnit_Framework_MockObject_MockObject
35+
* @var ShipmentEstimationInterface|MockObject
2936
*/
30-
private $quoteIdMaskMock;
37+
private $shipmentEstimationManagement;
3138

3239
/**
33-
* @var string
40+
* @var QuoteIdMask|MockObject
3441
*/
35-
private $maskedCartId;
42+
private $quoteIdMask;
3643

3744
/**
3845
* @var string
3946
*/
40-
private $cartId;
47+
private $maskedCartId = 'f216207248d65c789b17be8545e0aa73';
48+
49+
/**
50+
* @var int
51+
*/
52+
private $cartId = 867;
4153

4254
protected function setUp()
4355
{
@@ -46,22 +58,26 @@ protected function setUp()
4658
$this->shippingMethodManagementMock =
4759
$this->getMock('Magento\Quote\Model\ShippingMethodManagement', [], [], '', false);
4860

49-
$this->maskedCartId = 'f216207248d65c789b17be8545e0aa73';
50-
$this->cartId = 867;
51-
5261
$guestCartTestHelper = new GuestCartTestHelper($this);
53-
list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask(
62+
list($this->quoteIdMaskFactoryMock, $this->quoteIdMask) = $guestCartTestHelper->mockQuoteIdMask(
5463
$this->maskedCartId,
5564
$this->cartId
5665
);
5766

67+
$this->shipmentEstimationManagement = $this->getMockForAbstractClass(ShipmentEstimationInterface::class);
68+
5869
$this->model = $objectManager->getObject(
59-
'Magento\Quote\Model\GuestCart\GuestShippingMethodManagement',
70+
GuestShippingMethodManagement::class,
6071
[
6172
'shippingMethodManagement' => $this->shippingMethodManagementMock,
6273
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
6374
]
6475
);
76+
77+
$refObject = new \ReflectionClass(GuestShippingMethodManagement::class);
78+
$refProperty = $refObject->getProperty('shipmentEstimationManagement');
79+
$refProperty->setAccessible(true);
80+
$refProperty->setValue($this->model, $this->shipmentEstimationManagement);
6581
}
6682

6783
public function testSet()
@@ -99,4 +115,23 @@ public function testGet()
99115

100116
$this->assertEquals($retValue, $this->model->get($this->maskedCartId));
101117
}
118+
119+
/**
120+
* @covers \Magento\Quote\Model\GuestCart\GuestShippingMethodManagement::getShipmentEstimationManagement
121+
*/
122+
public function testEstimateByExtendedAddress()
123+
{
124+
$address = $this->getMockForAbstractClass(AddressInterface::class);
125+
126+
$methodObject = $this->getMockForAbstractClass(ShippingMethodInterface::class);
127+
$expectedRates = [$methodObject];
128+
129+
$this->shipmentEstimationManagement->expects(static::once())
130+
->method('estimateByExtendedAddress')
131+
->with($this->cartId, $address)
132+
->willReturn($expectedRates);
133+
134+
$carriersRates = $this->model->estimateByExtendedAddress($this->maskedCartId, $address);
135+
static::assertEquals($expectedRates, $carriersRates);
136+
}
102137
}

0 commit comments

Comments
 (0)