@@ -242,7 +242,22 @@ public function __construct(
242
242
*/
243
243
public function reindexById ($ id )
244
244
{
245
- $ this ->reindexByIds ([$ id ]);
245
+ try {
246
+ $ this ->cleanProductIndex ([$ id ]);
247
+
248
+ $ products = $ this ->productLoader ->getProducts ([$ id ]);
249
+ $ activeRules = $ this ->getActiveRules ();
250
+ foreach ($ products as $ product ) {
251
+ $ this ->applyRules ($ activeRules , $ product );
252
+ }
253
+
254
+ $ this ->reindexRuleGroupWebsite ->execute ();
255
+ } catch (\Exception $ e ) {
256
+ $ this ->critical ($ e );
257
+ throw new \Magento \Framework \Exception \LocalizedException (
258
+ __ ('Catalog rule indexing failed. See details in exception log. ' )
259
+ );
260
+ }
246
261
}
247
262
248
263
/**
@@ -275,11 +290,18 @@ protected function doReindexByIds($ids)
275
290
{
276
291
$ this ->cleanProductIndex ($ ids );
277
292
278
- $ products = $ this ->productLoader ->getProducts ($ ids );
279
- $ activeRules = $ this ->getActiveRules ();
280
- foreach ($ products as $ product ) {
281
- $ this ->applyRules ($ activeRules , $ product );
293
+ /** @var Rule[] $activeRules */
294
+ $ activeRules = $ this ->getActiveRules ()->getItems ();
295
+ foreach ($ activeRules as $ rule ) {
296
+ $ rule ->setProductsFilter ($ ids );
297
+ $ this ->reindexRuleProduct ->execute ($ rule , $ this ->batchCount );
282
298
}
299
+
300
+ foreach ($ ids as $ productId ) {
301
+ $ this ->cleanProductPriceIndex ([$ productId ]);
302
+ $ this ->reindexRuleProductPrice ->execute ($ this ->batchCount , $ productId );
303
+ }
304
+
283
305
$ this ->reindexRuleGroupWebsite ->execute ();
284
306
}
285
307
@@ -365,17 +387,13 @@ protected function cleanByIds($productIds)
365
387
* Assign product to rule
366
388
*
367
389
* @param Rule $rule
368
- * @param Product $product
390
+ * @param int $productEntityId
391
+ * @param array $websiteIds
369
392
* @return void
370
393
*/
371
- private function assignProductToRule (Rule $ rule , Product $ product ): void
394
+ private function assignProductToRule (Rule $ rule , int $ productEntityId , array $ websiteIds ): void
372
395
{
373
- if (!$ rule ->validate ($ product )) {
374
- return ;
375
- }
376
-
377
396
$ ruleId = (int ) $ rule ->getId ();
378
- $ productEntityId = (int ) $ product ->getId ();
379
397
$ ruleProductTable = $ this ->getTable ('catalogrule_product ' );
380
398
$ this ->connection ->delete (
381
399
$ ruleProductTable ,
@@ -385,7 +403,6 @@ private function assignProductToRule(Rule $rule, Product $product): void
385
403
]
386
404
);
387
405
388
- $ websiteIds = array_intersect ($ product ->getWebsiteIds (), $ rule ->getWebsiteIds ());
389
406
$ customerGroupIds = $ rule ->getCustomerGroupIds ();
390
407
$ fromTime = strtotime ($ rule ->getFromDate ());
391
408
$ toTime = strtotime ($ rule ->getToDate ());
@@ -429,12 +446,17 @@ private function assignProductToRule(Rule $rule, Product $product): void
429
446
* @param Product $product
430
447
* @return $this
431
448
* @throws \Exception
449
+ * @deprecated
450
+ * @see ReindexRuleProduct::execute
432
451
* @SuppressWarnings(PHPMD.NPathComplexity)
433
452
*/
434
453
protected function applyRule (Rule $ rule , $ product )
435
454
{
436
- $ this ->assignProductToRule ($ rule , $ product );
437
- $ this ->reindexRuleProductPrice ->execute ($ this ->batchCount , $ product );
455
+ if ($ rule ->validate ($ product )) {
456
+ $ websiteIds = array_intersect ($ product ->getWebsiteIds (), $ rule ->getWebsiteIds ());
457
+ $ this ->assignProductToRule ($ rule , $ product ->getId (), $ websiteIds );
458
+ }
459
+ $ this ->reindexRuleProductPrice ->execute ($ this ->batchCount , $ product ->getId ());
438
460
$ this ->reindexRuleGroupWebsite ->execute ();
439
461
440
462
return $ this ;
@@ -450,11 +472,16 @@ protected function applyRule(Rule $rule, $product)
450
472
private function applyRules (RuleCollection $ ruleCollection , Product $ product ): void
451
473
{
452
474
foreach ($ ruleCollection as $ rule ) {
453
- $ this ->assignProductToRule ($ rule , $ product );
475
+ if (!$ rule ->validate ($ product )) {
476
+ continue ;
477
+ }
478
+
479
+ $ websiteIds = array_intersect ($ product ->getWebsiteIds (), $ rule ->getWebsiteIds ());
480
+ $ this ->assignProductToRule ($ rule , $ product ->getId (), $ websiteIds );
454
481
}
455
482
456
483
$ this ->cleanProductPriceIndex ([$ product ->getId ()]);
457
- $ this ->reindexRuleProductPrice ->execute ($ this ->batchCount , $ product );
484
+ $ this ->reindexRuleProductPrice ->execute ($ this ->batchCount , $ product-> getId () );
458
485
}
459
486
460
487
/**
@@ -507,7 +534,7 @@ protected function updateRuleProductData(Rule $rule)
507
534
*/
508
535
protected function applyAllRules (Product $ product = null )
509
536
{
510
- $ this ->reindexRuleProductPrice ->execute ($ this ->batchCount , $ product );
537
+ $ this ->reindexRuleProductPrice ->execute ($ this ->batchCount , $ product-> getId () );
511
538
$ this ->reindexRuleGroupWebsite ->execute ();
512
539
return $ this ;
513
540
}
@@ -562,7 +589,7 @@ protected function calcRuleProductPrice($ruleData, $productData = null)
562
589
*/
563
590
protected function getRuleProductsStmt ($ websiteId , Product $ product = null )
564
591
{
565
- return $ this ->ruleProductsSelectBuilder ->build ($ websiteId , $ product );
592
+ return $ this ->ruleProductsSelectBuilder ->build (( int ) $ websiteId , ( int ) $ product-> getId () );
566
593
}
567
594
568
595
/**
0 commit comments