Skip to content

Commit fb60534

Browse files
Meta Catalog integration toggle button added and catalog configuration options removed from UI (#261)
* Catalog integratin toggle button added * Catalog sync options removed from UI and made default * data migration patch added for updated configurations * test failures fixed * code formatted * failed static tests fixed * failed static tests fixed * Null check improve for data patch * Save Event observer test case fixed --------- Co-authored-by: Alex Zarichnyi <[email protected]>
1 parent 139d8e6 commit fb60534

15 files changed

+225
-98
lines changed

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

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ class Config
5757
public const XML_PATH_FACEBOOK_BUSINESS_EXTENSION_FEED_ID = 'facebook/business_extension/feed_id';
5858
public const XML_PATH_FACEBOOK_BUSINESS_EXTENSION_OFFERS_FEED_ID = 'facebook/business_extension/offers_feed_id';
5959
private const XML_PATH_FACEBOOK_BUSINESS_EXTENSION_STORE = 'facebook/business_extension/store';
60-
61-
private const XML_PATH_FACEBOOK_DAILY_PRODUCT_FEED = 'facebook/catalog_management/daily_product_feed';
60+
private const XML_PATH_FACEBOOK_ENABLE_CATALOG_SYNC = 'facebook/catalog_management/enable_catalog_sync';
6261
private const XML_PATH_FACEBOOK_PRODUCT_IDENTIFIER = 'facebook/catalog_management/product_identifier';
6362
private const XML_PATH_FACEBOOK_PRICE_INCL_TAX = 'facebook/catalog_management/price_incl_tax';
6463
private const XML_PATH_FACEBOOK_COLLECTIONS_SYNC_IS_ACTIVE = 'facebook/catalog_management/collections_sync';
@@ -70,16 +69,11 @@ class Config
7069
public const XML_PATH_FACEBOOK_BUSINESS_EXTENSION_ACCESS_TOKEN = 'facebook/business_extension/access_token';
7170
public const XML_PATH_FACEBOOK_BUSINESS_EXTENSION_PAGE_ACCESS_TOKEN =
7271
'facebook/business_extension/page_access_token';
73-
74-
private const XML_PATH_FACEBOOK_BUSINESS_EXTENSION_INCREMENTAL_PRODUCT_UPDATES =
75-
'facebook/catalog_management/incremental_product_updates';
76-
77-
private const XML_PATH_FACEBOOK_ENABLE_INVENTORY_UPLOAD = 'facebook/inventory_management/enable_inventory_upload';
7872
private const XML_PATH_FACEBOOK_USE_MULTI_SOURCE_INVENTORY =
7973
'facebook/inventory_management/use_multi_source_inventory';
8074
private const XML_PATH_FACEBOOK_INVENTORY_STOCK = 'facebook/inventory_management/inventory_stock';
8175
private const XML_PATH_FACEBOOK_BUSINESS_EXTENSION_OUT_OF_STOCK_THRESHOLD =
82-
'facebook/inventory_management/out_of_stock_threshold';
76+
'facebook/catalog_management/out_of_stock_threshold';
8377

8478
public const XML_PATH_FACEBOOK_ORDERS_SYNC_ACTIVE = 'facebook/orders_sync/active';
8579
public const XML_PATH_FACEBOOK_ORDERS_SYNC_DEFAULT_ORDER_STATUS = 'facebook/orders_sync/default_order_status';
@@ -325,34 +319,6 @@ public function getStoreId()
325319
return $this->storeManager->getStore()->getId();
326320
}
327321

328-
/**
329-
* Is active incremental product updates
330-
*
331-
* @param int $scopeId
332-
* @param int $scope
333-
* @return bool
334-
*/
335-
public function isActiveIncrementalProductUpdates($scopeId = null, $scope = null): bool
336-
{
337-
return (bool)$this->getConfig(
338-
self::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_INCREMENTAL_PRODUCT_UPDATES,
339-
$scopeId,
340-
$scope
341-
);
342-
}
343-
344-
/**
345-
* Is active inventory upload
346-
*
347-
* @param int $scopeId
348-
* @param int $scope
349-
* @return bool
350-
*/
351-
public function isActiveInventoryUpload($scopeId = null, $scope = null): bool
352-
{
353-
return (bool)$this->getConfig(self::XML_PATH_FACEBOOK_ENABLE_INVENTORY_UPLOAD, $scopeId, $scope);
354-
}
355-
356322
/**
357323
* Use multi source inventory
358324
*
@@ -756,15 +722,15 @@ public function getShippingMethodsMap($storeId = null): array
756722
}
757723

758724
/**
759-
* Is active daily product feed
725+
* Is catalog sync enabled
760726
*
761727
* @param int $scopeId
762728
* @param string $scope
763729
* @return bool
764730
*/
765-
public function isActiveDailyProductFeed($scopeId = null, $scope = ScopeInterface::SCOPE_STORES): bool
731+
public function isCatalogSyncEnabled($scopeId = null, $scope = ScopeInterface::SCOPE_STORES): bool
766732
{
767-
return (bool)$this->getConfig(self::XML_PATH_FACEBOOK_DAILY_PRODUCT_FEED, $scopeId, $scope);
733+
return (bool)$this->getConfig(self::XML_PATH_FACEBOOK_ENABLE_CATALOG_SYNC, $scopeId, $scope);
768734
}
769735

770736
/**

app/code/Meta/Catalog/Cron/UploadInventory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ private function uploadForStore($storeId)
7979
*/
8080
private function isUploadEnabled($storeId)
8181
{
82-
if (!$this->systemConfig->isActiveExtension($storeId)) {
83-
return false;
84-
}
85-
return $this->systemConfig->isActiveInventoryUpload($storeId);
82+
return $this->systemConfig->isActiveExtension($storeId)
83+
&& $this->systemConfig->isCatalogSyncEnabled($storeId);
8684
}
8785

8886
/**

app/code/Meta/Catalog/Cron/UploadProductFeed.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ private function uploadForStore($storeId)
7878
*/
7979
private function isFeedUploadEnabled($storeId)
8080
{
81-
if (!$this->systemConfig->isActiveExtension($storeId)) {
82-
return false;
83-
}
84-
return $this->systemConfig->isActiveDailyProductFeed($storeId);
81+
return $this->systemConfig->isActiveExtension($storeId)
82+
&& $this->systemConfig->isCatalogSyncEnabled($storeId);
8583
}
8684

8785
/**

app/code/Meta/Catalog/Model/Product/Feed/Method/FeedApi.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ private function writeFile(WriteInterface $fileStream)
165165

166166
$total = 0;
167167
foreach ($this->productRetrievers as $productRetriever) {
168+
168169
$productRetriever->setStoreId($this->storeId);
169170
$offset = 0;
170171
$limit = $productRetriever->getLimit();

app/code/Meta/Catalog/Observer/ProcessCategoryAfterDeleteEventObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function __construct(
6060
*/
6161
public function execute(Observer $observer)
6262
{
63-
if (!$this->systemConfig->isActiveIncrementalProductUpdates()) {
63+
if (!$this->systemConfig->isCatalogSyncEnabled()) {
6464
return;
6565
}
6666
$category = $observer->getEvent()->getCategory();

app/code/Meta/Catalog/Observer/ProcessCategoryAfterSaveEventObserver.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
namespace Meta\Catalog\Observer;
2222

2323
use Meta\BusinessExtension\Helper\FBEHelper;
24+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2425
use Meta\Catalog\Model\Feed\CategoryCollection;
2526
use Magento\Framework\Event\Observer;
2627
use Magento\Framework\Event\ObserverInterface;
@@ -32,14 +33,20 @@ class ProcessCategoryAfterSaveEventObserver implements ObserverInterface
3233
*/
3334
private $_fbeHelper;
3435

36+
/**
37+
* @var SystemConfig
38+
*/
39+
private $systemConfig;
40+
3541
/**
3642
* Constructor
3743
* @param FBEHelper $helper
44+
* @param SystemConfig $systemConfig
3845
*/
39-
public function __construct(
40-
FBEHelper $helper
41-
) {
46+
public function __construct(FBEHelper $helper, SystemConfig $systemConfig)
47+
{
4248
$this->_fbeHelper = $helper;
49+
$this->systemConfig = $systemConfig;
4350
}
4451

4552
/**
@@ -49,21 +56,24 @@ public function __construct(
4956
* after save category from Magento
5057
*
5158
* @param Observer $observer
52-
* @return
59+
* @return string|void|null
5360
*/
5461
public function execute(Observer $observer)
5562
{
63+
if (!$this->systemConfig->isCatalogSyncEnabled()) {
64+
return;
65+
}
66+
5667
$category = $observer->getEvent()->getCategory();
5768
$this->_fbeHelper->log("save category: " . $category->getName());
5869
/** @var CategoryCollection $categoryObj */
5970
$categoryObj = $this->_fbeHelper->getObject(CategoryCollection::class);
60-
$syncEnabled =$category->getData("sync_to_facebook_catalog");
71+
$syncEnabled = $category->getData("sync_to_facebook_catalog");
6172
if ($syncEnabled === "0") {
6273
$this->_fbeHelper->log("user disabled category sync");
6374
return;
6475
}
6576

66-
$response = $categoryObj->makeHttpRequestAfterCategorySave($category);
67-
return $response;
77+
return $categoryObj->makeHttpRequestAfterCategorySave($category);
6878
}
6979
}

app/code/Meta/Catalog/Observer/Product/DeleteAfter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ public function execute(Observer $observer)
112112
private function deleteProduct($storeId, $product): void
113113
{
114114
$isActive = $this->systemConfig->isActiveExtension($storeId);
115-
$shouldIncrement = $this->systemConfig->isActiveIncrementalProductUpdates($storeId);
115+
$isCatalogSyncEnabled = $this->systemConfig->isCatalogSyncEnabled($storeId);
116116
$catalogId = $this->systemConfig->getCatalogId($storeId);
117117

118-
if (!($isActive && $shouldIncrement && $catalogId)) {
118+
if (!($isActive && $isCatalogSyncEnabled && $catalogId)) {
119119
return;
120120
}
121121

app/code/Meta/Catalog/Observer/Product/SaveAfter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ public function execute(Observer $observer)
123123
private function updateProduct($storeId, $productId): void
124124
{
125125
$isActive = $this->systemConfig->isActiveExtension($storeId);
126-
$shouldIncrement = $this->systemConfig->isActiveIncrementalProductUpdates($storeId);
126+
$isCatalogSyncEnabled = $this->systemConfig->isCatalogSyncEnabled($storeId);
127127
$catalogId = $this->systemConfig->getCatalogId($storeId);
128128

129-
if (!($isActive && $shouldIncrement && $catalogId)) {
129+
if (!($isActive && $isCatalogSyncEnabled && $catalogId)) {
130130
return;
131131
}
132132

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meta\Catalog\Setup\Patch\Data;
6+
7+
use Magento\Framework\Setup\ModuleDataSetupInterface;
8+
use Magento\Framework\Setup\Patch\DataPatchInterface;
9+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
10+
11+
class AddCatalogSwitch implements DataPatchInterface
12+
{
13+
private const CORE_CONFIG_TABLE = "core_config_data";
14+
/**
15+
* @var ModuleDataSetupInterface
16+
*/
17+
private ModuleDataSetupInterface $moduleDataSetup;
18+
/**
19+
* @var SystemConfig
20+
*/
21+
private SystemConfig $systemConfig;
22+
23+
/**
24+
* @param ModuleDataSetupInterface $moduleDataSetup
25+
* @param SystemConfig $systemConfig
26+
*/
27+
public function __construct(
28+
ModuleDataSetupInterface $moduleDataSetup,
29+
SystemConfig $systemConfig
30+
) {
31+
$this->moduleDataSetup = $moduleDataSetup;
32+
$this->systemConfig = $systemConfig;
33+
}
34+
35+
public static function getDependencies()
36+
{
37+
return [];
38+
}
39+
40+
public function getAliases()
41+
{
42+
return [];
43+
}
44+
45+
public function apply(): void
46+
{
47+
$connection = $this->moduleDataSetup->getConnection();
48+
$connection->startSetup();
49+
50+
$stores = $this->systemConfig
51+
->getStoreManager()
52+
->getStores(false, true);
53+
54+
// update for default config as well
55+
$this->updateStoreCatalogIntegration(0);
56+
foreach ($stores as $store) {
57+
$this->updateStoreCatalogIntegration($store->getId());
58+
}
59+
60+
$connection->endSetup();
61+
}
62+
63+
/**
64+
* Updates Store catalog integration
65+
*
66+
* @param $storeId
67+
* @return void
68+
*/
69+
private function updateStoreCatalogIntegration($storeId): void
70+
{
71+
$connection = $this->moduleDataSetup->getConnection();
72+
$coreConfigTable = $connection->getTableName(self::CORE_CONFIG_TABLE);
73+
74+
$isDailyFeedSyncEnabled = $this->fetchValue(
75+
$storeId,
76+
"facebook/catalog_management/daily_product_feed"
77+
);
78+
$isCatalogSyncEnabled = $this->fetchValue(
79+
$storeId,
80+
"facebook/catalog_management/enable_catalog_sync"
81+
);
82+
$outOfStockThresholdOld = $this->fetchValue(
83+
$storeId,
84+
"facebook/inventory_management/out_of_stock_threshold"
85+
);
86+
$outOfStockThresholdNew = $this->fetchValue(
87+
$storeId,
88+
"facebook/catalog_management/out_of_stock_threshold"
89+
);
90+
91+
if ($isCatalogSyncEnabled == null && $isDailyFeedSyncEnabled != null) {
92+
$connection->insert($coreConfigTable, [
93+
"scope" => $storeId ? "stores" : "default",
94+
"scope_id" => $storeId,
95+
"path" => "facebook/catalog_management/enable_catalog_sync",
96+
"value" => $isDailyFeedSyncEnabled,
97+
]);
98+
}
99+
100+
if ($outOfStockThresholdNew == null && $outOfStockThresholdOld != null) {
101+
$connection->insert($coreConfigTable, [
102+
"scope" => $storeId ? "stores" : "default",
103+
"scope_id" => $storeId,
104+
"path" => "facebook/catalog_management/out_of_stock_threshold",
105+
"value" => $outOfStockThresholdOld,
106+
]);
107+
}
108+
109+
$connection->delete($coreConfigTable, [
110+
"scope_id = ?" => $storeId,
111+
"path = ?" => "facebook/catalog_management/daily_product_feed",
112+
]);
113+
$connection->delete($coreConfigTable, [
114+
"scope_id = ?" => $storeId,
115+
"path = ?" =>
116+
"facebook/inventory_management/out_of_stock_threshold",
117+
]);
118+
$connection->delete($coreConfigTable, [
119+
"scope_id = ?" => $storeId,
120+
"path = ?" =>
121+
"facebook/catalog_management/incremental_product_updates",
122+
]);
123+
$connection->delete($coreConfigTable, [
124+
"scope_id = ?" => $storeId,
125+
"path = ?" =>
126+
"facebook/inventory_management/enable_inventory_upload",
127+
]);
128+
$connection->delete($coreConfigTable, [
129+
"scope_id = ?" => $storeId,
130+
"path = ?" => "facebook/catalog_management/feed_upload_method",
131+
]);
132+
}
133+
134+
/**
135+
* Fetch store config value
136+
*
137+
* @param $storeId
138+
* @param $config_path
139+
* @return mixed|null
140+
*/
141+
private function fetchValue($storeId, $config_path): mixed
142+
{
143+
$connection = $this->moduleDataSetup->getConnection();
144+
$scopeCondition = $connection->prepareSqlCondition("scope_id", [
145+
"eq" => $storeId,
146+
]);
147+
$pathCondition = $connection->prepareSqlCondition("path", [
148+
"eq" => $config_path,
149+
]);
150+
$query = $connection
151+
->select()
152+
->from($connection->getTableName(self::CORE_CONFIG_TABLE))
153+
->where($scopeCondition)
154+
->where($pathCondition);
155+
156+
$result = $connection->fetchRow($query);
157+
158+
return $result ? $result['value'] : null;
159+
}
160+
}

app/code/Meta/Catalog/Test/Unit/Observer/ProcessCategoryAfterDeleteEventObserverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function setUp(): void
8080

8181
public function testExecution()
8282
{
83-
$this->systemConfig->method('isActiveIncrementalProductUpdates')->willReturn(true);
83+
$this->systemConfig->method('isCatalogSyncEnabled')->willReturn(true);
8484
$categoryObj = $this->createMock(CategoryCollection::class);
8585
$this->fbeHelper->expects($this->once())->method('getObject')->willReturn($categoryObj);
8686
$this->fbeHelper->expects($this->once())->method('log')->willReturn(null);

0 commit comments

Comments
 (0)