6
6
7
7
namespace Magento \CatalogRule \Model \Indexer ;
8
8
9
+ use Exception ;
9
10
use Magento \Catalog \Model \Product ;
10
11
use Magento \Catalog \Model \ProductFactory ;
11
12
use Magento \Catalog \Model \ResourceModel \Indexer \ActiveTableSwitcher ;
28
29
use Magento \Store \Model \ScopeInterface ;
29
30
use Magento \Store \Model \StoreManagerInterface ;
30
31
use Psr \Log \LoggerInterface ;
32
+ use Zend_Db_Statement_Exception ;
31
33
32
34
/**
33
35
* Catalog rule index builder
@@ -181,6 +183,16 @@ class IndexBuilder
181
183
*/
182
184
private $ productCollectionFactory ;
183
185
186
+ /**
187
+ * @var ReindexRuleProductsPrice
188
+ */
189
+ private $ reindexRuleProductsPrice ;
190
+
191
+ /**
192
+ * @var int
193
+ */
194
+ private $ productBatchSize ;
195
+
184
196
/**
185
197
* @param RuleCollectionFactory $ruleCollectionFactory
186
198
* @param PriceCurrencyInterface $priceCurrency
@@ -204,6 +216,8 @@ class IndexBuilder
204
216
* @param TimezoneInterface|null $localeDate
205
217
* @param ProductCollectionFactory|null $productCollectionFactory
206
218
* @param IndexerRegistry|null $indexerRegistry
219
+ * @param ReindexRuleProductsPrice|null $reindexRuleProductsPrice
220
+ * @param int $productBatchSize
207
221
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
208
222
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
209
223
*/
@@ -229,7 +243,9 @@ public function __construct(
229
243
TableSwapper $ tableSwapper = null ,
230
244
TimezoneInterface $ localeDate = null ,
231
245
ProductCollectionFactory $ productCollectionFactory = null ,
232
- IndexerRegistry $ indexerRegistry = null
246
+ IndexerRegistry $ indexerRegistry = null ,
247
+ ReindexRuleProductsPrice $ reindexRuleProductsPrice = null ,
248
+ int $ productBatchSize = 1000
233
249
) {
234
250
$ this ->resource = $ resource ;
235
251
$ this ->connection = $ resource ->getConnection ();
@@ -242,6 +258,7 @@ public function __construct(
242
258
$ this ->dateTime = $ dateTime ;
243
259
$ this ->productFactory = $ productFactory ;
244
260
$ this ->batchCount = $ batchCount ;
261
+ $ this ->productBatchSize = $ productBatchSize ;
245
262
246
263
$ this ->productPriceCalculator = $ productPriceCalculator ?? ObjectManager::getInstance ()->get (
247
264
ProductPriceCalculator::class
@@ -275,6 +292,8 @@ public function __construct(
275
292
ObjectManager::getInstance ()->get (IndexerRegistry::class);
276
293
$ this ->productCollectionFactory = $ productCollectionFactory ??
277
294
ObjectManager::getInstance ()->get (ProductCollectionFactory::class);
295
+ $ this ->reindexRuleProductsPrice = $ reindexRuleProductsPrice ??
296
+ ObjectManager::getInstance ()->get (ReindexRuleProductsPrice::class);
278
297
}
279
298
280
299
/**
@@ -296,7 +315,7 @@ public function reindexById($id)
296
315
}
297
316
298
317
$ this ->reindexRuleGroupWebsite ->execute ();
299
- } catch (\ Exception $ e ) {
318
+ } catch (Exception $ e ) {
300
319
$ this ->critical ($ e );
301
320
throw new LocalizedException (
302
321
__ ('Catalog rule indexing failed. See details in exception log. ' )
@@ -315,7 +334,7 @@ public function reindexByIds(array $ids)
315
334
{
316
335
try {
317
336
$ this ->doReindexByIds ($ ids );
318
- } catch (\ Exception $ e ) {
337
+ } catch (Exception $ e ) {
319
338
$ this ->critical ($ e );
320
339
throw new LocalizedException (
321
340
__ ("Catalog rule indexing failed. See details in exception log. " )
@@ -328,6 +347,8 @@ public function reindexByIds(array $ids)
328
347
*
329
348
* @param array $ids
330
349
* @return void
350
+ * @throws LocalizedException
351
+ * @throws Zend_Db_Statement_Exception
331
352
*/
332
353
protected function doReindexByIds ($ ids )
333
354
{
@@ -340,9 +361,10 @@ protected function doReindexByIds($ids)
340
361
$ this ->reindexRuleProduct ->execute ($ rule , $ this ->batchCount );
341
362
}
342
363
343
- foreach ($ ids as $ productId ) {
344
- $ this ->cleanProductPriceIndex ([$ productId ]);
345
- $ this ->reindexRuleProductPrice ->execute ($ this ->batchCount , $ productId );
364
+ // batch products together, using configurable batch size parameter
365
+ foreach (array_chunk ($ ids , $ this ->productBatchSize ) as $ productIds ) {
366
+ $ this ->cleanProductPriceIndex ($ productIds );
367
+ $ this ->reindexRuleProductsPrice ->execute ($ this ->batchCount , $ productIds );
346
368
}
347
369
348
370
//the case was not handled via indexer dependency decorator or via mview configuration
@@ -367,7 +389,7 @@ public function reindexFull()
367
389
{
368
390
try {
369
391
$ this ->doReindexFull ();
370
- } catch (\ Exception $ e ) {
392
+ } catch (Exception $ e ) {
371
393
$ this ->critical ($ e );
372
394
throw new LocalizedException (
373
395
__ ("Catalog rule indexing failed. See details in exception log. " )
@@ -441,6 +463,7 @@ protected function cleanByIds($productIds)
441
463
* @param int $productEntityId
442
464
* @param array $websiteIds
443
465
* @return void
466
+ * @throws Exception
444
467
*/
445
468
private function assignProductToRule (Rule $ rule , int $ productEntityId , array $ websiteIds ): void
446
469
{
@@ -502,7 +525,7 @@ private function assignProductToRule(Rule $rule, int $productEntityId, array $we
502
525
* @param Rule $rule
503
526
* @param Product $product
504
527
* @return $this
505
- * @throws \ Exception
528
+ * @throws Exception
506
529
* @deprecated 101.1.5
507
530
* @see ReindexRuleProduct::execute
508
531
* @SuppressWarnings(PHPMD.NPathComplexity)
@@ -525,6 +548,7 @@ protected function applyRule(Rule $rule, $product)
525
548
* @param RuleCollection $ruleCollection
526
549
* @param Product $product
527
550
* @return void
551
+ * @throws LocalizedException
528
552
*/
529
553
private function applyRules (RuleCollection $ ruleCollection , Product $ product ): void
530
554
{
@@ -590,7 +614,7 @@ protected function updateRuleProductData(Rule $rule)
590
614
* Apply all rules
591
615
*
592
616
* @param Product|null $product
593
- * @throws \ Exception
617
+ * @throws Exception
594
618
* @return $this
595
619
* @deprecated 101.0.0
596
620
* @see ReindexRuleProductPrice::execute
@@ -661,7 +685,7 @@ protected function getRuleProductsStmt($websiteId, Product $product = null)
661
685
*
662
686
* @param array $arrData
663
687
* @return $this
664
- * @throws \ Exception
688
+ * @throws Exception
665
689
* @deprecated 101.0.0
666
690
* @see RuleProductPricesPersistor::execute
667
691
*/
@@ -708,7 +732,7 @@ protected function getProduct($productId)
708
732
/**
709
733
* Log critical exception
710
734
*
711
- * @param \ Exception $e
735
+ * @param Exception $e
712
736
* @return void
713
737
*/
714
738
protected function critical ($ e )
0 commit comments