66namespace Magento \Catalog \Model \ResourceModel \Product \Indexer \Eav ;
77
88use Magento \Catalog \Api \Data \ProductInterface ;
9+ use Magento \Framework \App \ObjectManager ;
910
1011/**
1112 * Catalog Product Eav Attributes abstract indexer resource model
@@ -54,6 +55,7 @@ public function reindexAll()
5455 $ this ->clearTemporaryIndexTable ();
5556 $ this ->_prepareIndex ();
5657 $ this ->_prepareRelationIndex ();
58+ $ this ->_removeNotVisibleEntityFromIndex ();
5759 $ this ->syncData ();
5860 $ this ->commit ();
5961 } catch (\Exception $ e ) {
@@ -76,6 +78,7 @@ public function reindexEntities($processIds)
7678
7779 $ this ->_prepareIndex ($ processIds );
7880 $ this ->_prepareRelationIndex ($ processIds );
81+ $ this ->_removeNotVisibleEntityFromIndex ();
7982
8083 return $ this ;
8184 }
@@ -88,7 +91,6 @@ public function reindexEntities($processIds)
8891 * @param int $attributeId
8992 * @param bool $isIndexable
9093 * @return $this
91- * @throws \Exception
9294 */
9395 public function reindexAttribute ($ attributeId , $ isIndexable = true )
9496 {
@@ -99,6 +101,7 @@ public function reindexAttribute($attributeId, $isIndexable = true)
99101
100102 $ this ->_prepareIndex (null , $ attributeId );
101103 $ this ->_prepareRelationIndex ();
104+ $ this ->_removeNotVisibleEntityFromIndex ();
102105
103106 $ this ->_synchronizeAttributeIndexData ($ attributeId );
104107 }
@@ -115,6 +118,39 @@ public function reindexAttribute($attributeId, $isIndexable = true)
115118 */
116119 abstract protected function _prepareIndex ($ entityIds = null , $ attributeId = null );
117120
121+ /**
122+ * Remove Not Visible products from temporary data index
123+ *
124+ * @return $this
125+ */
126+ protected function _removeNotVisibleEntityFromIndex ()
127+ {
128+ $ connection = $ this ->getConnection ();
129+ $ idxTable = $ this ->getIdxTable ();
130+
131+ $ select = $ connection ->select ()->from ($ idxTable , null );
132+
133+ $ select ->joinLeft (
134+ ['cpe ' => $ this ->getTable ('catalog_product_entity ' )],
135+ "cpe.entity_id = {$ idxTable }.entity_id " ,
136+ []
137+ );
138+ $ linkField = $ this ->getMetadataPool ()->getMetadata (ProductInterface::class)->getLinkField ();
139+ $ condition = $ connection ->quoteInto ('=? ' , \Magento \Catalog \Model \Product \Visibility::VISIBILITY_NOT_VISIBLE );
140+ $ this ->_addAttributeToSelect (
141+ $ select ,
142+ 'visibility ' ,
143+ "cpe. {$ linkField }" ,
144+ $ idxTable . '.store_id ' ,
145+ $ condition
146+ );
147+
148+ $ query = $ select ->deleteFromSelect ($ idxTable );
149+ $ connection ->query ($ query );
150+
151+ return $ this ;
152+ }
153+
118154 /**
119155 * Prepare data index select for product relations
120156 *
0 commit comments