Skip to content

Commit

Permalink
ENGCOM-7179: Fix issue 16315: Product save with onthefly index ignore…
Browse files Browse the repository at this point in the history
…s website assignments #27365

 - Merge Pull Request #27365 from tna274/magento2:fix_issue_16315
 - Merged commits:
   1. fe02648
   2. f2a562c
   3. 34c1818
   4. 52ea21d
   5. d0f356e
   6. b04a21c
  • Loading branch information
magento-engcom-team committed Mar 31, 2020
2 parents 1116437 + b04a21c commit 67d5bef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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');
}
}

0 comments on commit 67d5bef

Please sign in to comment.