Skip to content

Commit 1718278

Browse files
Product Set Id Category save replaced with updating catalog_category_entity_varchar directly (#514)
* Category save replaced with low level sql insert statement * static error resolved * update category product set id added * Static errors resolved
1 parent b3a649a commit 1718278

File tree

2 files changed

+61
-28
lines changed

2 files changed

+61
-28
lines changed

app/code/Meta/Catalog/Model/Category/CategoryCollection.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,9 @@ private function processCategoryBatchResponse(
396396
$responseData = json_decode($response['body'], true);
397397

398398
if ($httpStatusCode == 200) {
399-
$setId = $category->getData(SystemConfig::META_PRODUCT_SET_ID);
400-
401-
if ($setId === null && array_key_exists('id', $responseData)) {
399+
if (array_key_exists('id', $responseData)) {
402400
$setId = $responseData['id'];
403-
$this->categoryUtilities->saveFBProductSetID($category, $setId, $storeId, $this);
401+
$this->categoryUtilities->saveFBProductSetID($category, $setId, $storeId);
404402
}
405403
} else {
406404
$this->fbeHelper->log(sprintf(

app/code/Meta/Catalog/Model/Category/CategoryUtility/CategoryUtilities.php

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@
2727
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
2828
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
2929
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
30+
use Magento\Eav\Model\Config as EavConfig;
31+
use Magento\Framework\App\ResourceConnection;
3032
use Meta\BusinessExtension\Helper\FBEHelper;
3133
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
3234
use Meta\Catalog\Helper\Product\Identifier as ProductIdentifier;
3335

36+
/**
37+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38+
*/
3439
class CategoryUtilities
3540
{
3641
/**
@@ -68,6 +73,16 @@ class CategoryUtilities
6873
*/
6974
private CategoryImageService $imageService;
7075

76+
/**
77+
* @var EavConfig
78+
*/
79+
private EavConfig $eavConfig;
80+
81+
/**
82+
* @var ResourceConnection
83+
*/
84+
private ResourceConnection $resourceConnection;
85+
7186
/**
7287
* Constructor
7388
* @param ProductCollectionFactory $productCollectionFactory
@@ -77,6 +92,8 @@ class CategoryUtilities
7792
* @param SystemConfig $systemConfig
7893
* @param ProductIdentifier $productIdentifier
7994
* @param CategoryImageService $imageService
95+
* @param EavConfig $eavConfig
96+
* @param ResourceConnection $resourceConnection
8097
*/
8198
public function __construct(
8299
ProductCollectionFactory $productCollectionFactory,
@@ -85,7 +102,9 @@ public function __construct(
85102
FBEHelper $fbeHelper,
86103
SystemConfig $systemConfig,
87104
ProductIdentifier $productIdentifier,
88-
CategoryImageService $imageService
105+
CategoryImageService $imageService,
106+
EavConfig $eavConfig,
107+
ResourceConnection $resourceConnection
89108
) {
90109
$this->categoryCollection = $categoryCollection;
91110
$this->categoryRepository = $categoryRepository;
@@ -94,6 +113,8 @@ public function __construct(
94113
$this->systemConfig = $systemConfig;
95114
$this->productIdentifier = $productIdentifier;
96115
$this->imageService = $imageService;
116+
$this->eavConfig = $eavConfig;
117+
$this->resourceConnection = $resourceConnection;
97118
}
98119
/**
99120
* Fetch products for product category
@@ -233,36 +254,50 @@ public function getAllChildrenCategories(
233254
public function saveFBProductSetID(Category $category, string $setId, $storeId): void
234255
{
235256
$this->fbeHelper->log(sprintf(
236-
"saving category %s ,id %s ,storeId %s and setId %s",
257+
"saving product set id for category %s ,id %s ,storeId %s and setId %s",
237258
$category->getName(),
238259
$category->getId(),
239260
$storeId,
240261
$setId
241262
));
242-
243-
$category->setData(SystemConfig::META_PRODUCT_SET_ID, $setId);
244-
$this->saveCategoryForStore($category, $storeId);
245-
}
246-
247-
/**
248-
* Save category for store
249-
*
250-
* @param Category $category
251-
* @param int $storeId
252-
*/
253-
private function saveCategoryForStore(Category $category, $storeId): void
254-
{
255263
try {
256-
if (null !== $storeId) {
257-
$currentStoreId = $this->systemConfig->getStoreManager()->getStore()->getId();
258-
// needs to update it as category save function using storeId from store Manager
259-
$this->systemConfig->getStoreManager()->setCurrentStore($storeId);
260-
$this->categoryRepository->save($category);
261-
$this->systemConfig->getStoreManager()->setCurrentStore($currentStoreId);
262-
return;
263-
}
264+
$productSetAttribute = $this->eavConfig->getAttribute(
265+
Category::ENTITY,
266+
SystemConfig::META_PRODUCT_SET_ID
267+
);
268+
$productSetAttributeId = $productSetAttribute->getAttributeId();
264269

265-
$this->categoryRepository->save($category);
270+
if ($productSetAttributeId) {
271+
$categoryEntityVarcharTable = $this->resourceConnection->getTableName(
272+
'catalog_category_entity_varchar'
273+
);
274+
if ($category->getData(SystemConfig::META_PRODUCT_SET_ID) == null) {
275+
$this->resourceConnection->getConnection()->insert(
276+
$categoryEntityVarcharTable,
277+
[
278+
'attribute_id' => $productSetAttributeId,
279+
'store_id' => $storeId,
280+
'entity_id' => $category->getId(),
281+
'value' => $setId
282+
]
283+
);
284+
} else {
285+
$this->resourceConnection->getConnection()->update(
286+
$categoryEntityVarcharTable,
287+
[
288+
'attribute_id' => $productSetAttributeId,
289+
'store_id' => $storeId,
290+
'entity_id' => $category->getId(),
291+
'value' => $setId
292+
],
293+
[
294+
'attribute_id = ?' => $productSetAttributeId,
295+
'store_id = ?' => $storeId,
296+
'entity_id = ?' => $category->getId(),
297+
]
298+
);
299+
}
300+
}
266301
} catch (\Throwable $e) {
267302
$this->fbeHelper->logException($e);
268303
}

0 commit comments

Comments
 (0)