7
7
namespace Magento \Quote \Model ;
8
8
9
9
use Magento \Customer \Api \Data \AddressInterfaceFactory ;
10
+ use Magento \Customer \Model \Session as CustomerSession ;
10
11
use Magento \Framework \App \ObjectManager ;
11
12
use Magento \Framework \Exception \CouldNotSaveException ;
12
13
use Magento \Framework \Exception \InputException ;
22
23
* Shipping method read service
23
24
*
24
25
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26
+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
25
27
*/
26
28
class ShippingMethodManagement implements
27
29
\Magento \Quote \Api \ShippingMethodManagementInterface,
@@ -69,6 +71,11 @@ class ShippingMethodManagement implements
69
71
*/
70
72
private $ quoteAddressResource ;
71
73
74
+ /**
75
+ * @var CustomerSession
76
+ */
77
+ private $ customerSession ;
78
+
72
79
/**
73
80
* Constructor
74
81
*
@@ -78,14 +85,16 @@ class ShippingMethodManagement implements
78
85
* @param Quote\TotalsCollector $totalsCollector
79
86
* @param AddressInterfaceFactory|null $addressFactory
80
87
* @param QuoteAddressResource|null $quoteAddressResource
88
+ * @param CustomerSession|null $customerSession
81
89
*/
82
90
public function __construct (
83
91
\Magento \Quote \Api \CartRepositoryInterface $ quoteRepository ,
84
92
Cart \ShippingMethodConverter $ converter ,
85
93
\Magento \Customer \Api \AddressRepositoryInterface $ addressRepository ,
86
94
\Magento \Quote \Model \Quote \TotalsCollector $ totalsCollector ,
87
95
AddressInterfaceFactory $ addressFactory = null ,
88
- QuoteAddressResource $ quoteAddressResource = null
96
+ QuoteAddressResource $ quoteAddressResource = null ,
97
+ CustomerSession $ customerSession = null
89
98
) {
90
99
$ this ->quoteRepository = $ quoteRepository ;
91
100
$ this ->converter = $ converter ;
@@ -95,10 +104,11 @@ public function __construct(
95
104
->get (AddressInterfaceFactory::class);
96
105
$ this ->quoteAddressResource = $ quoteAddressResource ?: ObjectManager::getInstance ()
97
106
->get (QuoteAddressResource::class);
107
+ $ this ->customerSession = $ customerSession ?? ObjectManager::getInstance ()->get (CustomerSession::class);
98
108
}
99
109
100
110
/**
101
- * { @inheritDoc}
111
+ * @inheritDoc
102
112
*/
103
113
public function get ($ cartId )
104
114
{
@@ -126,7 +136,7 @@ public function get($cartId)
126
136
}
127
137
128
138
/**
129
- * { @inheritDoc}
139
+ * @inheritDoc
130
140
*/
131
141
public function getList ($ cartId )
132
142
{
@@ -155,7 +165,7 @@ public function getList($cartId)
155
165
}
156
166
157
167
/**
158
- * { @inheritDoc}
168
+ * @inheritDoc
159
169
*/
160
170
public function set ($ cartId , $ carrierCode , $ methodCode )
161
171
{
@@ -176,6 +186,8 @@ public function set($cartId, $carrierCode, $methodCode)
176
186
}
177
187
178
188
/**
189
+ * Apply carrier code.
190
+ *
179
191
* @param int $cartId The shopping cart ID.
180
192
* @param string $carrierCode The carrier code.
181
193
* @param string $methodCode The shipping method code.
@@ -209,7 +221,7 @@ public function apply($cartId, $carrierCode, $methodCode)
209
221
}
210
222
211
223
/**
212
- * { @inheritDoc}
224
+ * @inheritDoc
213
225
*/
214
226
public function estimateByAddress ($ cartId , \Magento \Quote \Api \Data \EstimateAddressInterface $ address )
215
227
{
@@ -240,7 +252,7 @@ public function estimateByExtendedAddress($cartId, AddressInterface $address)
240
252
}
241
253
242
254
/**
243
- * { @inheritDoc}
255
+ * @inheritDoc
244
256
*/
245
257
public function estimateByAddressId ($ cartId , $ addressId )
246
258
{
@@ -266,6 +278,7 @@ public function estimateByAddressId($cartId, $addressId)
266
278
* @param string $region
267
279
* @param \Magento\Framework\Api\ExtensibleDataInterface|null $address
268
280
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
281
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
269
282
* @deprecated 100.2.0
270
283
*/
271
284
protected function getEstimatedRates (
@@ -277,11 +290,10 @@ protected function getEstimatedRates(
277
290
$ address = null
278
291
) {
279
292
if (!$ address ) {
280
- $ address = $ this ->getAddressFactory () ->create ()
293
+ $ address = $ this ->addressFactory ->create ()
281
294
->setCountryId ($ country )
282
295
->setPostcode ($ postcode )
283
- ->setRegionId ($ regionId )
284
- ->setRegion ($ region );
296
+ ->setRegionId ($ regionId );
285
297
}
286
298
return $ this ->getShippingMethods ($ quote , $ address );
287
299
}
@@ -301,12 +313,21 @@ private function getShippingMethods(Quote $quote, $address)
301
313
$ shippingAddress ->setCollectShippingRates (true );
302
314
303
315
$ this ->totalsCollector ->collectAddressTotals ($ quote , $ shippingAddress );
316
+ $ quoteCustomerGroupId = $ quote ->getCustomerGroupId ();
317
+ $ customerGroupId = $ this ->customerSession ->getCustomerGroupId ();
318
+ $ isCustomerGroupChanged = $ quoteCustomerGroupId !== $ customerGroupId ;
319
+ if ($ isCustomerGroupChanged ) {
320
+ $ quote ->setCustomerGroupId ($ customerGroupId );
321
+ }
304
322
$ shippingRates = $ shippingAddress ->getGroupedAllShippingRates ();
305
323
foreach ($ shippingRates as $ carrierRates ) {
306
324
foreach ($ carrierRates as $ rate ) {
307
325
$ output [] = $ this ->converter ->modelToDataObject ($ rate , $ quote ->getQuoteCurrencyCode ());
308
326
}
309
327
}
328
+ if ($ isCustomerGroupChanged ) {
329
+ $ quote ->setCustomerGroupId ($ quoteCustomerGroupId );
330
+ }
310
331
return $ output ;
311
332
}
312
333
0 commit comments