Skip to content

Commit 65550ba

Browse files
committed
Merge remote-tracking branch 'github-magento2ce/MAGETWO-91694' into EPAM-PR-12
2 parents bdcc16a + c10859d commit 65550ba

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

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

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
namespace Magento\Catalog\Model\Indexer\Product\Flat\Action;
99

1010
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
12+
use Magento\Store\Model\Store;
1113

14+
/**
15+
* Flat item eraser. Used to clear items from the catalog flat table.
16+
*/
1217
class Eraser
1318
{
1419
/**
@@ -50,12 +55,7 @@ public function __construct(
5055
*/
5156
public function removeDeletedProducts(array &$ids, $storeId)
5257
{
53-
$select = $this->connection->select()->from(
54-
$this->productIndexerHelper->getTable('catalog_product_entity')
55-
)->where(
56-
'entity_id IN(?)',
57-
$ids
58-
);
58+
$select = $this->getSelectForProducts($ids);
5959
$result = $this->connection->query($select);
6060

6161
$existentProducts = [];
@@ -69,6 +69,61 @@ public function removeDeletedProducts(array &$ids, $storeId)
6969
$this->deleteProductsFromStore($productsToDelete, $storeId);
7070
}
7171

72+
/**
73+
* Remove products with "Disabled" status from the flat table(s).
74+
*
75+
* @param array $ids
76+
* @param int $storeId
77+
* @return void
78+
*/
79+
public function removeDisabledProducts(array &$ids, $storeId)
80+
{
81+
/* @var $statusAttribute \Magento\Eav\Model\Entity\Attribute */
82+
$statusAttribute = $this->productIndexerHelper->getAttribute('status');
83+
84+
$select = $this->getSelectForProducts($ids);
85+
$select->joinLeft(
86+
['status_global_attr' => $statusAttribute->getBackendTable()],
87+
' status_global_attr.attribute_id = ' . (int)$statusAttribute->getAttributeId()
88+
. ' AND status_global_attr.store_id = ' . Store::DEFAULT_STORE_ID,
89+
[]
90+
);
91+
$select->joinLeft(
92+
['status_attr' => $statusAttribute->getBackendTable()],
93+
' status_attr.attribute_id = ' . (int)$statusAttribute->getAttributeId()
94+
. ' AND status_attr.store_id = ' . $storeId,
95+
[]
96+
);
97+
$select->where('IFNULL(status_attr.value, status_global_attr.value) = ?', Status::STATUS_DISABLED);
98+
99+
$result = $this->connection->query($select);
100+
101+
$disabledProducts = [];
102+
foreach ($result->fetchAll() as $product) {
103+
$disabledProducts[] = $product['entity_id'];
104+
}
105+
106+
if (!empty($disabledProducts)) {
107+
$ids = array_diff($ids, $disabledProducts);
108+
$this->deleteProductsFromStore($disabledProducts, $storeId);
109+
}
110+
}
111+
112+
/**
113+
* Get Select object for existed products.
114+
*
115+
* @param array $ids
116+
* @return \Magento\Framework\DB\Select
117+
*/
118+
private function getSelectForProducts(array $ids)
119+
{
120+
$productTable = $this->productIndexerHelper->getTable('catalog_product_entity');
121+
$select = $this->connection->select()->from($productTable)
122+
->columns('entity_id')
123+
->where('entity_id IN(?)', $ids);
124+
return $select;
125+
}
126+
72127
/**
73128
* Delete products from flat table(s)
74129
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function execute($id = null)
7575
$tableExists = $this->_isFlatTableExists($store->getId());
7676
if ($tableExists) {
7777
$this->flatItemEraser->removeDeletedProducts($ids, $store->getId());
78+
$this->flatItemEraser->removeDisabledProducts($ids, $store->getId());
7879
}
7980

8081
/* @var $status \Magento\Eav\Model\Entity\Attribute */

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function testRemoveDeletedProducts()
5454
$productsToDeleteIds = [1, 2];
5555
$select = $this->createMock(\Magento\Framework\DB\Select::class);
5656
$select->expects($this->once())->method('from')->with('catalog_product_entity')->will($this->returnSelf());
57+
$select->expects($this->once())->method('columns')->with('entity_id')->will($this->returnSelf());
5758
$select->expects($this->once())->method('where')->with('entity_id IN(?)', $productsToDeleteIds)
5859
->will($this->returnSelf());
5960
$products = [['entity_id' => 2]];

0 commit comments

Comments
 (0)