27
27
use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory as CategoryCollectionFactory ;
28
28
use Magento \Catalog \Model \ResourceModel \Product \Collection as ProductCollection ;
29
29
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
30
+ use Magento \Eav \Model \Config as EavConfig ;
31
+ use Magento \Framework \App \ResourceConnection ;
30
32
use Meta \BusinessExtension \Helper \FBEHelper ;
31
33
use Meta \BusinessExtension \Model \System \Config as SystemConfig ;
32
34
use Meta \Catalog \Helper \Product \Identifier as ProductIdentifier ;
33
35
36
+ /**
37
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38
+ */
34
39
class CategoryUtilities
35
40
{
36
41
/**
@@ -68,6 +73,16 @@ class CategoryUtilities
68
73
*/
69
74
private CategoryImageService $ imageService ;
70
75
76
+ /**
77
+ * @var EavConfig
78
+ */
79
+ private EavConfig $ eavConfig ;
80
+
81
+ /**
82
+ * @var ResourceConnection
83
+ */
84
+ private ResourceConnection $ resourceConnection ;
85
+
71
86
/**
72
87
* Constructor
73
88
* @param ProductCollectionFactory $productCollectionFactory
@@ -77,6 +92,8 @@ class CategoryUtilities
77
92
* @param SystemConfig $systemConfig
78
93
* @param ProductIdentifier $productIdentifier
79
94
* @param CategoryImageService $imageService
95
+ * @param EavConfig $eavConfig
96
+ * @param ResourceConnection $resourceConnection
80
97
*/
81
98
public function __construct (
82
99
ProductCollectionFactory $ productCollectionFactory ,
@@ -85,7 +102,9 @@ public function __construct(
85
102
FBEHelper $ fbeHelper ,
86
103
SystemConfig $ systemConfig ,
87
104
ProductIdentifier $ productIdentifier ,
88
- CategoryImageService $ imageService
105
+ CategoryImageService $ imageService ,
106
+ EavConfig $ eavConfig ,
107
+ ResourceConnection $ resourceConnection
89
108
) {
90
109
$ this ->categoryCollection = $ categoryCollection ;
91
110
$ this ->categoryRepository = $ categoryRepository ;
@@ -94,6 +113,8 @@ public function __construct(
94
113
$ this ->systemConfig = $ systemConfig ;
95
114
$ this ->productIdentifier = $ productIdentifier ;
96
115
$ this ->imageService = $ imageService ;
116
+ $ this ->eavConfig = $ eavConfig ;
117
+ $ this ->resourceConnection = $ resourceConnection ;
97
118
}
98
119
/**
99
120
* Fetch products for product category
@@ -233,36 +254,50 @@ public function getAllChildrenCategories(
233
254
public function saveFBProductSetID (Category $ category , string $ setId , $ storeId ): void
234
255
{
235
256
$ 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 " ,
237
258
$ category ->getName (),
238
259
$ category ->getId (),
239
260
$ storeId ,
240
261
$ setId
241
262
));
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
- {
255
263
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 ();
264
269
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
+ }
266
301
} catch (\Throwable $ e ) {
267
302
$ this ->fbeHelper ->logException ($ e );
268
303
}
0 commit comments