Skip to content

Commit e81b6fd

Browse files
author
Volodymyr Kublytskyi
committed
Shipping rates should not be calculated until quote is created as this may cause issues in 3rd party modules which expects all properties filled in rate request.
1 parent 29a1a27 commit e81b6fd

File tree

4 files changed

+71
-89
lines changed

4 files changed

+71
-89
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\InstantPurchase\Model\ShippingMethodChoose;
7+
8+
use Magento\Customer\Model\Address;
9+
use Magento\Framework\DataObject;
10+
use Magento\Shipping\Model\Config as CarriersConfig;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
13+
/**
14+
* Collect shipping rates for customer address without packaging estiamtion.
15+
*/
16+
class CarrierFinder
17+
{
18+
/**
19+
* @var CarriersConfig
20+
*/
21+
private $carriersConfig;
22+
23+
/**
24+
* @var StoreManagerInterface
25+
*/
26+
private $storeManager;
27+
28+
/**
29+
* CarrierFinder constructor.
30+
* @param CarriersConfig $carriersConfig
31+
* @param StoreManagerInterface $storeManager
32+
*/
33+
public function __construct(
34+
CarriersConfig $carriersConfig,
35+
StoreManagerInterface $storeManager
36+
) {
37+
$this->carriersConfig = $carriersConfig;
38+
$this->storeManager = $storeManager;
39+
}
40+
41+
/**
42+
* Finds carriers delivering to customer address
43+
*
44+
* @param Address $address
45+
* @return array
46+
*/
47+
public function getCarriersForCustomerAddress(Address $address): array
48+
{
49+
$request = new DataObject([
50+
'dest_country_id' => $address->getCountryId()
51+
]);
52+
53+
$carriers = [];
54+
foreach ($this->carriersConfig->getActiveCarriers($this->storeManager->getStore()->getId()) as $carrier) {
55+
$checked = $carrier->checkAvailableShipCountries($request);
56+
if (false !== $checked && null === $checked->getErrorMessage() && !empty($checked->getAllowedMethods())) {
57+
$carriers[] = $checked;
58+
}
59+
}
60+
61+
return $carriers;
62+
}
63+
}

app/code/Magento/InstantPurchase/Model/ShippingMethodChoose/CheapestMethodChooser.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Magento\InstantPurchase\Model\ShippingMethodChoose;
77

88
use Magento\Customer\Model\Address;
9-
use Magento\Quote\Api\Data\ShippingMethodInterface;
109
use Magento\Quote\Api\Data\ShippingMethodInterfaceFactory;
1110

1211
/**
@@ -20,21 +19,21 @@ class CheapestMethodChooser implements ShippingMethodChooserInterface
2019
private $shippingMethodFactory;
2120

2221
/**
23-
* @var ShippingRateFinder
22+
* @var CarrierFinder
2423
*/
25-
private $shippingRateFinder;
24+
private $carrierFinder;
2625

2726
/**
2827
* CheapestMethodChooser constructor.
2928
* @param ShippingMethodInterfaceFactory $shippingMethodFactory
30-
* @param ShippingRateFinder $shippingRateFinder
29+
* @param CarrierFinder $carrierFinder
3130
*/
3231
public function __construct(
3332
ShippingMethodInterfaceFactory $shippingMethodFactory,
34-
ShippingRateFinder $shippingRateFinder
33+
CarrierFinder $carrierFinder
3534
) {
3635
$this->shippingMethodFactory = $shippingMethodFactory;
37-
$this->shippingRateFinder = $shippingRateFinder;
36+
$this->carrierFinder = $carrierFinder;
3837
}
3938

4039
/**
@@ -58,7 +57,7 @@ public function choose(Address $address)
5857
*/
5958
private function areShippingMethodsAvailable(Address $address): bool
6059
{
61-
$shippingRatesForAddress = $this->shippingRateFinder->getRatesForCustomerAddress($address);
62-
return !empty($shippingRatesForAddress);
60+
$carriersForAddress = $this->carrierFinder->getCarriersForCustomerAddress($address);
61+
return !empty($carriersForAddress);
6362
}
6463
}

app/code/Magento/InstantPurchase/Model/ShippingMethodChoose/ShippingRateFinder.php

Lines changed: 0 additions & 81 deletions
This file was deleted.

app/code/Magento/InstantPurchase/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"magento/module-catalog": "102.0.*",
1414
"magento/module-customer": "101.0.*",
1515
"magento/module-sales": "101.0.*",
16+
"magento/module-shipping": "100.2.*",
1617
"magento/module-quote": "101.0.*",
1718
"magento/module-vault": "101.0.*",
1819
"magento/framework": "100.2.*"

0 commit comments

Comments
 (0)