Skip to content

Commit db0f733

Browse files
authored
ENGCOM-7179: Fix issue 16315: Product save with onthefly index ignores website assignments #27365
2 parents 8ef9334 + 67d5bef commit db0f733

File tree

2 files changed

+41
-0
lines changed
  • app/code/Magento/Catalog
    • Model/Indexer/Product/Flat/Action
    • Test/Unit/Model/Indexer/Product/Flat/Action

2 files changed

+41
-0
lines changed

app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Row.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,16 @@ public function execute($id = null)
8585
$ids = [$id];
8686
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
8787

88+
$storeIds = $this->getAssignedStoreIdsOfProduct($id);
89+
8890
$stores = $this->_storeManager->getStores();
8991
foreach ($stores as $store) {
9092
$tableExists = $this->_isFlatTableExists($store->getId());
9193
if ($tableExists) {
94+
if (!in_array($store->getId(), $storeIds)) {
95+
$this->flatItemEraser->deleteProductsFromStore($id, $store->getId());
96+
continue;
97+
}
9298
$this->flatItemEraser->removeDeletedProducts($ids, $store->getId());
9399
$this->flatItemEraser->removeDisabledProducts($ids, $store->getId());
94100
}
@@ -129,4 +135,23 @@ public function execute($id = null)
129135

130136
return $this;
131137
}
138+
139+
/**
140+
* Get list store id where the product is enable
141+
*
142+
* @param int $productId
143+
* @return array
144+
*/
145+
private function getAssignedStoreIdsOfProduct($productId)
146+
{
147+
$select = $this->_connection->select();
148+
$select->from(['e' => $this->_productIndexerHelper->getTable('store')], ['e.store_id'])
149+
->where('c.product_id = ' . $productId)
150+
->joinLeft(
151+
['c' => $this->_productIndexerHelper->getTable('catalog_product_website')],
152+
'e.website_id = c.website_id',
153+
[]
154+
);
155+
return $this->_connection->fetchCol($select);
156+
}
132157
}

app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Flat/Action/RowTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,24 @@ public function testExecuteWithExistingFlatTablesCreatesTables()
160160
->willReturn('store_flat_table');
161161
$this->connection->expects($this->any())->method('isTableExists')->with('store_flat_table')
162162
->willReturn(true);
163+
$this->connection->expects($this->any())->method('fetchCol')
164+
->willReturn(['store_id_1']);
163165
$this->flatItemEraser->expects($this->once())->method('removeDeletedProducts');
164166
$this->flatTableBuilder->expects($this->never())->method('build')->with('store_id_1', ['product_id_1']);
165167
$this->model->execute('product_id_1');
166168
}
169+
170+
public function testExecuteWithExistingFlatTablesRemoveProductFromStore()
171+
{
172+
$this->productIndexerHelper->expects($this->any())->method('getFlatTableName')
173+
->willReturn('store_flat_table');
174+
$this->connection->expects($this->any())->method('isTableExists')->with('store_flat_table')
175+
->willReturn(true);
176+
$this->connection->expects($this->any())->method('fetchCol')
177+
->willReturn([1]);
178+
$this->flatItemEraser->expects($this->once())->method('deleteProductsFromStore');
179+
$this->flatItemEraser->expects($this->never())->method('removeDeletedProducts');
180+
$this->flatTableBuilder->expects($this->never())->method('build')->with('store_id_1', ['product_id_1']);
181+
$this->model->execute('product_id_1');
182+
}
167183
}

0 commit comments

Comments
 (0)