@@ -228,61 +228,90 @@ protected function _prepareTierPriceIndex($entityIds = null)
228
228
{
229
229
$ table = $ this ->_defaultIndexerResource ->getTable ('catalog_product_index_tier_price ' );
230
230
$ this ->_emptyTable ($ table );
231
-
232
- $ tierPriceExpression = $ this ->_connection ->getCheckSql (
233
- 'tp.value = 0 ' ,
234
- 'product_price.value * (1 - tp.percentage_value / 100) ' ,
235
- 'tp.value '
236
- );
237
- $ websiteExpression = $ this ->_connection ->getCheckSql (
238
- 'tp.website_id = 0 ' ,
239
- 'ROUND( ' . $ tierPriceExpression . ' * cwd.rate, 4) ' ,
240
- $ tierPriceExpression
241
- );
231
+ if (empty ($ entityIds )) {
232
+ return $ this ;
233
+ }
242
234
$ linkField = $ this ->getProductIdFieldName ();
243
235
$ priceAttribute = $ this ->getProductResource ()->getAttribute ('price ' );
244
-
245
- $ select = $ this ->_connection ->select ()->from (
236
+ $ baseColumns = [
237
+ 'cpe.entity_id ' ,
238
+ 'tp.customer_group_id ' ,
239
+ 'tp.website_id '
240
+ ];
241
+ if ($ linkField !== 'entity_id ' ) {
242
+ $ baseColumns [] = 'cpe. ' . $ linkField ;
243
+ };
244
+ $ subSelect = $ this ->_connection ->select ()->from (
246
245
['cpe ' => $ this ->_defaultIndexerResource ->getTable ('catalog_product_entity ' )],
247
- ['cpe.entity_id ' ]
248
- )->join (
246
+ array_merge_recursive (
247
+ $ baseColumns ,
248
+ [
249
+ 'min(tp.value) AS value ' ,
250
+ 'min(tp.percentage_value) AS percentage_value '
251
+ ]
252
+ )
253
+ )->joinInner (
249
254
['tp ' => $ this ->_defaultIndexerResource ->getTable (['catalog_product_entity ' , 'tier_price ' ])],
250
255
'tp. ' . $ linkField . ' = cpe. ' . $ linkField ,
251
256
[]
252
- )->join (
253
- ['cg ' => $ this ->_defaultIndexerResource ->getTable ('customer_group ' )],
254
- 'tp.all_groups = 1 OR (tp.all_groups = 0 AND tp.customer_group_id = cg.customer_group_id) ' ,
255
- ['customer_group_id ' ]
256
- )->join (
257
- ['cw ' => $ this ->_defaultIndexerResource ->getTable ('store_website ' )],
258
- 'tp.website_id = 0 OR tp.website_id = cw.website_id ' ,
259
- ['website_id ' ]
260
- )->join (
261
- ['cwd ' => $ this ->_defaultIndexerResource ->getTable ('catalog_product_index_website ' )],
262
- 'cw.website_id = cwd.website_id ' ,
263
- []
264
- )->join (
265
- ['product_price ' => $ priceAttribute ->getBackend ()->getTable ()],
266
- 'tp. ' . $ linkField . ' = product_price. ' . $ linkField ,
267
- []
268
- )->where (
269
- 'cw.website_id != 0 '
270
- )->where (
271
- 'product_price.attribute_id = ? ' ,
272
- $ priceAttribute ->getAttributeId ()
273
- )->columns (
274
- new \Zend_Db_Expr ("MIN( {$ websiteExpression }) " )
275
- )->group (
276
- ['cpe.entity_id ' , 'cg.customer_group_id ' , 'cw.website_id ' ]
277
- );
257
+ )->where ("cpe.entity_id IN(?) " , $ entityIds )
258
+ ->where ("tp.website_id != 0 " )
259
+ ->group (['cpe.entity_id ' , 'tp.customer_group_id ' , 'tp.website_id ' ]);
260
+
261
+ $ subSelect2 = $ this ->_connection ->select ()
262
+ ->from (
263
+ ['cpe ' => $ this ->_defaultIndexerResource ->getTable ('catalog_product_entity ' )],
264
+ array_merge_recursive (
265
+ $ baseColumns ,
266
+ [
267
+ 'MIN(ROUND(tp.value * cwd.rate, 4)) AS value ' ,
268
+ 'MIN(ROUND(tp.percentage_value * cwd.rate, 4)) AS percentage_value '
269
+
270
+ ]
271
+ )
272
+ )
273
+ ->joinInner (
274
+ ['tp ' => $ this ->_defaultIndexerResource ->getTable (['catalog_product_entity ' , 'tier_price ' ])],
275
+ 'tp. ' . $ linkField . ' = cpe. ' . $ linkField ,
276
+ []
277
+ )->join (
278
+ ['cw ' => $ this ->_defaultIndexerResource ->getTable ('store_website ' )],
279
+ true ,
280
+ []
281
+ )
282
+ ->joinInner (
283
+ ['cwd ' => $ this ->_defaultIndexerResource ->getTable ('catalog_product_index_website ' )],
284
+ 'cw.website_id = cwd.website_id ' ,
285
+ []
286
+ )
287
+ ->where ("cpe.entity_id IN(?) " , $ entityIds )
288
+ ->where ("tp.website_id = 0 " )
289
+ ->group (
290
+ ['cpe.entity_id ' , 'tp.customer_group_id ' , 'tp.website_id ' ]
291
+ );
278
292
279
- if (!empty ($ entityIds )) {
280
- $ select ->where ("cpe.entity_id IN(?) " , $ entityIds );
281
- }
293
+ $ unionSelect = $ this ->_connection ->select ()
294
+ ->union ([$ subSelect , $ subSelect2 ], \Magento \Framework \DB \Select::SQL_UNION_ALL );
295
+ $ select = $ this ->_connection ->select ()
296
+ ->from (
297
+ ['b ' => new \Zend_Db_Expr (sprintf ('(%s) ' , $ unionSelect ->assemble ()))],
298
+ [
299
+ 'b.entity_id ' ,
300
+ 'b.customer_group_id ' ,
301
+ 'b.website_id ' ,
302
+ 'MIN(IF(b.value = 0, product_price.value * (1 - b.percentage_value / 100), b.value)) '
303
+ ]
304
+ )
305
+ ->joinInner (
306
+ ['product_price ' => $ priceAttribute ->getBackend ()->getTable ()],
307
+ 'b. ' . $ linkField . ' = product_price. ' . $ linkField ,
308
+ []
309
+ )
310
+ ->group (['b.entity_id ' , 'b.customer_group_id ' , 'b.website_id ' ]);
311
+
312
+ $ query = $ select ->insertFromSelect ($ table , [], false );
282
313
283
- $ query = $ select ->insertFromSelect ($ table );
284
314
$ this ->_connection ->query ($ query );
285
-
286
315
return $ this ;
287
316
}
288
317
0 commit comments