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
12
14
class Eraser
13
15
{
@@ -50,12 +52,7 @@ public function __construct(
50
52
*/
51
53
public function removeDeletedProducts (array &$ ids , $ storeId )
52
54
{
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 );
59
56
$ result = $ this ->connection ->query ($ select );
60
57
61
58
$ existentProducts = [];
@@ -69,6 +66,61 @@ public function removeDeletedProducts(array &$ids, $storeId)
69
66
$ this ->deleteProductsFromStore ($ productsToDelete , $ storeId );
70
67
}
71
68
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
+
72
124
/**
73
125
* Delete products from flat table(s)
74
126
*
0 commit comments