Skip to content

Commit 302d156

Browse files
authored
Save onsite eligible flag and use it to decide if running the Onsite shop features (#843)
* Address linter warning suggestions * Only sync orders if: Order sync enabled AND Extension enabled(included in 1) AND There's any shop feature installed(fb_shop, page_shop, ig_shopping) and active * Split format change into a different PR * Bring back int casting * When saving fbeConfig, also save the onsite eligibility flag * Save onsite eligible flag after onboarding * Persist onsite flag * Checking the onsite flag before syncing orders * Cleanup unused methods * Merge with main * Only sync orders if: Order sync enabled AND Extension enabled(included in 1) AND There's any shop feature installed(fb_shop, page_shop, ig_shopping) and active * Merge with main * When saving fbeConfig, also save the onsite eligibility flag * Save onsite eligible flag after onboarding * Persist onsite flag * Checking the onsite flag before syncing orders * Cleanup unused methods * Cleanup * Fix format * Revert some non-functional cleanup changes
1 parent e4449fe commit 302d156

File tree

10 files changed

+77
-45
lines changed

10 files changed

+77
-45
lines changed

app/code/Meta/BusinessExtension/Block/Adminhtml/Setup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function getExternalBusinessId($storeId)
263263
*
264264
* @return mixed
265265
*/
266-
public function fetchConfigurationAjaxRoute()
266+
public function persistConfigurationAjaxRoute()
267267
{
268268
return $this->fbeHelper->getUrl('fbeadmin/ajax/persistConfiguration');
269269
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ private function deleteConfigKeys($storeId)
180180
)
181181
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_FEED_ID, $storeId)
182182
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_API_VERSION, $storeId)
183-
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_API_VERSION_LAST_UPDATE, $storeId);
183+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_API_VERSION_LAST_UPDATE, $storeId)
184+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_IS_ONSITE_ELIGIBLE, $storeId);
184185

185186
return $this;
186187
}

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

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace Meta\BusinessExtension\Controller\Adminhtml\Ajax;
2222

23+
use GuzzleHttp\Exception\GuzzleException;
2324
use Meta\BusinessExtension\Helper\FBEHelper;
2425
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
2526
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
@@ -47,17 +48,17 @@ class PersistConfiguration extends AbstractAjax
4748
/**
4849
* Construct
4950
*
50-
* @param Context $context
51-
* @param JsonFactory $resultJsonFactory
52-
* @param FBEHelper $fbeHelper
53-
* @param SystemConfig $systemConfig
51+
* @param Context $context
52+
* @param JsonFactory $resultJsonFactory
53+
* @param FBEHelper $fbeHelper
54+
* @param SystemConfig $systemConfig
5455
* @param GraphAPIAdapter $graphApiAdapter
5556
*/
5657
public function __construct(
57-
Context $context,
58-
JsonFactory $resultJsonFactory,
59-
FBEHelper $fbeHelper,
60-
SystemConfig $systemConfig,
58+
Context $context,
59+
JsonFactory $resultJsonFactory,
60+
FBEHelper $fbeHelper,
61+
SystemConfig $systemConfig,
6162
GraphAPIAdapter $graphApiAdapter
6263
) {
6364
parent::__construct($context, $resultJsonFactory, $fbeHelper);
@@ -81,12 +82,13 @@ public function executeForJson()
8182
$catalogId = $this->getRequest()->getParam('catalogId');
8283
$pageId = $this->getRequest()->getParam('pageId');
8384
$commercePartnerIntegrationId = $this->getRequest()->getParam('commercePartnerIntegrationId');
85+
$isOnsiteEligible = $this->getRequest()->getParam('isOnsiteEligible') === 'true';
8486

8587
$this->saveExternalBusinessId($externalBusinessId, $storeId)
8688
->saveCatalogId($catalogId, $storeId)
8789
->saveCommercePartnerIntegrationId($commercePartnerIntegrationId, $storeId)
8890
->saveInstalledFlag($storeId)
89-
->completeOnsiteOnboarding($accessToken, $pageId, $storeId)
91+
->completeOnsiteOnboarding($accessToken, $pageId, $storeId, $isOnsiteEligible)
9092
->enableCatalogSync($commercePartnerIntegrationId, $storeId);
9193

9294
$response['success'] = true;
@@ -110,8 +112,8 @@ public function executeForJson()
110112
/**
111113
* Save catalog id
112114
*
113-
* @param int $catalogId
114-
* @param int $storeId
115+
* @param int $catalogId
116+
* @param int $storeId
115117
* @return $this
116118
*/
117119
public function saveCatalogId($catalogId, $storeId)
@@ -122,16 +124,16 @@ public function saveCatalogId($catalogId, $storeId)
122124
$catalogId,
123125
$storeId
124126
);
125-
$this->fbeHelper->log('Catalog ID saved on instance --- '. $catalogId);
127+
$this->fbeHelper->log('Catalog ID saved on instance --- ' . $catalogId);
126128
}
127129
return $this;
128130
}
129131

130132
/**
131133
* Save commerce partner integration id
132134
*
133-
* @param int $commercePartnerIntegrationId
134-
* @param int $storeId
135+
* @param int $commercePartnerIntegrationId
136+
* @param int $storeId
135137
* @return $this
136138
*/
137139
public function saveCommercePartnerIntegrationId($commercePartnerIntegrationId, $storeId)
@@ -153,8 +155,8 @@ public function saveCommercePartnerIntegrationId($commercePartnerIntegrationId,
153155
/**
154156
* Based on commerce PI presence it enables catalog sync.
155157
*
156-
* @param int $commercePartnerIntegrationId
157-
* @param int $storeId
158+
* @param int $commercePartnerIntegrationId
159+
* @param int $storeId
158160
* @return $this
159161
*/
160162
public function enableCatalogSync($commercePartnerIntegrationId, $storeId)
@@ -174,8 +176,8 @@ public function enableCatalogSync($commercePartnerIntegrationId, $storeId)
174176
/**
175177
* Save external business id
176178
*
177-
* @param int $externalBusinessId
178-
* @param int $storeId
179+
* @param int $externalBusinessId
180+
* @param int $storeId
179181
* @return $this
180182
*/
181183
public function saveExternalBusinessId($externalBusinessId, $storeId)
@@ -186,20 +188,20 @@ public function saveExternalBusinessId($externalBusinessId, $storeId)
186188
$externalBusinessId,
187189
$storeId
188190
);
189-
$this->fbeHelper->log('External business ID saved on instance --- '. $externalBusinessId);
191+
$this->fbeHelper->log('External business ID saved on instance --- ' . $externalBusinessId);
190192
}
191193
return $this;
192194
}
193195

194196
/**
195197
* Update install flag to true and save
196198
*
197-
* @param int $storeId
199+
* @param int $storeId
198200
* @return $this
199201
*/
200202
public function saveInstalledFlag($storeId)
201203
{
202-
// set installed to true
204+
// set installed to true
203205
$this->systemConfig->saveConfig(
204206
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_INSTALLED,
205207
true,
@@ -211,15 +213,16 @@ public function saveInstalledFlag($storeId)
211213
/**
212214
* Complete onsite onboarding
213215
*
214-
* @param string $accessToken
215-
* @param int $pageId
216-
* @param int $storeId
216+
* @param string $accessToken
217+
* @param int $pageId
218+
* @param int $storeId
219+
* @param bool $isOnsiteEligible
217220
* @return $this
218221
* @throws LocalizedException
219-
* @throws \GuzzleHttp\Exception\GuzzleException
222+
* @throws GuzzleException
220223
*/
221224

222-
public function completeOnsiteOnboarding($accessToken, $pageId, $storeId)
225+
public function completeOnsiteOnboarding($accessToken, $pageId, $storeId, $isOnsiteEligible)
223226
{
224227
if (!$accessToken) {
225228
$this->fbeHelper->log('No access token available, skipping onboarding to onsite checkout');
@@ -237,7 +240,7 @@ public function completeOnsiteOnboarding($accessToken, $pageId, $storeId)
237240
$pageId,
238241
$storeId
239242
);
240-
$this->fbeHelper->log('Page ID saved on instance --- '. $pageId);
243+
$this->fbeHelper->log('Page ID saved on instance --- ' . $pageId);
241244

242245
// retrieve page access token
243246
$pageAccessToken = $this->graphApiAdapter->getPageAccessToken($accessToken, $pageId);
@@ -266,6 +269,13 @@ public function completeOnsiteOnboarding($accessToken, $pageId, $storeId)
266269
$storeId
267270
);
268271

272+
// save if onsite eligible
273+
$this->systemConfig->saveConfig(
274+
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_IS_ONSITE_ELIGIBLE,
275+
$isOnsiteEligible ? 1 : 0,
276+
$storeId
277+
);
278+
269279
// enable API integration
270280
$this->graphApiAdapter->associateMerchantSettingsWithApp($commerceAccountId, $accessToken);
271281

app/code/Meta/BusinessExtension/Model/MBEInstalls.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,23 @@ public function saveCommercePartnerIntegrationId($commercePartnerIntegrationId,
285285
* @param int $storeId
286286
* @return $this
287287
*/
288-
public function saveIsOnsiteEligible($onsite_eligible, $storeId)
288+
public function saveIsOnsiteEligible(bool $onsite_eligible, int $storeId): MBEInstalls
289289
{
290290
if ($onsite_eligible) {
291-
$this->systemConfig->saveConfig(
292-
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_IS_ONSITE_ELIGIBLE,
293-
1,
294-
$storeId
295-
);
296291
$this->fbeHelper->log(
297292
"Mark storeID: {$storeId} as Onsite eligible"
298293
);
294+
} else {
295+
$this->fbeHelper->log(
296+
"Mark storeID: {$storeId} as Onsite ineligible"
297+
);
299298
}
299+
300+
$this->systemConfig->saveConfig(
301+
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_IS_ONSITE_ELIGIBLE,
302+
$onsite_eligible ? 1 : 0,
303+
$storeId
304+
);
300305
return $this;
301306
}
302307

app/code/Meta/BusinessExtension/Model/System/Config.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020

2121
namespace Meta\BusinessExtension\Model\System;
2222

23-
use Exception;
2423
use Magento\Config\Model\ResourceModel\Config as ResourceConfig;
25-
use Magento\Framework\App\Cache\TypeListInterface;
2624
use Magento\Framework\App\CacheInterface;
2725
use Magento\Framework\App\Config as AppConfig;
2826
use Magento\Framework\App\Config\ScopeConfigInterface;
@@ -1066,6 +1064,17 @@ public function isFBEShopInstalled(int $storeId = null): bool
10661064
$this->isFeatureInstalled('page_shop', $storeId);
10671065
}
10681066

1067+
/**
1068+
* Check if installed shop eligible for onsite features
1069+
*
1070+
* @param int|null $storeId
1071+
* @return bool
1072+
*/
1073+
public function isInstalledShopOnsiteEligible(int $storeId = null): bool
1074+
{
1075+
return $this->getConfig(self::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_IS_ONSITE_ELIGIBLE, $storeId);
1076+
}
1077+
10691078
/**
10701079
* Save the External Business ID
10711080
*

app/code/Meta/BusinessExtension/view/adminhtml/templates/setup.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use Magento\Framework\Escaper;
6161
pixelId: '<?= $escaper->escapeHtml($block->fetchPixelId($block->getSelectedStoreId())) ?>',
6262
popupOrigin: '<?= $escaper->escapeHtml($block->getPopupOrigin()) ?>',
6363
postFBEOnboardingSync: '<?= $escaper->escapeHtml($block->fetchPostFBEOnboardingSyncAjaxRoute()) ?>',
64-
saveConfig: '<?= $escaper->escapeHtml($block->fetchConfigurationAjaxRoute()) ?>',
64+
saveConfig: '<?= $escaper->escapeHtml($block->persistConfigurationAjaxRoute()) ?>',
6565
setAAMSettings: '<?= $escaper->escapeHtml($block->getAAMSettingsRoute()) ?>',
6666
setAccessToken: '<?= $escaper->escapeHtml($block->getAccessTokenAjaxRoute()) ?>',
6767
setInstalledFeatures: '<?= $escaper->escapeHtml($block->getInstalledFeaturesAjaxRouteUrl()) ?>',

app/code/Meta/BusinessExtension/view/adminhtml/web/js/fbe_allinone.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,15 @@ jQuery(document).ready(function () {
101101
const profiles = message.profiles;
102102
const catalogId = message.catalog_id;
103103
const commercePartnerIntegrationId = message.commerce_partner_integration_id;
104+
const isOnsiteEligible = message.onsite_eligible;
104105
const pageId = message.page_id;
105106
const installedFeatures = message.installed_features;
106107

107108
_this.savePixelId(pixelId);
108109
_this.saveAccessToken(accessToken);
109110
_this.saveProfilesData(profiles);
110111
_this.saveAAMSettings(pixelId);
111-
_this.saveConfig(accessToken, catalogId, pageId, commercePartnerIntegrationId);
112+
_this.saveConfig(accessToken, catalogId, pageId, commercePartnerIntegrationId, isOnsiteEligible);
112113
_this.saveInstalledFeatures(installedFeatures);
113114
_this.cleanConfigCache();
114115
_this.postFBEOnboardingSync();
@@ -272,7 +273,7 @@ jQuery(document).ready(function () {
272273
}
273274
});
274275
},
275-
saveConfig: function saveConfig(accessToken, catalogId, pageId, commercePartnerIntegrationId)
276+
saveConfig: function saveConfig(accessToken, catalogId, pageId, commercePartnerIntegrationId, isOnsiteEligible)
276277
{
277278
const _this = this;
278279
jQuery.ajax({
@@ -285,6 +286,7 @@ jQuery(document).ready(function () {
285286
pageId: pageId,
286287
accessToken: accessToken,
287288
commercePartnerIntegrationId: commercePartnerIntegrationId,
289+
isOnsiteEligible: isOnsiteEligible,
288290
storeId: window.facebookBusinessExtensionConfig.storeId,
289291
}),
290292
success: function onSuccess(data, _textStatus, _jqXHR)

app/code/Meta/BusinessExtension/view/adminhtml/web/js/lib/fbe.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,15 @@ jQuery(document).ready(function () {
100100
const profiles = message.profiles;
101101
const catalogId = message.catalog_id;
102102
const commercePartnerIntegrationId = message.commerce_partner_integration_id;
103+
const isOnsiteEligible = message.onsite_eligible;
103104
const pageId = message.page_id;
104105
const installedFeatures = message.installed_features;
105106

106107
_this.savePixelId(pixelId);
107108
_this.saveAccessToken(accessToken);
108109
_this.saveProfilesData(profiles);
109110
_this.saveAAMSettings(pixelId);
110-
_this.saveConfig(accessToken, catalogId, pageId, commercePartnerIntegrationId);
111+
_this.saveConfig(accessToken, catalogId, pageId, commercePartnerIntegrationId, isOnsiteEligible);
111112
_this.saveInstalledFeatures(installedFeatures);
112113
_this.cleanConfigCache();
113114
_this.postFBEOnboardingSync();
@@ -271,7 +272,7 @@ jQuery(document).ready(function () {
271272
}
272273
});
273274
},
274-
saveConfig: function saveConfig(accessToken, catalogId, pageId, commercePartnerIntegrationId)
275+
saveConfig: function saveConfig(accessToken, catalogId, pageId, commercePartnerIntegrationId, isOnsiteEligible)
275276
{
276277
const _this = this;
277278
jQuery.ajax({
@@ -284,6 +285,7 @@ jQuery(document).ready(function () {
284285
pageId: pageId,
285286
accessToken: accessToken,
286287
commercePartnerIntegrationId: commercePartnerIntegrationId,
288+
isOnsiteEligible: isOnsiteEligible,
287289
storeId: window.facebookBusinessExtensionConfig.storeId,
288290
}),
289291
success: function onSuccess(data, _textStatus, _jqXHR)

app/code/Meta/Sales/Cron/SyncOrders.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ private function pullOrdersForStore(int $storeId): void
7575
// 1. Order sync enabled
7676
// 2. Extension enabled(included in 1)
7777
// 3. There's any shop feature installed(fb_shop, page_shop, ig_shopping) and active
78+
// 4. The installed shop is eligible for onsite features
7879
if (!($this->systemConfig->isOrderSyncEnabled($storeId)
79-
&& $this->systemConfig->isFBEShopInstalled($storeId))) {
80+
&& $this->systemConfig->isFBEShopInstalled($storeId)
81+
&& $this->systemConfig->isInstalledShopOnsiteEligible($storeId))) {
8082
return;
8183
}
8284

app/code/Meta/Sales/Cron/SyncRefundsAndCancellations.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ private function pullRefundsAndCancellationsForStore(int $storeId)
8383
// 1. Order sync enabled
8484
// 2. Extension enabled(included in 1)
8585
// 3. There's any shop feature installed(fb_shop, page_shop, ig_shopping) and active
86+
// 4. The installed shop is eligible for onsite features
8687
if (!($this->systemConfig->isOrderSyncEnabled($storeId)
87-
&& $this->systemConfig->isFBEShopInstalled($storeId))
88+
&& $this->systemConfig->isFBEShopInstalled($storeId)
89+
&& $this->systemConfig->isInstalledShopOnsiteEligible($storeId))
8890
) {
8991
return;
9092
}
91-
9293
$this->commerceHelper->pullRefundOrders($storeId);
9394
$this->commerceHelper->pullCancelledOrders($storeId);
9495
}

0 commit comments

Comments
 (0)