5
5
*/
6
6
namespace Magento \Catalog \Model \Indexer \Product \Price ;
7
7
8
+ use Magento \Framework \App \ObjectManager ;
9
+
8
10
/**
9
11
* Abstract action reindex class
10
12
*
@@ -71,9 +73,9 @@ abstract class AbstractAction
71
73
protected $ _indexers ;
72
74
73
75
/**
74
- * @var \Magento\Catalog\Model\ResourceModel\Product
76
+ * @var \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\TierPrice
75
77
*/
76
- private $ productResource ;
78
+ private $ tierPriceIndexResource ;
77
79
78
80
/**
79
81
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
@@ -84,6 +86,7 @@ abstract class AbstractAction
84
86
* @param \Magento\Catalog\Model\Product\Type $catalogProductType
85
87
* @param \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\Factory $indexerPriceFactory
86
88
* @param \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice $defaultIndexerResource
89
+ * @param \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\TierPrice $tierPriceIndexResource
87
90
*/
88
91
public function __construct (
89
92
\Magento \Framework \App \Config \ScopeConfigInterface $ config ,
@@ -93,7 +96,8 @@ public function __construct(
93
96
\Magento \Framework \Stdlib \DateTime $ dateTime ,
94
97
\Magento \Catalog \Model \Product \Type $ catalogProductType ,
95
98
\Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \Factory $ indexerPriceFactory ,
96
- \Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \DefaultPrice $ defaultIndexerResource
99
+ \Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \DefaultPrice $ defaultIndexerResource ,
100
+ \Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \TierPrice $ tierPriceIndexResource = null
97
101
) {
98
102
$ this ->_config = $ config ;
99
103
$ this ->_storeManager = $ storeManager ;
@@ -104,6 +108,9 @@ public function __construct(
104
108
$ this ->_indexerPriceFactory = $ indexerPriceFactory ;
105
109
$ this ->_defaultIndexerResource = $ defaultIndexerResource ;
106
110
$ this ->_connection = $ this ->_defaultIndexerResource ->getConnection ();
111
+ $ this ->tierPriceIndexResource = $ tierPriceIndexResource ?: ObjectManager::getInstance ()->get (
112
+ \Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \TierPrice::class
113
+ );
107
114
}
108
115
109
116
/**
@@ -215,92 +222,8 @@ protected function _prepareWebsiteDateTable()
215
222
*/
216
223
protected function _prepareTierPriceIndex ($ entityIds = null )
217
224
{
218
- $ table = $ this ->_defaultIndexerResource ->getTable ('catalog_product_index_tier_price ' );
219
- $ this ->_emptyTable ($ table );
220
- if (empty ($ entityIds )) {
221
- return $ this ;
222
- }
223
- $ linkField = $ this ->getProductIdFieldName ();
224
- $ priceAttribute = $ this ->getProductResource ()->getAttribute ('price ' );
225
- $ baseColumns = [
226
- 'cpe.entity_id ' ,
227
- 'tp.customer_group_id ' ,
228
- 'tp.website_id '
229
- ];
230
- if ($ linkField !== 'entity_id ' ) {
231
- $ baseColumns [] = 'cpe. ' . $ linkField ;
232
- };
233
- $ subSelect = $ this ->_connection ->select ()->from (
234
- ['cpe ' => $ this ->_defaultIndexerResource ->getTable ('catalog_product_entity ' )],
235
- array_merge_recursive (
236
- $ baseColumns ,
237
- [
238
- 'min(tp.value) AS value ' ,
239
- 'min(tp.percentage_value) AS percentage_value '
240
- ]
241
- )
242
- )->joinInner (
243
- ['tp ' => $ this ->_defaultIndexerResource ->getTable (['catalog_product_entity ' , 'tier_price ' ])],
244
- 'tp. ' . $ linkField . ' = cpe. ' . $ linkField ,
245
- []
246
- )->where ("cpe.entity_id IN(?) " , $ entityIds )
247
- ->where ("tp.website_id != 0 " )
248
- ->group (['cpe.entity_id ' , 'tp.customer_group_id ' , 'tp.website_id ' ]);
249
-
250
- $ subSelect2 = $ this ->_connection ->select ()
251
- ->from (
252
- ['cpe ' => $ this ->_defaultIndexerResource ->getTable ('catalog_product_entity ' )],
253
- array_merge_recursive (
254
- $ baseColumns ,
255
- [
256
- 'MIN(ROUND(tp.value * cwd.rate, 4)) AS value ' ,
257
- 'MIN(ROUND(tp.percentage_value * cwd.rate, 4)) AS percentage_value '
258
-
259
- ]
260
- )
261
- )
262
- ->joinInner (
263
- ['tp ' => $ this ->_defaultIndexerResource ->getTable (['catalog_product_entity ' , 'tier_price ' ])],
264
- 'tp. ' . $ linkField . ' = cpe. ' . $ linkField ,
265
- []
266
- )->join (
267
- ['cw ' => $ this ->_defaultIndexerResource ->getTable ('store_website ' )],
268
- true ,
269
- []
270
- )
271
- ->joinInner (
272
- ['cwd ' => $ this ->_defaultIndexerResource ->getTable ('catalog_product_index_website ' )],
273
- 'cw.website_id = cwd.website_id ' ,
274
- []
275
- )
276
- ->where ("cpe.entity_id IN(?) " , $ entityIds )
277
- ->where ("tp.website_id = 0 " )
278
- ->group (
279
- ['cpe.entity_id ' , 'tp.customer_group_id ' , 'tp.website_id ' ]
280
- );
281
-
282
- $ unionSelect = $ this ->_connection ->select ()
283
- ->union ([$ subSelect , $ subSelect2 ], \Magento \Framework \DB \Select::SQL_UNION_ALL );
284
- $ select = $ this ->_connection ->select ()
285
- ->from (
286
- ['b ' => new \Zend_Db_Expr (sprintf ('(%s) ' , $ unionSelect ->assemble ()))],
287
- [
288
- 'b.entity_id ' ,
289
- 'b.customer_group_id ' ,
290
- 'b.website_id ' ,
291
- 'MIN(IF(b.value = 0, product_price.value * (1 - b.percentage_value / 100), b.value)) '
292
- ]
293
- )
294
- ->joinInner (
295
- ['product_price ' => $ priceAttribute ->getBackend ()->getTable ()],
296
- 'b. ' . $ linkField . ' = product_price. ' . $ linkField ,
297
- []
298
- )
299
- ->group (['b.entity_id ' , 'b.customer_group_id ' , 'b.website_id ' ]);
300
-
301
- $ query = $ select ->insertFromSelect ($ table , [], false );
225
+ $ this ->tierPriceIndexResource ->reindexEntity ((array ) $ entityIds );
302
226
303
- $ this ->_connection ->query ($ query );
304
227
return $ this ;
305
228
}
306
229
@@ -391,30 +314,17 @@ protected function _emptyTable($table)
391
314
*
392
315
* @param array $changedIds
393
316
* @return array Affected ids
394
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
395
317
*/
396
318
protected function _reindexRows ($ changedIds = [])
397
319
{
398
320
$ this ->_emptyTable ($ this ->_defaultIndexerResource ->getIdxTable ());
399
321
$ this ->_prepareWebsiteDateTable ();
400
322
401
- $ select = $ this ->_connection ->select ()->from (
402
- $ this ->_defaultIndexerResource ->getTable ('catalog_product_entity ' ),
403
- ['entity_id ' , 'type_id ' ]
404
- )->where (
405
- 'entity_id IN(?) ' ,
406
- $ changedIds
407
- );
408
- $ pairs = $ this ->_connection ->fetchPairs ($ select );
409
- $ byType = [];
410
- foreach ($ pairs as $ productId => $ productType ) {
411
- $ byType [$ productType ][$ productId ] = $ productId ;
412
- }
413
-
323
+ $ productsTypes = $ this ->getProductsTypes ($ changedIds );
414
324
$ compositeIds = [];
415
325
$ notCompositeIds = [];
416
326
417
- foreach ($ byType as $ productType => $ entityIds ) {
327
+ foreach ($ productsTypes as $ productType => $ entityIds ) {
418
328
$ indexer = $ this ->_getIndexer ($ productType );
419
329
if ($ indexer ->getIsComposite ()) {
420
330
$ compositeIds += $ entityIds ;
@@ -424,37 +334,21 @@ protected function _reindexRows($changedIds = [])
424
334
}
425
335
426
336
if (!empty ($ notCompositeIds )) {
427
- $ select = $ this ->_connection ->select ()->from (
428
- ['l ' => $ this ->_defaultIndexerResource ->getTable ('catalog_product_relation ' )],
429
- ''
430
- )->join (
431
- ['e ' => $ this ->_defaultIndexerResource ->getTable ('catalog_product_entity ' )],
432
- 'e. ' . $ this ->getProductIdFieldName () . ' = l.parent_id ' ,
433
- ['e.entity_id as parent_id ' , 'type_id ' ]
434
- )->where (
435
- 'l.child_id IN(?) ' ,
436
- $ notCompositeIds
437
- );
438
- $ pairs = $ this ->_connection ->fetchPairs ($ select );
439
- foreach ($ pairs as $ productId => $ productType ) {
440
- if (!in_array ($ productId , $ changedIds )) {
441
- $ changedIds [] = (string ) $ productId ;
442
- $ byType [$ productType ][$ productId ] = $ productId ;
443
- $ compositeIds [$ productId ] = $ productId ;
444
- }
445
- }
337
+ $ parentProductsTypes = $ this ->getParentProductsTypes ($ notCompositeIds );
338
+ $ productsTypes = array_merge_recursive ($ productsTypes , $ parentProductsTypes );
339
+ $ parentProductsIds = array_keys ($ parentProductsTypes );
340
+ $ compositeIds = $ compositeIds + array_combine ($ parentProductsIds , $ parentProductsIds );
341
+ $ changedIds = array_merge ($ changedIds , $ parentProductsIds );
446
342
}
447
343
448
344
if (!empty ($ compositeIds )) {
449
345
$ this ->_copyRelationIndexData ($ compositeIds , $ notCompositeIds );
450
346
}
451
347
$ this ->_prepareTierPriceIndex ($ compositeIds + $ notCompositeIds );
452
348
453
- $ indexers = $ this ->getTypeIndexers ();
454
- foreach ($ indexers as $ indexer ) {
455
- if (!empty ($ byType [$ indexer ->getTypeId ()])) {
456
- $ indexer ->reindexEntity ($ byType [$ indexer ->getTypeId ()]);
457
- }
349
+ foreach ($ productsTypes as $ productType => $ entityIds ) {
350
+ $ indexer = $ this ->_getIndexer ($ productType );
351
+ $ indexer ->reindexEntity ($ entityIds );
458
352
}
459
353
$ this ->_syncData ($ changedIds );
460
354
@@ -524,15 +418,56 @@ protected function getProductIdFieldName()
524
418
}
525
419
526
420
/**
527
- * @return \Magento\Catalog\Model\ResourceModel\Product
528
- * @deprecated 101.1.0
421
+ * Get products types.
422
+ *
423
+ * @param array $changedIds
424
+ * @return array
425
+ */
426
+ private function getProductsTypes (array $ changedIds = [])
427
+ {
428
+ $ select = $ this ->_connection ->select ()->from (
429
+ $ this ->_defaultIndexerResource ->getTable ('catalog_product_entity ' ),
430
+ ['entity_id ' , 'type_id ' ]
431
+ );
432
+ if ($ changedIds ) {
433
+ $ select ->where ('entity_id IN (?) ' , $ changedIds );
434
+ }
435
+ $ pairs = $ this ->_connection ->fetchPairs ($ select );
436
+
437
+ $ byType = [];
438
+ foreach ($ pairs as $ productId => $ productType ) {
439
+ $ byType [$ productType ][$ productId ] = $ productId ;
440
+ }
441
+
442
+ return $ byType ;
443
+ }
444
+
445
+ /**
446
+ * Get parent products types.
447
+ *
448
+ * @param array $productsIds
449
+ * @return array
529
450
*/
530
- private function getProductResource ( )
451
+ private function getParentProductsTypes ( array $ productsIds )
531
452
{
532
- if (null === $ this ->productResource ) {
533
- $ this ->productResource = \Magento \Framework \App \ObjectManager::getInstance ()
534
- ->get (\Magento \Catalog \Model \ResourceModel \Product::class);
453
+ $ select = $ this ->_connection ->select ()->from (
454
+ ['l ' => $ this ->_defaultIndexerResource ->getTable ('catalog_product_relation ' )],
455
+ ''
456
+ )->join (
457
+ ['e ' => $ this ->_defaultIndexerResource ->getTable ('catalog_product_entity ' )],
458
+ 'e. ' . $ this ->getProductIdFieldName () . ' = l.parent_id ' ,
459
+ ['e.entity_id as parent_id ' , 'type_id ' ]
460
+ )->where (
461
+ 'l.child_id IN(?) ' ,
462
+ $ productsIds
463
+ );
464
+ $ pairs = $ this ->_connection ->fetchPairs ($ select );
465
+
466
+ $ byType = [];
467
+ foreach ($ pairs as $ productId => $ productType ) {
468
+ $ byType [$ productType ][$ productId ] = $ productId ;
535
469
}
536
- return $ this ->productResource ;
470
+
471
+ return $ byType ;
537
472
}
538
473
}
0 commit comments