Skip to content

Commit e0e5d89

Browse files
committed
ACP2E-2843: Products on the frontend use store specific data when Single-Store Mode is enabled
- solution with test coverage
1 parent adb7c0b commit e0e5d89

File tree

1 file changed

+67
-18
lines changed

1 file changed

+67
-18
lines changed

app/code/Magento/Catalog/Model/ResourceModel/CatalogCategoryAndProductResolverOnSingleStoreMode.php

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,21 @@ public function __construct(
4747
*/
4848
private function process(int $storeId, string $table): void
4949
{
50-
$connection = $this->resourceConnection->getConnection();
5150
$catalogProductTable = $this->resourceConnection->getTableName($table);
52-
$select = $connection->select()
53-
->from($catalogProductTable, ['value_id', 'attribute_id', 'row_id'])
54-
->where('store_id = ?', $storeId);
55-
$catalogProducts = $connection->fetchAll($select);
51+
52+
$catalogProducts = $this->getCatalogProducts($table, $storeId);
53+
$rowIds = [];
54+
$attributeIds = [];
55+
$valueIds = [];
5656
try {
5757
if ($catalogProducts) {
5858
foreach ($catalogProducts as $catalogProduct) {
59-
$connection->delete(
60-
$table,
61-
[
62-
'store_id = ?' => Store::DEFAULT_STORE_ID,
63-
'attribute_id = ?' => $catalogProduct['attribute_id'],
64-
'row_id = ?' => $catalogProduct['row_id']
65-
]
66-
);
67-
$connection->update(
68-
$table,
69-
['store_id' => Store::DEFAULT_STORE_ID],
70-
['value_id = ?' => $catalogProduct['value_id']]
71-
);
59+
$rowIds[] = $catalogProduct['row_id'];
60+
$attributeIds[] = $catalogProduct['attribute_id'];
61+
$valueIds[] = $catalogProduct['value_id'];
7262
}
63+
$this->massDelete($catalogProductTable, $attributeIds, $rowIds);
64+
$this->massUpdate($catalogProductTable, $valueIds);
7365
}
7466
} catch (LocalizedException $e) {
7567
throw new CouldNotSaveException(
@@ -111,4 +103,61 @@ public function migrateCatalogCategoryAndProductTables(int $storeId): void
111103
$connection->rollBack();
112104
}
113105
}
106+
107+
/**
108+
* Delete default store related products
109+
*
110+
* @param $catalogProductTable
111+
* @param array $attributeIds
112+
* @param array $rowIds
113+
* @return void
114+
*/
115+
private function massDelete($catalogProductTable, array $attributeIds, array $rowIds): void
116+
{
117+
$connection = $this->resourceConnection->getConnection();
118+
119+
$connection->delete(
120+
$catalogProductTable,
121+
[
122+
'store_id = ?' => Store::DEFAULT_STORE_ID,
123+
'attribute_id IN(?)' => $attributeIds,
124+
'row_id IN(?)' => $rowIds
125+
]
126+
);
127+
}
128+
129+
/**
130+
* Update default store related products
131+
*
132+
* @param $catalogProductTable
133+
* @param array $valueIds
134+
* @return void
135+
*/
136+
private function massUpdate($catalogProductTable, array $valueIds): void
137+
{
138+
$connection = $this->resourceConnection->getConnection();
139+
140+
$connection->update(
141+
$catalogProductTable,
142+
['store_id' => Store::DEFAULT_STORE_ID],
143+
['value_id IN(?)' => $valueIds]
144+
);
145+
}
146+
147+
/**
148+
* Get list of products
149+
*
150+
* @param string $table
151+
* @param int $storeId
152+
* @return array
153+
*/
154+
private function getCatalogProducts(string $table, int $storeId): array
155+
{
156+
$connection = $this->resourceConnection->getConnection();
157+
$catalogProductTable = $this->resourceConnection->getTableName($table);
158+
$select = $connection->select()
159+
->from($catalogProductTable, ['value_id', 'attribute_id', 'row_id'])
160+
->where('store_id = ?', $storeId);
161+
return $connection->fetchAll($select);
162+
}
114163
}

0 commit comments

Comments
 (0)