@@ -125,31 +125,32 @@ protected function _prepareSelectIndex($entityIds = null, $attributeId = null)
125
125
['s ' => $ this ->getTable ('store ' )],
126
126
['store_id ' , 'website_id ' ]
127
127
)->joinLeft (
128
- ['d ' => $ this ->getTable ('catalog_product_entity_int ' )],
129
- 'd .store_id = 0 OR d.store_id = s.store_id ' ,
130
- ['attribute_id ' , ' value ' ]
128
+ ['dd ' => $ this ->getTable ('catalog_product_entity_int ' )],
129
+ 'dd .store_id = 0 ' ,
130
+ ['attribute_id ' ]
131
131
)->joinLeft (
132
- ['d2 ' => $ this ->getTable ('catalog_product_entity_int ' )],
133
- sprintf (
134
- "d. {$ productIdField } = d2. {$ productIdField }"
135
- . ' AND d2.attribute_id = %s AND d2.value = %s AND d.store_id = d2.store_id ' ,
136
- $ this ->_eavConfig ->getAttribute (\Magento \Catalog \Model \Product::ENTITY , 'status ' )->getId (),
137
- ProductStatus::STATUS_ENABLED
138
- ),
132
+ ['ds ' => $ this ->getTable ('catalog_product_entity_int ' )],
133
+ "ds.store_id = s.store_id AND ds.attribute_id = dd.attribute_id AND ds. {$ productIdField } = dd. {$ productIdField }" ,
134
+ ['value ' => new \Zend_Db_Expr ('COALESCE(ds.value, dd.value) ' )]
135
+ )->joinLeft (
136
+ ['d2d ' => $ this ->getTable ('catalog_product_entity_int ' )],
137
+ sprintf ("d2d.store_id = 0 AND d2d. {$ productIdField } = dd. {$ productIdField } AND d2d.attribute_id = %s " , $ this ->_eavConfig ->getAttribute (\Magento \Catalog \Model \Product::ENTITY , 'status ' )->getId ()),
138
+ []
139
+ )->joinLeft (
140
+ ['d2s ' => $ this ->getTable ('catalog_product_entity_int ' )],
141
+ "d2s.store_id = s.store_id AND d2s.attribute_id = d2d.attribute_id AND d2s. {$ productIdField } = d2d. {$ productIdField }" ,
139
142
[]
140
143
)->joinLeft (
141
144
['cpe ' => $ this ->getTable ('catalog_product_entity ' )],
142
- "cpe. {$ productIdField } = d . {$ productIdField }" ,
145
+ "cpe. {$ productIdField } = dd . {$ productIdField }" ,
143
146
array_unique ([$ productIdField , 'entity_id ' ])
144
147
)->where (
145
148
's.store_id != 0 '
146
149
)->where (
147
- 'd .value IS NOT NULL '
150
+ '(ds .value IS NOT NULL OR dd.value IS NOT NULL) '
148
151
)->where (
149
- 'd2.value IS NOT NULL '
150
- )->group ([
151
- 's.store_id ' , 's.website_id ' , 'cpe.entity_id ' , 'd.attribute_id ' , 'd.value ' ,
152
- ]);
152
+ (new \Zend_Db_Expr ('COALESCE(d2s.value, d2d.value) ' )) . ' = ' . ProductStatus::STATUS_ENABLED
153
+ )->distinct (true );
153
154
154
155
if ($ entityIds !== null ) {
155
156
$ subSelect ->where ('cpe.entity_id IN(?) ' , $ entityIds );
0 commit comments