7
7
namespace Magento \CatalogRule \Model \Indexer ;
8
8
9
9
use Magento \Catalog \Model \Product ;
10
+ use Magento \CatalogRule \Model \ResourceModel \Rule \Collection as RuleCollection ;
10
11
use Magento \CatalogRule \Model \ResourceModel \Rule \CollectionFactory as RuleCollectionFactory ;
11
12
use Magento \CatalogRule \Model \Rule ;
12
13
use Magento \Framework \App \ObjectManager ;
@@ -270,14 +271,14 @@ public function reindexByIds(array $ids)
270
271
*/
271
272
protected function doReindexByIds ($ ids )
272
273
{
273
- $ this ->cleanByIds ($ ids );
274
+ $ this ->cleanProductIndex ($ ids );
274
275
275
276
$ products = $ this ->productLoader ->getProducts ($ ids );
276
- foreach ($ this ->getActiveRules () as $ rule ) {
277
- foreach ($ products as $ product ) {
278
- $ this ->applyRule ($ rule , $ product );
279
- }
277
+ $ activeRules = $ this ->getActiveRules ();
278
+ foreach ($ products as $ product ) {
279
+ $ this ->applyRules ($ activeRules , $ product );
280
280
}
281
+ $ this ->reindexRuleGroupWebsite ->execute ();
281
282
}
282
283
283
284
/**
@@ -322,6 +323,30 @@ protected function doReindexFull()
322
323
);
323
324
}
324
325
326
+ /**
327
+ * Clean product index
328
+ *
329
+ * @param array $productIds
330
+ * @return void
331
+ */
332
+ private function cleanProductIndex (array $ productIds ): void
333
+ {
334
+ $ where = ['product_id IN (?) ' => $ productIds ];
335
+ $ this ->connection ->delete ($ this ->getTable ('catalogrule_product ' ), $ where );
336
+ }
337
+
338
+ /**
339
+ * Clean product price index
340
+ *
341
+ * @param array $productIds
342
+ * @return void
343
+ */
344
+ private function cleanProductPriceIndex (array $ productIds ): void
345
+ {
346
+ $ where = ['product_id IN (?) ' => $ productIds ];
347
+ $ this ->connection ->delete ($ this ->getTable ('catalogrule_product_price ' ), $ where );
348
+ }
349
+
325
350
/**
326
351
* Clean by product ids
327
352
*
@@ -330,51 +355,35 @@ protected function doReindexFull()
330
355
*/
331
356
protected function cleanByIds ($ productIds )
332
357
{
333
- $ query = $ this ->connection ->deleteFromSelect (
334
- $ this ->connection
335
- ->select ()
336
- ->from ($ this ->resource ->getTableName ('catalogrule_product ' ), 'product_id ' )
337
- ->distinct ()
338
- ->where ('product_id IN (?) ' , $ productIds ),
339
- $ this ->resource ->getTableName ('catalogrule_product ' )
340
- );
341
- $ this ->connection ->query ($ query );
342
-
343
- $ query = $ this ->connection ->deleteFromSelect (
344
- $ this ->connection ->select ()
345
- ->from ($ this ->resource ->getTableName ('catalogrule_product_price ' ), 'product_id ' )
346
- ->distinct ()
347
- ->where ('product_id IN (?) ' , $ productIds ),
348
- $ this ->resource ->getTableName ('catalogrule_product_price ' )
349
- );
350
- $ this ->connection ->query ($ query );
358
+ $ this ->cleanProductIndex ($ productIds );
359
+ $ this ->cleanProductPriceIndex ($ productIds );
351
360
}
352
361
353
362
/**
363
+ * Assign product to rule
364
+ *
354
365
* @param Rule $rule
355
366
* @param Product $product
356
- * @return $this
357
- * @throws \Exception
358
- * @SuppressWarnings(PHPMD.NPathComplexity)
367
+ * @return void
359
368
*/
360
- protected function applyRule (Rule $ rule , $ product )
369
+ private function assignProductToRule (Rule $ rule , Product $ product ): void
361
370
{
362
- $ ruleId = $ rule ->getId ();
363
- $ productEntityId = $ product ->getId ();
364
- $ websiteIds = array_intersect ($ product ->getWebsiteIds (), $ rule ->getWebsiteIds ());
365
-
366
371
if (!$ rule ->validate ($ product )) {
367
- return $ this ;
372
+ return ;
368
373
}
369
374
375
+ $ ruleId = (int ) $ rule ->getId ();
376
+ $ productEntityId = (int ) $ product ->getId ();
377
+ $ ruleProductTable = $ this ->getTable ('catalogrule_product ' );
370
378
$ this ->connection ->delete (
371
- $ this -> resource -> getTableName ( ' catalogrule_product ' ) ,
379
+ $ ruleProductTable ,
372
380
[
373
- $ this -> connection -> quoteInto ( 'rule_id = ? ' , $ ruleId) ,
374
- $ this -> connection -> quoteInto ( 'product_id = ? ' , $ productEntityId)
381
+ 'rule_id = ? ' => $ ruleId ,
382
+ 'product_id = ? ' => $ productEntityId,
375
383
]
376
384
);
377
385
386
+ $ websiteIds = array_intersect ($ product ->getWebsiteIds (), $ rule ->getWebsiteIds ());
378
387
$ customerGroupIds = $ rule ->getCustomerGroupIds ();
379
388
$ fromTime = strtotime ($ rule ->getFromDate ());
380
389
$ toTime = strtotime ($ rule ->getToDate ());
@@ -385,43 +394,70 @@ protected function applyRule(Rule $rule, $product)
385
394
$ actionStop = $ rule ->getStopRulesProcessing ();
386
395
387
396
$ rows = [];
388
- try {
389
- foreach ($ websiteIds as $ websiteId ) {
390
- foreach ($ customerGroupIds as $ customerGroupId ) {
391
- $ rows [] = [
392
- 'rule_id ' => $ ruleId ,
393
- 'from_time ' => $ fromTime ,
394
- 'to_time ' => $ toTime ,
395
- 'website_id ' => $ websiteId ,
396
- 'customer_group_id ' => $ customerGroupId ,
397
- 'product_id ' => $ productEntityId ,
398
- 'action_operator ' => $ actionOperator ,
399
- 'action_amount ' => $ actionAmount ,
400
- 'action_stop ' => $ actionStop ,
401
- 'sort_order ' => $ sortOrder ,
402
- ];
403
-
404
- if (count ($ rows ) == $ this ->batchCount ) {
405
- $ this ->connection ->insertMultiple ($ this ->getTable ('catalogrule_product ' ), $ rows );
406
- $ rows = [];
407
- }
397
+ foreach ($ websiteIds as $ websiteId ) {
398
+ foreach ($ customerGroupIds as $ customerGroupId ) {
399
+ $ rows [] = [
400
+ 'rule_id ' => $ ruleId ,
401
+ 'from_time ' => $ fromTime ,
402
+ 'to_time ' => $ toTime ,
403
+ 'website_id ' => $ websiteId ,
404
+ 'customer_group_id ' => $ customerGroupId ,
405
+ 'product_id ' => $ productEntityId ,
406
+ 'action_operator ' => $ actionOperator ,
407
+ 'action_amount ' => $ actionAmount ,
408
+ 'action_stop ' => $ actionStop ,
409
+ 'sort_order ' => $ sortOrder ,
410
+ ];
411
+
412
+ if (count ($ rows ) == $ this ->batchCount ) {
413
+ $ this ->connection ->insertMultiple ($ ruleProductTable , $ rows );
414
+ $ rows = [];
408
415
}
409
416
}
410
-
411
- if (!empty ($ rows )) {
412
- $ this ->connection ->insertMultiple ($ this ->resource ->getTableName ('catalogrule_product ' ), $ rows );
413
- }
414
- } catch (\Exception $ e ) {
415
- throw $ e ;
416
417
}
418
+ if ($ rows ) {
419
+ $ this ->connection ->insertMultiple ($ ruleProductTable , $ rows );
420
+ }
421
+ }
417
422
423
+ /**
424
+ * Apply rule
425
+ *
426
+ * @param Rule $rule
427
+ * @param Product $product
428
+ * @return $this
429
+ * @throws \Exception
430
+ * @SuppressWarnings(PHPMD.NPathComplexity)
431
+ */
432
+ protected function applyRule (Rule $ rule , $ product )
433
+ {
434
+ $ this ->assignProductToRule ($ rule , $ product );
418
435
$ this ->reindexRuleProductPrice ->execute ($ this ->batchCount , $ product );
419
436
$ this ->reindexRuleGroupWebsite ->execute ();
420
437
421
438
return $ this ;
422
439
}
423
440
424
441
/**
442
+ * Apply rules
443
+ *
444
+ * @param RuleCollection $ruleCollection
445
+ * @param Product $product
446
+ * @return void
447
+ */
448
+ private function applyRules (RuleCollection $ ruleCollection , Product $ product ): void
449
+ {
450
+ foreach ($ ruleCollection as $ rule ) {
451
+ $ this ->assignProductToRule ($ rule , $ product );
452
+ }
453
+
454
+ $ this ->cleanProductPriceIndex ([$ product ->getId ()]);
455
+ $ this ->reindexRuleProductPrice ->execute ($ this ->batchCount , $ product );
456
+ }
457
+
458
+ /**
459
+ * Retrieve table name
460
+ *
425
461
* @param string $tableName
426
462
* @return string
427
463
*/
@@ -431,6 +467,8 @@ protected function getTable($tableName)
431
467
}
432
468
433
469
/**
470
+ * Update rule product data
471
+ *
434
472
* @param Rule $rule
435
473
* @return $this
436
474
* @deprecated 100.2.0
@@ -456,6 +494,8 @@ protected function updateRuleProductData(Rule $rule)
456
494
}
457
495
458
496
/**
497
+ * Apply all rules
498
+ *
459
499
* @param Product|null $product
460
500
* @throws \Exception
461
501
* @return $this
@@ -495,6 +535,8 @@ protected function deleteOldData()
495
535
}
496
536
497
537
/**
538
+ * Calculate rule product price
539
+ *
498
540
* @param array $ruleData
499
541
* @param null $productData
500
542
* @return float
@@ -507,6 +549,8 @@ protected function calcRuleProductPrice($ruleData, $productData = null)
507
549
}
508
550
509
551
/**
552
+ * Get rule products statement
553
+ *
510
554
* @param int $websiteId
511
555
* @param Product|null $product
512
556
* @return \Zend_Db_Statement_Interface
@@ -520,6 +564,8 @@ protected function getRuleProductsStmt($websiteId, Product $product = null)
520
564
}
521
565
522
566
/**
567
+ * Save rule product prices
568
+ *
523
569
* @param array $arrData
524
570
* @return $this
525
571
* @throws \Exception
@@ -535,7 +581,7 @@ protected function saveRuleProductPrices($arrData)
535
581
/**
536
582
* Get active rules
537
583
*
538
- * @return array
584
+ * @return RuleCollection
539
585
*/
540
586
protected function getActiveRules ()
541
587
{
@@ -545,14 +591,16 @@ protected function getActiveRules()
545
591
/**
546
592
* Get active rules
547
593
*
548
- * @return array
594
+ * @return RuleCollection
549
595
*/
550
596
protected function getAllRules ()
551
597
{
552
598
return $ this ->ruleCollectionFactory ->create ();
553
599
}
554
600
555
601
/**
602
+ * Get product
603
+ *
556
604
* @param int $productId
557
605
* @return Product
558
606
*/
@@ -565,6 +613,8 @@ protected function getProduct($productId)
565
613
}
566
614
567
615
/**
616
+ * Log critical exception
617
+ *
568
618
* @param \Exception $e
569
619
* @return void
570
620
*/
0 commit comments