Skip to content

Commit c1cee22

Browse files
danielmetaconnollyathensbirdDaniel M Connollysinghramanpreett
authored
Shipping sync database scope changes (#407)
* Update dependency versions for version 1.2.0 * changes * change * changes * changes * changes * linter * changes * changes * changes * Category sync cron job enabled (#405) * Mapping added for age group (#408) * 0 SKU id issue fixed for product feed (#411) * 0 SKU id issue fixed * 0 SKU id issue fixed * changes * changes * removing comma * casting store id to string --------- Co-authored-by: Mark Wang <[email protected]> Co-authored-by: Daniel M Connolly <[email protected]> Co-authored-by: singhramanpreett <[email protected]>
1 parent 81e210b commit c1cee22

File tree

4 files changed

+59
-99
lines changed

4 files changed

+59
-99
lines changed

.watchman-cookie-danconnolly-mbp-1492-146

Whitespace-only changes.

app/code/Meta/Sales/Plugin/ShippingData.php

Lines changed: 41 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,25 @@
2020

2121
namespace Meta\Sales\Plugin;
2222

23-
use Magento\OfflineShipping\Model\Carrier\Flatrate;
24-
use Magento\OfflineShipping\Model\Carrier\Freeshipping;
25-
use Magento\OfflineShipping\Model\Carrier\Tablerate;
23+
use Magento\Framework\App\Config\ScopeConfigInterface;
2624
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\CollectionFactory;
27-
use Magento\Shipping\Model\Carrier\AbstractCarrier;
2825

2926
class ShippingData
3027
{
3128
/**
32-
* @var Flatrate
33-
*/
34-
protected Flatrate $flatRateModel;
35-
36-
/**
37-
* @var Tablerate
29+
* @var CollectionFactory
3830
*/
39-
protected Tablerate $tableRateModel;
31+
protected CollectionFactory $tableRateCollection;
4032

4133
/**
42-
* @var Freeshipping
34+
* @var ScopeConfigInterface
4335
*/
44-
protected Freeshipping $freeShippingModel;
36+
protected ScopeConfigInterface $scopeConfig;
4537

4638
/**
47-
* @var CollectionFactory
39+
* @var string
4840
*/
49-
protected CollectionFactory $tableRateCollection;
41+
private string $storeId;
5042

5143
public const ATTR_ENABLED = 'enabled';
5244
public const ATTR_TITLE = 'title';
@@ -58,78 +50,50 @@ class ShippingData
5850
public const ATTR_FREE_SHIPPING_MIN_ORDER_AMOUNT = 'free_shipping_minimum_order_amount';
5951

6052
/**
61-
* @param Flatrate $flatRateModel
62-
* @param Tablerate $tableRateModel
63-
* @param Freeshipping $freeShippingModel
6453
* @param CollectionFactory $tableRateCollection
54+
* @param ScopeConfigInterface $scopeConfig
6555
*/
6656
public function __construct(
67-
Flatrate $flatRateModel,
68-
Tablerate $tableRateModel,
69-
Freeshipping $freeShippingModel,
70-
CollectionFactory $tableRateCollection
57+
CollectionFactory $tableRateCollection,
58+
ScopeConfigInterface $scopeConfig
7159
) {
72-
$this->flatRateModel = $flatRateModel;
73-
$this->tableRateModel = $tableRateModel;
74-
$this->freeShippingModel = $freeShippingModel;
60+
$this->scopeConfig = $scopeConfig;
7561
$this->tableRateCollection = $tableRateCollection;
7662
}
7763

7864
/**
79-
* A function that builds a table rates shipping profile
80-
*
81-
* @return array
82-
*/
83-
public function buildTableRatesProfile(): array
84-
{
85-
return $this->buildShippingProfile($this->tableRateModel, true);
86-
}
87-
88-
/**
89-
* A function that builds a flat-rate shipping profile
90-
*
91-
* @return array
92-
*/
93-
public function buildFlatRateProfile(): array
94-
{
95-
return $this->buildShippingProfile($this->flatRateModel, false);
96-
}
97-
98-
/**
99-
* A function that builds a free shipping profile
65+
* Setter for store id
10066
*
101-
* @return array
67+
* @param string $store_id
68+
* @return void
10269
*/
103-
public function buildFreeShippingProfile(): array
70+
public function setStoreId(string $store_id)
10471
{
105-
return $this->buildShippingProfile($this->freeShippingModel, false);
72+
$this->storeId = $store_id;
10673
}
10774

10875
/**
10976
* Returns shipping profiles based on the AbstractModel carrier passed in
11077
*
111-
* @param AbstractCarrier $model
112-
* @param bool $tableRates
78+
* @param string $shippingProfileType
11379
* @return array
11480
*/
115-
private function buildShippingProfile(AbstractCarrier $model, bool $tableRates): array
81+
public function buildShippingProfile(string $shippingProfileType): array
11682
{
117-
$isEnabled = $model->getConfigData('active');
118-
$title = $model->getConfigData('title');
119-
$methodName = $model->getConfigData('name');
120-
$price = (float)$model->getConfigData("price") ?? 0.0;
121-
$allowedCountries = $model->getConfigData('specificcountry');
122-
if ($tableRates) {
83+
$isEnabled = $this->getFieldFromModel($shippingProfileType, 'active');
84+
$title = $this->getFieldFromModel($shippingProfileType, 'title');
85+
$methodName = $this->getFieldFromModel($shippingProfileType, 'name');
86+
$price = (float)$this->getFieldFromModel($shippingProfileType, 'price') ?? 0.0;
87+
$allowedCountries = $this->getFieldFromModel($shippingProfileType, 'specificcountries');
88+
if ($shippingProfileType === ShippingProfileTypes::TABLE_RATE) {
12389
$shippingMethods = $this->getShippingMethodsInfoForTableRates();
12490
} else {
12591
$shippingMethods = $this->buildShippingMethodsInfo($allowedCountries, $price);
12692
}
127-
128-
$freeShippingThreshold = $model->getConfigData('free_shipping_subtotal');
129-
130-
$handlingFee = $model->getConfigData('handling_fee');
131-
$handlingFeeType = $model->getConfigData('handling_type');
132-
$shippingType = $model->getConfigData('type');
93+
$freeShippingThreshold = $this->getFieldFromModel($shippingProfileType, 'free_shipping_subtotal');
94+
$handlingFee = $this->getFieldFromModel($shippingProfileType, 'handling_fee');
95+
$handlingFeeType = $this->getFieldFromModel($shippingProfileType, 'handling_type');
96+
$shippingType = $this->getFieldFromModel($shippingProfileType, 'type');
13397
return [
13498
self::ATTR_ENABLED => $isEnabled,
13599
self::ATTR_TITLE => $title,
@@ -142,6 +106,19 @@ private function buildShippingProfile(AbstractCarrier $model, bool $tableRates):
142106
];
143107
}
144108

109+
/**
110+
* Get field from abstract carrier DB
111+
*
112+
* @param string $shippingProfileType
113+
* @param string $field
114+
* @return mixed
115+
*/
116+
private function getFieldFromModel(string $shippingProfileType, string $field)
117+
{
118+
$path = 'carriers/' . $shippingProfileType . '/' . $field;
119+
return $this->scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->storeId);
120+
}
121+
145122
/**
146123
* A function that builds shipping methods info for shipping profiles which are not table rates
147124
*

app/code/Meta/Sales/Plugin/ShippingDataFactory.php renamed to app/code/Meta/Sales/Plugin/ShippingProfileTypes.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,12 @@
1818
* limitations under the License.
1919
*/
2020

21-
namespace Meta\Sales\Plugin;
2221

23-
use \Magento\Framework\ObjectManagerInterface;
24-
use Magento\Framework\Exception\FileSystemException;
25-
use Magento\Framework\Filesystem;
22+
namespace Meta\Sales\Plugin;
2623

27-
class ShippingDataFactory
24+
class ShippingProfileTypes
2825
{
29-
/**
30-
* @var ObjectManagerInterface
31-
*/
32-
protected ObjectManagerInterface $objectManager;
33-
34-
/**
35-
* @param ObjectManagerInterface $objectManager
36-
*/
37-
public function __construct(ObjectManagerInterface $objectManager)
38-
{
39-
$this->objectManager = $objectManager;
40-
}
41-
42-
/**
43-
* A function to instantiate the shipping data class
44-
*
45-
* @return mixed
46-
*/
47-
public function create(): mixed
48-
{
49-
return $this->objectManager->create(ShippingData::class);
50-
}
26+
public const TABLE_RATE = 'tablerate';
27+
public const FREE_SHIPPING = 'freeshipping';
28+
public const FLAT_RATE = 'flatrate';
5129
}

app/code/Meta/Sales/Plugin/ShippingSettingsUpdatePlugin.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,35 @@ class ShippingSettingsUpdatePlugin
6262
*/
6363
private StoreManagerInterface $storeManager;
6464

65+
/**
66+
* @var ShippingData
67+
*/
68+
private ShippingData $shippingData;
69+
6570
/**
6671
* Constructor for Shipping settings update plugin
6772
*
68-
* @param ShippingDataFactory $shippingRatesFactory
6973
* @param Filesystem $fileSystem
7074
* @param GraphAPIAdapter $graphApiAdapter
7175
* @param FBEHelper $fbeHelper
7276
* @param SystemConfig $systemConfig
7377
* @param StoreManagerInterface $storeManager
78+
* @param ShippingData $shippingData
7479
*/
7580
public function __construct(
76-
ShippingDataFactory $shippingRatesFactory,
7781
FileSystem $fileSystem,
7882
GraphAPIAdapter $graphApiAdapter,
7983
FBEHelper $fbeHelper,
8084
SystemConfig $systemConfig,
81-
StoreManagerInterface $storeManager
85+
StoreManagerInterface $storeManager,
86+
ShippingData $shippingData,
8287
) {
83-
$this->shippingRatesFactory = $shippingRatesFactory;
8488
$this->fileSystem = $fileSystem;
8589
$this->graphApiAdapter = $graphApiAdapter;
8690
$this->fbeHelper = $fbeHelper;
8791
$this->systemConfig = $systemConfig;
8892
$this->storeManager = $storeManager;
93+
$this->shippingData = $shippingData;
8994
}
9095

9196
/**
@@ -102,12 +107,12 @@ public function afterSave(Config $config): void
102107
}
103108
foreach ($this->storeManager->getStores() as $store) {
104109
try {
105-
$shippingRates = $this->shippingRatesFactory->create();
106110
$fileBuilder = new ShippingFileBuilder($this->fileSystem);
111+
$this->shippingData->setStoreId((string)$store->getId());
107112
$shippingProfiles = [
108-
$shippingRates->buildTableRatesProfile(),
109-
$shippingRates->buildFlatRateProfile(),
110-
$shippingRates->buildFreeShippingProfile()
113+
$this->shippingData->buildShippingProfile(ShippingProfileTypes::TABLE_RATE),
114+
$this->shippingData->buildShippingProfile(ShippingProfileTypes::FLAT_RATE),
115+
$this->shippingData->buildShippingProfile(ShippingProfileTypes::FREE_SHIPPING),
111116
];
112117
$file_uri = $fileBuilder->createFile($shippingProfiles);
113118
$partnerIntegrationId = $this->systemConfig->getCommercePartnerIntegrationId($store->getId());

0 commit comments

Comments
 (0)