88use Magento \Catalog \Model \Product \Attribute \Source \Status as ProductStatus ;
99use Magento \Catalog \Api \Data \ProductInterface ;
1010use Magento \Catalog \Api \Data \ProductAttributeInterface ;
11+ use Magento \Catalog \Model \ResourceModel \Helper ;
12+ use Magento \Eav \Api \AttributeRepositoryInterface ;
13+ use Magento \Eav \Model \Config ;
14+ use Magento \Framework \Api \SearchCriteriaBuilder ;
1115use Magento \Framework \DB \Select ;
1216use Magento \Framework \DB \Sql \UnionExpression ;
17+ use Magento \Framework \EntityManager \MetadataPool ;
18+ use Magento \Framework \Event \ManagerInterface ;
19+ use Magento \Framework \Indexer \Table \StrategyInterface ;
20+ use Magento \Framework \Model \ResourceModel \Db \Context ;
1321
1422/**
1523 * Catalog Product Eav Select and Multiply Select Attributes Indexer resource model
@@ -40,31 +48,34 @@ class Source extends AbstractEav
4048 /**
4149 * Construct
4250 *
43- * @param \Magento\Framework\Model\ResourceModel\Db\ Context $context
44- * @param \Magento\Framework\Indexer\Table\ StrategyInterface $tableStrategy
45- * @param \Magento\Eav\Model\ Config $eavConfig
46- * @param \Magento\Framework\Event\ ManagerInterface $eventManager
47- * @param \Magento\Catalog\Model\ResourceModel\ Helper $resourceHelper
51+ * @param Context $context
52+ * @param StrategyInterface $tableStrategy
53+ * @param Config $eavConfig
54+ * @param ManagerInterface $eventManager
55+ * @param Helper $resourceHelper
4856 * @param null|string $connectionName
49- * @param \Magento\Eav\Api\AttributeRepositoryInterface|null $attributeRepository
50- * @param \Magento\Framework\Api\SearchCriteriaBuilder|null $criteriaBuilder
57+ * @param AttributeRepositoryInterface|null $attributeRepository
58+ * @param SearchCriteriaBuilder|null $criteriaBuilder
59+ * @param MetadataPool|null $metadataPool
5160 */
5261 public function __construct (
5362 \Magento \Framework \Model \ResourceModel \Db \Context $ context ,
5463 \Magento \Framework \Indexer \Table \StrategyInterface $ tableStrategy ,
5564 \Magento \Eav \Model \Config $ eavConfig ,
5665 \Magento \Framework \Event \ManagerInterface $ eventManager ,
5766 \Magento \Catalog \Model \ResourceModel \Helper $ resourceHelper ,
58- $ connectionName = null ,
67+ ? string $ connectionName = null ,
5968 \Magento \Eav \Api \AttributeRepositoryInterface $ attributeRepository = null ,
60- \Magento \Framework \Api \SearchCriteriaBuilder $ criteriaBuilder = null
69+ \Magento \Framework \Api \SearchCriteriaBuilder $ criteriaBuilder = null ,
70+ ?\Magento \Framework \EntityManager \MetadataPool $ metadataPool = null
6171 ) {
6272 parent ::__construct (
6373 $ context ,
6474 $ tableStrategy ,
6575 $ eavConfig ,
6676 $ eventManager ,
67- $ connectionName
77+ $ connectionName ,
78+ $ metadataPool
6879 );
6980 $ this ->_resourceHelper = $ resourceHelper ;
7081 $ this ->attributeRepository = $ attributeRepository
@@ -75,6 +86,19 @@ public function __construct(
7586 ->get (\Magento \Framework \Api \SearchCriteriaBuilder::class);
7687 }
7788
89+ /**
90+ * @inheritDoc
91+ */
92+ public function reindexEntities ($ processIds )
93+ {
94+ $ this ->clearTemporaryIndexTable ();
95+
96+ $ this ->_prepareIndex ($ processIds );
97+ $ this ->_prepareRelationIndex ($ processIds );
98+
99+ return $ this ;
100+ }
101+
78102 /**
79103 * Initialize connection and define main index table
80104 *
@@ -135,6 +159,7 @@ protected function _prepareIndex($entityIds = null, $attributeId = null)
135159 * @param int $attributeId the attribute id limitation
136160 * @return $this
137161 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
162+ * @throws \Exception
138163 */
139164 protected function _prepareSelectIndex ($ entityIds = null , $ attributeId = null )
140165 {
@@ -149,7 +174,7 @@ protected function _prepareSelectIndex($entityIds = null, $attributeId = null)
149174 $ attrIdsFlat = implode (', ' , array_map ('intval ' , $ attrIds ));
150175 $ ifNullSql = $ connection ->getIfNullSql ('pis.value ' , 'COALESCE(ds.value, dd.value) ' );
151176
152- /**@var $select \Magento\Framework\DB\ Select*/
177+ /**@var $select Select */
153178 $ select = $ connection ->select ()->distinct (true )->from (
154179 ['s ' => $ this ->getTable ('store ' )],
155180 []
@@ -204,6 +229,17 @@ protected function _prepareSelectIndex($entityIds = null, $attributeId = null)
204229 'cpe.entity_id AS source_id ' ,
205230 ]
206231 );
232+ $ visibilityCondition = $ connection ->quoteInto (
233+ '>? ' ,
234+ \Magento \Catalog \Model \Product \Visibility::VISIBILITY_NOT_VISIBLE
235+ );
236+ $ this ->_addAttributeToSelect (
237+ $ select ,
238+ 'visibility ' ,
239+ "cpe. {$ productIdField }" ,
240+ 's.store_id ' ,
241+ $ visibilityCondition
242+ );
207243
208244 if ($ entityIds !== null ) {
209245 $ ids = implode (', ' , array_map ('intval ' , $ entityIds ));
@@ -239,6 +275,14 @@ protected function _prepareSelectIndex($entityIds = null, $attributeId = null)
239275 ->where ('wd.store_id != 0 ' )
240276 ->where ("cpe.entity_id IN( {$ ids }) " );
241277 $ select ->where ("cpe.entity_id IN( {$ ids }) " );
278+ $ this ->_addAttributeToSelect (
279+ $ selectWithoutDefaultStore ,
280+ 'visibility ' ,
281+ "cpe. {$ productIdField }" ,
282+ 'wd.store_id ' ,
283+ $ visibilityCondition
284+ );
285+
242286 $ selects = new UnionExpression (
243287 [$ select , $ selectWithoutDefaultStore ],
244288 Select::SQL_UNION ,
@@ -272,6 +316,7 @@ protected function _prepareSelectIndex($entityIds = null, $attributeId = null)
272316 * @param array $entityIds the entity ids limitation
273317 * @param int $attributeId the attribute id limitation
274318 * @return $this
319+ * @throws \Exception
275320 */
276321 protected function _prepareMultiselectIndex ($ entityIds = null , $ attributeId = null )
277322 {
@@ -358,6 +403,13 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu
358403 ]
359404 );
360405
406+ $ this ->_addAttributeToSelect (
407+ $ select ,
408+ 'visibility ' ,
409+ "cpe. {$ productIdField }" ,
410+ 'cs.store_id ' ,
411+ $ connection ->quoteInto ('>? ' , \Magento \Catalog \Model \Product \Visibility::VISIBILITY_NOT_VISIBLE )
412+ );
361413 $ this ->saveDataFromSelect ($ select , $ options );
362414
363415 return $ this ;
@@ -431,11 +483,11 @@ public function getIdxTable($table = null)
431483 /**
432484 * Save data from select
433485 *
434- * @param \Magento\Framework\DB\ Select $select
486+ * @param Select $select
435487 * @param array $options
436488 * @return void
437489 */
438- private function saveDataFromSelect (\ Magento \ Framework \ DB \ Select $ select , array $ options )
490+ private function saveDataFromSelect (Select $ select , array $ options )
439491 {
440492 $ i = 0 ;
441493 $ data = [];
0 commit comments