66
77namespace Magento \CatalogRule \Model \Indexer ;
88
9+ use Exception ;
910use Magento \Catalog \Model \Product ;
1011use Magento \Catalog \Model \ProductFactory ;
1112use Magento \Catalog \Model \ResourceModel \Indexer \ActiveTableSwitcher ;
2829use Magento \Store \Model \ScopeInterface ;
2930use Magento \Store \Model \StoreManagerInterface ;
3031use Psr \Log \LoggerInterface ;
32+ use Zend_Db_Statement_Exception ;
3133
3234/**
3335 * Catalog rule index builder
@@ -181,6 +183,16 @@ class IndexBuilder
181183 */
182184 private $ productCollectionFactory ;
183185
186+ /**
187+ * @var ReindexRuleProductsPrice
188+ */
189+ private $ reindexRuleProductsPrice ;
190+
191+ /**
192+ * @var int
193+ */
194+ private $ productBatchSize ;
195+
184196 /**
185197 * @param RuleCollectionFactory $ruleCollectionFactory
186198 * @param PriceCurrencyInterface $priceCurrency
@@ -204,6 +216,8 @@ class IndexBuilder
204216 * @param TimezoneInterface|null $localeDate
205217 * @param ProductCollectionFactory|null $productCollectionFactory
206218 * @param IndexerRegistry|null $indexerRegistry
219+ * @param ReindexRuleProductsPrice|null $reindexRuleProductsPrice
220+ * @param int $productBatchSize
207221 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
208222 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
209223 */
@@ -229,7 +243,9 @@ public function __construct(
229243 TableSwapper $ tableSwapper = null ,
230244 TimezoneInterface $ localeDate = null ,
231245 ProductCollectionFactory $ productCollectionFactory = null ,
232- IndexerRegistry $ indexerRegistry = null
246+ IndexerRegistry $ indexerRegistry = null ,
247+ ReindexRuleProductsPrice $ reindexRuleProductsPrice = null ,
248+ int $ productBatchSize = 1000
233249 ) {
234250 $ this ->resource = $ resource ;
235251 $ this ->connection = $ resource ->getConnection ();
@@ -242,6 +258,7 @@ public function __construct(
242258 $ this ->dateTime = $ dateTime ;
243259 $ this ->productFactory = $ productFactory ;
244260 $ this ->batchCount = $ batchCount ;
261+ $ this ->productBatchSize = $ productBatchSize ;
245262
246263 $ this ->productPriceCalculator = $ productPriceCalculator ?? ObjectManager::getInstance ()->get (
247264 ProductPriceCalculator::class
@@ -275,6 +292,8 @@ public function __construct(
275292 ObjectManager::getInstance ()->get (IndexerRegistry::class);
276293 $ this ->productCollectionFactory = $ productCollectionFactory ??
277294 ObjectManager::getInstance ()->get (ProductCollectionFactory::class);
295+ $ this ->reindexRuleProductsPrice = $ reindexRuleProductsPrice ??
296+ ObjectManager::getInstance ()->get (ReindexRuleProductsPrice::class);
278297 }
279298
280299 /**
@@ -296,7 +315,7 @@ public function reindexById($id)
296315 }
297316
298317 $ this ->reindexRuleGroupWebsite ->execute ();
299- } catch (\ Exception $ e ) {
318+ } catch (Exception $ e ) {
300319 $ this ->critical ($ e );
301320 throw new LocalizedException (
302321 __ ('Catalog rule indexing failed. See details in exception log. ' )
@@ -315,7 +334,7 @@ public function reindexByIds(array $ids)
315334 {
316335 try {
317336 $ this ->doReindexByIds ($ ids );
318- } catch (\ Exception $ e ) {
337+ } catch (Exception $ e ) {
319338 $ this ->critical ($ e );
320339 throw new LocalizedException (
321340 __ ("Catalog rule indexing failed. See details in exception log. " )
@@ -328,6 +347,8 @@ public function reindexByIds(array $ids)
328347 *
329348 * @param array $ids
330349 * @return void
350+ * @throws LocalizedException
351+ * @throws Zend_Db_Statement_Exception
331352 */
332353 protected function doReindexByIds ($ ids )
333354 {
@@ -340,9 +361,10 @@ protected function doReindexByIds($ids)
340361 $ this ->reindexRuleProduct ->execute ($ rule , $ this ->batchCount );
341362 }
342363
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 );
346368 }
347369
348370 //the case was not handled via indexer dependency decorator or via mview configuration
@@ -367,7 +389,7 @@ public function reindexFull()
367389 {
368390 try {
369391 $ this ->doReindexFull ();
370- } catch (\ Exception $ e ) {
392+ } catch (Exception $ e ) {
371393 $ this ->critical ($ e );
372394 throw new LocalizedException (
373395 __ ("Catalog rule indexing failed. See details in exception log. " )
@@ -441,6 +463,7 @@ protected function cleanByIds($productIds)
441463 * @param int $productEntityId
442464 * @param array $websiteIds
443465 * @return void
466+ * @throws Exception
444467 */
445468 private function assignProductToRule (Rule $ rule , int $ productEntityId , array $ websiteIds ): void
446469 {
@@ -502,7 +525,7 @@ private function assignProductToRule(Rule $rule, int $productEntityId, array $we
502525 * @param Rule $rule
503526 * @param Product $product
504527 * @return $this
505- * @throws \ Exception
528+ * @throws Exception
506529 * @deprecated 101.1.5
507530 * @see ReindexRuleProduct::execute
508531 * @SuppressWarnings(PHPMD.NPathComplexity)
@@ -525,6 +548,7 @@ protected function applyRule(Rule $rule, $product)
525548 * @param RuleCollection $ruleCollection
526549 * @param Product $product
527550 * @return void
551+ * @throws LocalizedException
528552 */
529553 private function applyRules (RuleCollection $ ruleCollection , Product $ product ): void
530554 {
@@ -590,7 +614,7 @@ protected function updateRuleProductData(Rule $rule)
590614 * Apply all rules
591615 *
592616 * @param Product|null $product
593- * @throws \ Exception
617+ * @throws Exception
594618 * @return $this
595619 * @deprecated 101.0.0
596620 * @see ReindexRuleProductPrice::execute
@@ -661,7 +685,7 @@ protected function getRuleProductsStmt($websiteId, Product $product = null)
661685 *
662686 * @param array $arrData
663687 * @return $this
664- * @throws \ Exception
688+ * @throws Exception
665689 * @deprecated 101.0.0
666690 * @see RuleProductPricesPersistor::execute
667691 */
@@ -708,7 +732,7 @@ protected function getProduct($productId)
708732 /**
709733 * Log critical exception
710734 *
711- * @param \ Exception $e
735+ * @param Exception $e
712736 * @return void
713737 */
714738 protected function critical ($ e )
0 commit comments