66 */
77namespace Magento \CatalogRuleConfigurable \Plugin \CatalogRule \Model \Indexer ;
88
9+ use Magento \CatalogRule \Model \Indexer \Product \ProductRuleIndexer ;
910use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
1011use Magento \CatalogRuleConfigurable \Plugin \CatalogRule \Model \ConfigurableProductsProvider ;
1112
1213/**
13- * Class ReindexProduct. Add configurable sub-products to reindex
14+ * Add configurable sub-products to reindex
1415 */
1516class ProductRuleReindex
1617{
1718 /**
18- * @var \Magento\ConfigurableProduct\Model\Product\Type\ Configurable
19+ * @var Configurable
1920 */
2021 private $ configurable ;
2122
2223 /**
23- * @var \Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\ ConfigurableProductsProvider
24+ * @var ConfigurableProductsProvider
2425 */
2526 private $ configurableProductsProvider ;
2627
@@ -37,61 +38,47 @@ public function __construct(
3738 }
3839
3940 /**
40- * @param \Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject
41+ * Reindex configurable product with sub-products
42+ *
43+ * @param ProductRuleIndexer $subject
4144 * @param \Closure $proceed
4245 * @param int $id
43- *
4446 * @return void
4547 */
46- public function aroundExecuteRow (
47- \Magento \CatalogRule \Model \Indexer \Product \ProductRuleIndexer $ subject ,
48- \Closure $ proceed ,
49- $ id
50- ) {
48+ public function aroundExecuteRow (ProductRuleIndexer $ subject , \Closure $ proceed , $ id )
49+ {
50+ $ isReindexed = false ;
51+
5152 $ configurableProductIds = $ this ->configurableProductsProvider ->getIds ([$ id ]);
52- $ this ->reindexSubProducts ($ configurableProductIds , $ subject );
53- if (!$ configurableProductIds ) {
54- $ proceed ($ id );
53+ if ($ configurableProductIds ) {
54+ $ subProducts = array_values ($ this ->configurable ->getChildrenIds ($ id )[0 ]);
55+ if ($ subProducts ) {
56+ $ subject ->executeList (array_merge ([$ id ], $ subProducts ));
57+ $ isReindexed = true ;
58+ }
5559 }
56- }
5760
58- /**
59- * @param \Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject
60- * @param \Closure $proceed
61- * @param array $ids
62- *
63- * @return void
64- */
65- public function aroundExecuteList (
66- \Magento \CatalogRule \Model \Indexer \Product \ProductRuleIndexer $ subject ,
67- \Closure $ proceed ,
68- array $ ids
69- ) {
70- $ configurableProductIds = $ this ->configurableProductsProvider ->getIds ($ ids );
71- $ subProducts = $ this ->reindexSubProducts ($ configurableProductIds , $ subject );
72- $ ids = array_diff ($ ids , $ configurableProductIds , $ subProducts );
73- if ($ ids ) {
74- $ proceed ($ ids );
61+ if (!$ isReindexed ) {
62+ $ proceed ($ id );
7563 }
7664 }
7765
7866 /**
79- * @param array $configurableIds
80- * @param \Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject
67+ * Add sub-products to reindex
8168 *
69+ * @param ProductRuleIndexer $subject
70+ * @param array $ids
8271 * @return array
72+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
8373 */
84- private function reindexSubProducts (
85- array $ configurableIds ,
86- \Magento \CatalogRule \Model \Indexer \Product \ProductRuleIndexer $ subject
87- ) {
88- $ subProducts = [];
89- if ($ configurableIds ) {
90- $ subProducts = array_values ($ this ->configurable ->getChildrenIds ($ configurableIds )[0 ]);
91- if ($ subProducts ) {
92- $ subject ->executeList ($ subProducts );
93- }
74+ public function beforeExecuteList (ProductRuleIndexer $ subject , array $ ids ): array
75+ {
76+ $ configurableProductIds = $ this ->configurableProductsProvider ->getIds ($ ids );
77+ if ($ configurableProductIds ) {
78+ $ subProducts = array_values ($ this ->configurable ->getChildrenIds ($ configurableProductIds )[0 ]);
79+ $ ids = array_unique (array_merge ($ ids , $ subProducts ));
9480 }
95- return $ subProducts ;
81+
82+ return [$ ids ];
9683 }
9784}
0 commit comments