8
8
namespace Magento \Catalog \Model \Indexer \Product \Flat \Action ;
9
9
10
10
use Magento \Framework \App \ResourceConnection ;
11
+ use Magento \Catalog \Model \Product \Attribute \Source \Status ;
12
+ use Magento \Store \Model \Store ;
11
13
14
+ /**
15
+ * Flat item eraser. Used to clear items from the catalog flat table.
16
+ */
12
17
class Eraser
13
18
{
14
19
/**
@@ -50,12 +55,7 @@ public function __construct(
50
55
*/
51
56
public function removeDeletedProducts (array &$ ids , $ storeId )
52
57
{
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 );
59
59
$ result = $ this ->connection ->query ($ select );
60
60
61
61
$ existentProducts = [];
@@ -69,6 +69,61 @@ public function removeDeletedProducts(array &$ids, $storeId)
69
69
$ this ->deleteProductsFromStore ($ productsToDelete , $ storeId );
70
70
}
71
71
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
+
72
127
/**
73
128
* Delete products from flat table(s)
74
129
*
0 commit comments