88namespace Magento \Catalog \Model \Indexer \Product \Flat \Action ;
99
1010use 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+ */
1217class 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 *
0 commit comments