Skip to content

Commit 1c3c78d

Browse files
danielmetaconnollyDaniel M Connolly
andauthored
Cleaning up Shipping syncing code (#420)
* changes * changes * changes --------- Co-authored-by: Daniel M Connolly <[email protected]>
1 parent 5e50889 commit 1c3c78d

File tree

3 files changed

+58
-36
lines changed

3 files changed

+58
-36
lines changed

app/code/Meta/BusinessExtension/Controller/Adminhtml/Ajax/PostFBEOnboardingSync.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ class PostFBEOnboardingSync extends AbstractAjax
6363
* @param ShippingSyncer $shippingSyncer
6464
*/
6565
public function __construct(
66-
Context $context,
67-
JsonFactory $resultJsonFactory,
68-
FBEHelper $fbeHelper,
69-
SystemConfig $systemConfig,
66+
Context $context,
67+
JsonFactory $resultJsonFactory,
68+
FBEHelper $fbeHelper,
69+
SystemConfig $systemConfig,
7070
CatalogSyncHelper $catalogSyncHelper,
71-
ShippingSyncer $shippingSyncer
71+
ShippingSyncer $shippingSyncer
7272
) {
7373
parent::__construct($context, $resultJsonFactory, $fbeHelper);
7474
$this->fbeHelper = $fbeHelper;
@@ -93,7 +93,8 @@ public function executeForJson(): array
9393
}
9494

9595
try {
96-
$storeName = $this->systemConfig->getStoreManager()->getStore($storeId)->getName();
96+
$store = $this->systemConfig->getStoreManager()->getStore($storeId);
97+
$storeName = $store->getName();
9798
if (!$this->systemConfig->getAccessToken($storeId)) {
9899
$response['success'] = false;
99100
$response['message'] = __(
@@ -112,7 +113,7 @@ public function executeForJson(): array
112113
// Immediately after onboarding we initiate full catalog sync.
113114
// It syncs all products and all categories to Meta Catalog
114115
$this->catalogSyncHelper->syncFullCatalog($storeId);
115-
$this->shippingSyncer->syncShippingProfiles();
116+
$this->shippingSyncer->syncShippingProfiles('post_fbe_onboarding', $store);
116117

117118
$response['success'] = true;
118119
$response['message'] = 'Post FBE Onboarding Sync successful';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ public function afterSave(Config $config): void
5050
if ($sectionName !== 'carriers') {
5151
return;
5252
}
53-
$this->shippingSyncer->syncShippingProfiles();
53+
$this->shippingSyncer->syncShippingProfiles('after_save');
5454
}
5555
}

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

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
use Exception;
2424
use Magento\Framework\Filesystem;
25+
use Magento\Store\Api\Data\StoreInterface;
2526
use Magento\Store\Model\StoreManagerInterface;
2627
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
2728
use Meta\BusinessExtension\Helper\FBEHelper;
@@ -96,39 +97,59 @@ public function __construct(
9697
/**
9798
* Syncing shipping profiles to Meta
9899
*
99-
* @param string|null $accessToken
100-
* @param string|null $partnerIntegrationId
100+
* @param string $event_type
101+
* @param StoreInterface|null $store
101102
* @return void
102103
*/
103-
public function syncShippingProfiles(string $accessToken = null, string $partnerIntegrationId = null)
104+
public function syncShippingProfiles(string $event_type, StoreInterface $store = null)
104105
{
106+
if ($store !== null) {
107+
$this->syncShippingProfilesForStore($event_type, $store);
108+
return;
109+
}
105110
foreach ($this->storeManager->getStores() as $store) {
106-
try {
107-
$this->shippingData->setStoreId((int)$store->getId());
108-
$shippingProfiles = [
109-
$this->shippingData->buildShippingProfile(ShippingProfileTypes::TABLE_RATE),
110-
$this->shippingData->buildShippingProfile(ShippingProfileTypes::FLAT_RATE),
111-
$this->shippingData->buildShippingProfile(ShippingProfileTypes::FREE_SHIPPING),
112-
];
113-
$fileUri = $this->shippingFileBuilder->createFile($shippingProfiles);
114-
$accessToken = $accessToken ?? $this->systemConfig->getAccessToken($store->getId());
115-
$partnerIntegrationId = $partnerIntegrationId ??
116-
$this->systemConfig->getCommercePartnerIntegrationId($store->getId());
117-
$this->graphApiAdapter->setDebugMode($this->systemConfig->isDebugMode($store->getId()))
118-
->setAccessToken($accessToken);
119-
$this->graphApiAdapter->uploadFile(
120-
$partnerIntegrationId,
121-
$fileUri,
122-
'SHIPPING_PROFILES',
123-
'CREATE'
124-
);
125-
} catch (Exception $e) {
126-
$this->fbeHelper->logExceptionImmediatelyToMeta($e, [
127-
'store_id' => $this->fbeHelper->getStore()->getId(),
128-
'event' => 'shipping_profile_sync',
129-
'event_type' => 'after_save'
130-
]);
111+
$this->syncShippingProfilesForStore($event_type, $store);
112+
}
113+
}
114+
115+
/**
116+
* Syncing shipping profiles for an individual store
117+
*
118+
* @param string $event_type
119+
* @param StoreInterface $store
120+
* @return void
121+
*/
122+
private function syncShippingProfilesForStore(string $event_type, StoreInterface $store)
123+
{
124+
try {
125+
$accessToken = $accessToken ?? $this->systemConfig->getAccessToken($store->getId());
126+
if ($accessToken === null) {
127+
return;
131128
}
129+
$this->shippingData->setStoreId((int)$store->getId());
130+
$shippingProfiles = [
131+
$this->shippingData->buildShippingProfile(ShippingProfileTypes::TABLE_RATE),
132+
$this->shippingData->buildShippingProfile(ShippingProfileTypes::FLAT_RATE),
133+
$this->shippingData->buildShippingProfile(ShippingProfileTypes::FREE_SHIPPING),
134+
];
135+
$fileUri = $this->shippingFileBuilder->createFile($shippingProfiles);
136+
$accessToken = $accessToken ?? $this->systemConfig->getAccessToken($store->getId());
137+
$partnerIntegrationId = $partnerIntegrationId ??
138+
$this->systemConfig->getCommercePartnerIntegrationId($store->getId());
139+
$this->graphApiAdapter->setDebugMode($this->systemConfig->isDebugMode($store->getId()))
140+
->setAccessToken($accessToken);
141+
$this->graphApiAdapter->uploadFile(
142+
$partnerIntegrationId,
143+
$fileUri,
144+
'SHIPPING_PROFILES',
145+
'CREATE'
146+
);
147+
} catch (Exception $e) {
148+
$this->fbeHelper->logExceptionImmediatelyToMeta($e, [
149+
'store_id' => $this->fbeHelper->getStore()->getId(),
150+
'event' => 'shipping_profile_sync',
151+
'event_type' => $event_type
152+
]);
132153
}
133154
}
134155
}

0 commit comments

Comments
 (0)