Skip to content

Commit 28066af

Browse files
author
Yauhen_Lyskavets
committed
MAGETWO-91694: Product does not get removed from flat product table after getting disabled
- Fix added - Refactoring
1 parent ed8b676 commit 28066af

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

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

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
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

1214
class Eraser
1315
{
@@ -50,12 +52,7 @@ public function __construct(
5052
*/
5153
public function removeDeletedProducts(array &$ids, $storeId)
5254
{
53-
$select = $this->connection->select()->from(
54-
$this->productIndexerHelper->getTable('catalog_product_entity')
55-
)->where(
56-
'entity_id IN(?)',
57-
$ids
58-
);
55+
$select = $this->getSelectForProducts($ids);
5956
$result = $this->connection->query($select);
6057

6158
$existentProducts = [];
@@ -69,6 +66,61 @@ public function removeDeletedProducts(array &$ids, $storeId)
6966
$this->deleteProductsFromStore($productsToDelete, $storeId);
7067
}
7168

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

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
@@ -74,6 +74,7 @@ public function execute($id = null)
7474
$tableExists = $this->_isFlatTableExists($store->getId());
7575
if ($tableExists) {
7676
$this->flatItemEraser->removeDeletedProducts($ids, $store->getId());
77+
$this->flatItemEraser->removeDisabledProducts($ids, $store->getId());
7778
}
7879
if (isset($ids[0])) {
7980
if (!$tableExists) {

0 commit comments

Comments
 (0)