Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 16315: Product save with onthefly index ignores website assignments #27365

Merged
merged 6 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@ public function execute($id = null)
$ids = [$id];
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();

$storeIds = $this->getAssignedStoreIdsOfProduct($id);

$stores = $this->_storeManager->getStores();
foreach ($stores as $store) {
$tableExists = $this->_isFlatTableExists($store->getId());
if ($tableExists) {
if (!in_array($store->getId(), $storeIds)) {
$this->flatItemEraser->deleteProductsFromStore($id, $store->getId());
continue;
}
$this->flatItemEraser->removeDeletedProducts($ids, $store->getId());
$this->flatItemEraser->removeDisabledProducts($ids, $store->getId());
}
Expand Down Expand Up @@ -129,4 +135,23 @@ public function execute($id = null)

return $this;
}

/**
* Get list store id where the product is enable
*
* @param int $productId
* @return array
*/
private function getAssignedStoreIdsOfProduct($productId)
{
$select = $this->_connection->select();
$select->from(['e' => $this->_productIndexerHelper->getTable('store')], ['e.store_id'])
->where('c.product_id = ' . $productId)
->joinLeft(
['c' => $this->_productIndexerHelper->getTable('catalog_product_website')],
'e.website_id = c.website_id',
[]
);
return $this->_connection->fetchCol($select);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,24 @@ public function testExecuteWithExistingFlatTablesCreatesTables()
->willReturn('store_flat_table');
$this->connection->expects($this->any())->method('isTableExists')->with('store_flat_table')
->willReturn(true);
$this->connection->expects($this->any())->method('fetchCol')
->willReturn(['store_id_1']);
$this->flatItemEraser->expects($this->once())->method('removeDeletedProducts');
$this->flatTableBuilder->expects($this->never())->method('build')->with('store_id_1', ['product_id_1']);
$this->model->execute('product_id_1');
}

public function testExecuteWithExistingFlatTablesRemoveProductFromStore()
{
$this->productIndexerHelper->expects($this->any())->method('getFlatTableName')
->willReturn('store_flat_table');
$this->connection->expects($this->any())->method('isTableExists')->with('store_flat_table')
->willReturn(true);
$this->connection->expects($this->any())->method('fetchCol')
->willReturn([1]);
$this->flatItemEraser->expects($this->once())->method('deleteProductsFromStore');
$this->flatItemEraser->expects($this->never())->method('removeDeletedProducts');
$this->flatTableBuilder->expects($this->never())->method('build')->with('store_id_1', ['product_id_1']);
$this->model->execute('product_id_1');
}
}