@@ -105,6 +105,24 @@ public function __construct(
105105 $ this ->storeManager = $ storeManager ;
106106 }
107107
108+ /**
109+ * Ensure that the RateRequest always has store and website IDs set. Hyvä is way stricter compared to Luma in this
110+ * regard.
111+ * @param \Magento\Quote\Model\Quote\Address\RateRequest $request
112+ * @return \Magento\Quote\Model\Quote\Address\RateRequest
113+ */
114+ protected function ensureRequestScope (\Magento \Quote \Model \Quote \Address \RateRequest $ request )
115+ {
116+ $ storeId = (int ) $ request ->getStoreId ();
117+ $ websiteId = (int ) $ request ->getWebsiteId ();
118+ if ($ storeId === 0 || $ websiteId === 0 ) {
119+ $ store = $ this ->storeManager ->getStore ();
120+ $ request ->setStoreId ((int ) $ store ->getId ());
121+ $ request ->setWebsiteId ((int ) $ store ->getWebsiteId ());
122+ }
123+ return $ request ;
124+ }
125+
108126 /**
109127 * The DHL Shipping shipping carrier does not calculate rates.
110128 * @param \Magento\Quote\Model\Quote\Address\RateRequest $request
@@ -113,6 +131,9 @@ public function __construct(
113131 */
114132 public function collectRates (\Magento \Quote \Model \Quote \Address \RateRequest $ request )
115133 {
134+
135+ $ this ->ensureRequestScope ($ request );
136+
116137 $ this ->debugLogger ->info ('CARRIER ### initiating collect rates ' , $ request ->toArray ());
117138 /** @var \Magento\Shipping\Model\Rate\Result $result */
118139 $ result = $ this ->_rateFactory ->create ();
@@ -223,17 +244,50 @@ protected function getShippingMethod(
223244 return null ;
224245 }
225246
226- $ method ->setPrice ($ rate ['price ' ] + $ serviceCost );
247+ $ finalPrice = $ rate ['price ' ] + $ serviceCost ;
248+ $ method ->setPrice ($ finalPrice );
227249 $ method ->setCost ($ rate ['cost ' ]);
250+ $ this ->debugLogger ->info ("CARRIER method $ methodKey variable rate price set " , [
251+ 'ratePrice ' => $ rate ['price ' ],
252+ 'serviceCost ' => $ serviceCost ,
253+ 'finalPrice ' => $ finalPrice ,
254+ ]);
228255 } else {
229- $ method ->setPrice ($ this ->getConfigData ('shipping_methods/ ' . $ methodKey . '/price ' ) + $ serviceCost );
256+ $ configPrice = $ this ->getConfigData ('shipping_methods/ ' . $ methodKey . '/price ' );
257+ $ finalPrice = $ configPrice + $ serviceCost ;
258+ $ method ->setPrice ($ finalPrice );
259+ $ this ->debugLogger ->info ("CARRIER method $ methodKey fixed price set " , [
260+ 'configPrice ' => $ configPrice ,
261+ 'serviceCost ' => $ serviceCost ,
262+ 'finalPrice ' => $ finalPrice ,
263+ ]);
230264 }
231265
232- if ($ request ->getFreeShipping () || $ request ->getPackageQty () == $ this ->cartService ->getFreeBoxesCount ($ request )) {
266+ $ freeShippingFlag = $ request ->getFreeShipping ();
267+ $ packageQty = $ request ->getPackageQty ();
268+ $ freeBoxesCount = $ this ->cartService ->getFreeBoxesCount ($ request );
269+
270+ // Only consider it free shipping if:
271+ // 1. The free shipping flag is explicitly set, OR
272+ // 2. packageQty equals freeBoxesCount AND both are greater than 0
273+ // (prevents false positive when both are 0 due to empty/stale request data)
274+ $ isFreeShipping = $ freeShippingFlag || ($ packageQty > 0 && $ packageQty == $ freeBoxesCount );
275+
276+ $ this ->debugLogger ->info ("CARRIER method $ methodKey free shipping check " , [
277+ 'freeShippingFlag ' => $ freeShippingFlag ,
278+ 'packageQty ' => $ packageQty ,
279+ 'freeBoxesCount ' => $ freeBoxesCount ,
280+ 'isFreeShipping ' => $ isFreeShipping ,
281+ 'priceBeforeFreeShippingCheck ' => $ method ->getPrice (),
282+ ]);
283+
284+ if ($ isFreeShipping ) {
233285 if (boolval ($ this ->getConfigData ('usability/discount_after_coupon/always_charge_servicecosts ' ))) {
234286 $ method ->setPrice ($ serviceCost );
287+ $ this ->debugLogger ->info ("CARRIER method $ methodKey set to service cost only due to free shipping " , ['serviceCost ' => $ serviceCost ]);
235288 } else {
236289 $ method ->setPrice ('0.00 ' );
290+ $ this ->debugLogger ->info ("CARRIER method $ methodKey set to 0.00 due to free shipping " );
237291 }
238292 }
239293
@@ -283,6 +337,8 @@ public function getTracking($tracking)
283337
284338 protected function getMethodTitle (\Magento \Quote \Model \Quote \Address \RateRequest $ request , $ key , $ titleKey = 'title ' )
285339 {
340+ $ this ->ensureRequestScope ($ request );
341+
286342 // Default
287343 $ title = $ this ->getConfigData ('shipping_methods/ ' . $ key . '/ ' . $ titleKey );
288344 if ($ titleKey !== 'title ' && empty ($ title )) {
0 commit comments