Skip to content

Commit 0bbc293

Browse files
danielmetaconnollyathensbirdDaniel M Connollysinghramanpreettzlik
authored
Sync shipping post install (#418)
* Update dependency versions for version 1.2.0 * changes * change * changes * changes * changes * linter * changes * changes * changes * changes * Post FBE onboarding Catalog sync triggered * Added ajx call to trigger all products and category upload after MBE onboarding flow * Unused config removed * 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 * changes * changes * add all * changes * changes * Static errors fixed * static errors fixed * PR comments fixed * updating file * changes * changes --------- Co-authored-by: Mark Wang <[email protected]> Co-authored-by: Daniel M Connolly <[email protected]> Co-authored-by: singhramanpreett <[email protected]> Co-authored-by: Alex Zarichnyi <[email protected]>
1 parent c100f2d commit 0bbc293

File tree

4 files changed

+148
-82
lines changed

4 files changed

+148
-82
lines changed

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

Whitespace-only changes.

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2525
use Meta\Catalog\Helper\CatalogSyncHelper;
2626
use Magento\Backend\App\Action\Context;
27+
use Meta\Sales\Plugin\ShippingSyncer;
2728
use Magento\Framework\Controller\Result\JsonFactory;
2829

2930
class PostFBEOnboardingSync extends AbstractAjax
@@ -47,6 +48,12 @@ class PostFBEOnboardingSync extends AbstractAjax
4748
private $catalogSyncHelper;
4849

4950
/**
51+
* @var ShippingSyncer
52+
*/
53+
private $shippingSyncer;
54+
55+
/**
56+
5057
* Construct
5158
*
5259
* @param Context $context
@@ -60,12 +67,14 @@ public function __construct(
6067
JsonFactory $resultJsonFactory,
6168
FBEHelper $fbeHelper,
6269
SystemConfig $systemConfig,
63-
CatalogSyncHelper $catalogSyncHelper
70+
CatalogSyncHelper $catalogSyncHelper,
71+
ShippingSyncer $shippingSyncer
6472
) {
6573
parent::__construct($context, $resultJsonFactory, $fbeHelper);
6674
$this->fbeHelper = $fbeHelper;
6775
$this->systemConfig = $systemConfig;
6876
$this->catalogSyncHelper = $catalogSyncHelper;
77+
$this->shippingSyncer = $shippingSyncer;
6978
}
7079

7180
/**
@@ -75,7 +84,7 @@ public function __construct(
7584
*/
7685
public function executeForJson(): array
7786
{
78-
$storeId = $this->getRequest()->getParam('storeId');
87+
$storeId = (int)$this->getRequest()->getParam('storeId');
7988
if (!$storeId) {
8089
$response['success'] = false;
8190
$response['message'] = __('StoreId param is not set for Post FBE onboarding sync');
@@ -103,6 +112,7 @@ public function executeForJson(): array
103112
// Immediately after onboarding we initiate full catalog sync.
104113
// It syncs all products and all categories to Meta Catalog
105114
$this->catalogSyncHelper->syncFullCatalog($storeId);
115+
$this->shippingSyncer->syncShippingProfiles();
106116

107117
$response['success'] = true;
108118
$response['message'] = 'Post FBE Onboarding Sync successful';

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

Lines changed: 7 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -22,75 +22,23 @@
2222
namespace Meta\Sales\Plugin;
2323

2424
use Magento\Framework\Exception\FileSystemException;
25-
use Magento\Store\Model\StoreManagerInterface;
26-
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
27-
use Magento\Framework\Filesystem;
28-
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
29-
use Meta\BusinessExtension\Helper\FBEHelper;
3025
use Magento\Config\Model\Config;
31-
use Exception;
3226

3327
class ShippingSettingsUpdatePlugin
3428
{
29+
3530
/**
36-
* @var ShippingDataFactory
31+
* @var ShippingSyncer
3732
*/
38-
protected ShippingDataFactory $shippingRatesFactory;
33+
private ShippingSyncer $shippingSyncer;
3934

4035
/**
41-
* @var FBEHelper
42-
*/
43-
private FBEHelper $fbeHelper;
44-
45-
/**
46-
* @var Filesystem
47-
*/
48-
protected Filesystem $fileSystem;
49-
50-
/**
51-
* @var GraphAPIAdapter
52-
*/
53-
protected $graphApiAdapter;
54-
55-
/**
56-
* @var SystemConfig
57-
*/
58-
protected $systemConfig;
59-
60-
/**
61-
* @var StoreManagerInterface
62-
*/
63-
private StoreManagerInterface $storeManager;
64-
65-
/**
66-
* @var ShippingData
67-
*/
68-
private ShippingData $shippingData;
69-
70-
/**
71-
* Constructor for Shipping settings update plugin
72-
*
73-
* @param Filesystem $fileSystem
74-
* @param GraphAPIAdapter $graphApiAdapter
75-
* @param FBEHelper $fbeHelper
76-
* @param SystemConfig $systemConfig
77-
* @param StoreManagerInterface $storeManager
78-
* @param ShippingData $shippingData
36+
* @param ShippingSyncer $shippingSyncer
7937
*/
8038
public function __construct(
81-
FileSystem $fileSystem,
82-
GraphAPIAdapter $graphApiAdapter,
83-
FBEHelper $fbeHelper,
84-
SystemConfig $systemConfig,
85-
StoreManagerInterface $storeManager,
86-
ShippingData $shippingData,
39+
ShippingSyncer $shippingSyncer
8740
) {
88-
$this->fileSystem = $fileSystem;
89-
$this->graphApiAdapter = $graphApiAdapter;
90-
$this->fbeHelper = $fbeHelper;
91-
$this->systemConfig = $systemConfig;
92-
$this->storeManager = $storeManager;
93-
$this->shippingData = $shippingData;
41+
$this->shippingSyncer = $shippingSyncer;
9442
}
9543

9644
/**
@@ -105,27 +53,6 @@ public function afterSave(Config $config): void
10553
if ($section_name !== 'carriers') {
10654
return;
10755
}
108-
foreach ($this->storeManager->getStores() as $store) {
109-
try {
110-
$fileBuilder = new ShippingFileBuilder($this->fileSystem);
111-
$this->shippingData->setStoreId((string)$store->getId());
112-
$shippingProfiles = [
113-
$this->shippingData->buildShippingProfile(ShippingProfileTypes::TABLE_RATE),
114-
$this->shippingData->buildShippingProfile(ShippingProfileTypes::FLAT_RATE),
115-
$this->shippingData->buildShippingProfile(ShippingProfileTypes::FREE_SHIPPING),
116-
];
117-
$file_uri = $fileBuilder->createFile($shippingProfiles);
118-
$partnerIntegrationId = $this->systemConfig->getCommercePartnerIntegrationId($store->getId());
119-
$this->graphApiAdapter->setDebugMode($this->systemConfig->isDebugMode($store->getId()))
120-
->setAccessToken($this->systemConfig->getAccessToken($store->getId()));
121-
$this->graphApiAdapter->uploadFile($partnerIntegrationId, $file_uri, "SHIPPING_PROFILES", "CREATE");
122-
} catch (Exception $e) {
123-
$this->fbeHelper->logExceptionImmediatelyToMeta($e, [
124-
'store_id' => $this->fbeHelper->getStore()->getId(),
125-
'event' => 'shipping_profile_sync',
126-
'event_type' => 'after_save'
127-
]);
128-
}
129-
}
56+
$this->shippingSyncer->syncShippingProfiles();
13057
}
13158
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Sales\Plugin;
22+
23+
use Magento\Framework\Exception\FileSystemException;
24+
use Magento\Framework\Filesystem;
25+
use Magento\Store\Model\StoreManagerInterface;
26+
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
27+
use Meta\BusinessExtension\Helper\FBEHelper;
28+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
29+
30+
class ShippingSyncer
31+
{
32+
33+
/**
34+
* @var ShippingDataFactory
35+
*/
36+
protected ShippingDataFactory $shippingRatesFactory;
37+
38+
/**
39+
* @var FBEHelper
40+
*/
41+
private FBEHelper $fbeHelper;
42+
43+
/**
44+
* @var Filesystem
45+
*/
46+
protected Filesystem $fileSystem;
47+
48+
/**
49+
* @var GraphAPIAdapter
50+
*/
51+
protected $graphApiAdapter;
52+
53+
/**
54+
* @var SystemConfig
55+
*/
56+
protected $systemConfig;
57+
58+
/**
59+
* @var StoreManagerInterface
60+
*/
61+
private StoreManagerInterface $storeManager;
62+
63+
/**
64+
* @var ShippingData
65+
*/
66+
private ShippingData $shippingData;
67+
68+
/**
69+
* Constructor for Shipping settings update plugin
70+
*
71+
* @param Filesystem $fileSystem
72+
* @param GraphAPIAdapter $graphApiAdapter
73+
* @param FBEHelper $fbeHelper
74+
* @param SystemConfig $systemConfig
75+
* @param StoreManagerInterface $storeManager
76+
* @param ShippingData $shippingData
77+
*/
78+
public function __construct(
79+
FileSystem $fileSystem,
80+
GraphAPIAdapter $graphApiAdapter,
81+
FBEHelper $fbeHelper,
82+
SystemConfig $systemConfig,
83+
StoreManagerInterface $storeManager,
84+
ShippingData $shippingData,
85+
) {
86+
$this->fileSystem = $fileSystem;
87+
$this->graphApiAdapter = $graphApiAdapter;
88+
$this->fbeHelper = $fbeHelper;
89+
$this->systemConfig = $systemConfig;
90+
$this->storeManager = $storeManager;
91+
$this->shippingData = $shippingData;
92+
}
93+
94+
/**
95+
* Syncing shipping profiles to Meta
96+
*
97+
* @param string $access_token
98+
* @param string $partner_integration_id
99+
* @return void
100+
* @throws FileSystemException
101+
*/
102+
public function syncShippingProfiles(string $access_token = null, string $partner_integration_id = null)
103+
{
104+
foreach ($this->storeManager->getStores() as $store) {
105+
try {
106+
$fileBuilder = new ShippingFileBuilder($this->fileSystem);
107+
$this->shippingData->setStoreId($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+
$file_uri = $fileBuilder->createFile($shippingProfiles);
114+
$access_token = $access_token ?? $this->systemConfig->getAccessToken($store->getId());
115+
$partner_integration_id = $partner_integration_id ??
116+
$this->systemConfig->getCommercePartnerIntegrationId($store->getId());
117+
$this->graphApiAdapter->setDebugMode($this->systemConfig->isDebugMode($store->getId()))
118+
->setAccessToken($access_token);
119+
$this->graphApiAdapter->uploadFile($partner_integration_id, $file_uri, "SHIPPING_PROFILES", "CREATE");
120+
} catch (Exception $e) {
121+
$this->fbeHelper->logExceptionImmediatelyToMeta($e, [
122+
'store_id' => $this->fbeHelper->getStore()->getId(),
123+
'event' => 'shipping_profile_sync',
124+
'event_type' => 'after_save'
125+
]);
126+
}
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)