3
3
* Copyright 2015 Adobe
4
4
* All Rights Reserved.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \CatalogRuleConfigurable \Plugin \CatalogRule \Model \Rule ;
7
9
8
- use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
9
10
use Magento \CatalogRuleConfigurable \Plugin \CatalogRule \Model \ConfigurableProductsProvider ;
11
+ use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable as ConfigurableProductsResourceModel ;
10
12
11
13
/**
12
14
* Add configurable sub products to catalog rule indexer on full reindex
13
15
*/
14
16
class ConfigurableProductHandler
15
17
{
16
18
/**
17
- * @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable
19
+ * @var ConfigurableProductsResourceModel
18
20
*/
19
- private $ configurable ;
21
+ private ConfigurableProductsResourceModel $ configurable ;
20
22
21
23
/**
22
- * @var \Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\ ConfigurableProductsProvider
24
+ * @var ConfigurableProductsProvider
23
25
*/
24
- private $ configurableProductsProvider ;
26
+ private ConfigurableProductsProvider $ configurableProductsProvider ;
25
27
26
28
/**
27
29
* @var array
28
30
*/
29
- private $ childrenProducts = [];
31
+ private array $ childrenProducts = [];
30
32
31
33
/**
32
- * @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurable
34
+ * @param ConfigurableProductsResourceModel $configurable
33
35
* @param ConfigurableProductsProvider $configurableProductsProvider
34
36
*/
35
37
public function __construct (
36
- \ Magento \ ConfigurableProduct \ Model \ ResourceModel \ Product \ Type \ Configurable $ configurable ,
37
- ConfigurableProductsProvider $ configurableProductsProvider
38
+ ConfigurableProductsResourceModel $ configurable ,
39
+ ConfigurableProductsProvider $ configurableProductsProvider
38
40
) {
39
41
$ this ->configurable = $ configurable ;
40
42
$ this ->configurableProductsProvider = $ configurableProductsProvider ;
@@ -48,40 +50,71 @@ public function __construct(
48
50
* @return array
49
51
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
50
52
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
53
+ * @SuppressWarnings(PHPMD.NPathComplexity)
54
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
51
55
*/
52
56
public function aroundGetMatchingProductIds (
53
57
\Magento \CatalogRule \Model \Rule $ rule ,
54
58
\Closure $ proceed
55
- ) {
59
+ ): array {
56
60
$ productsFilter = $ rule ->getProductsFilter () ? (array ) $ rule ->getProductsFilter () : [];
57
61
if ($ productsFilter ) {
58
- $ parentProductIds = $ this ->configurable ->getParentIdsByChild ($ productsFilter );
59
- $ rule ->setProductsFilter (array_unique (array_merge ($ productsFilter , $ parentProductIds )));
62
+ $ rule ->setProductsFilter (
63
+ array_unique (
64
+ array_merge (
65
+ $ productsFilter ,
66
+ $ this ->configurable ->getParentIdsByChild ($ productsFilter )
67
+ )
68
+ )
69
+ );
60
70
}
61
71
62
72
$ productIds = $ proceed ();
73
+ foreach ($ productIds as $ productId => $ productData ) {
74
+ if ($ this ->hasAntecedentRule ((int ) $ productId )) {
75
+ $ productIds [$ productId ]['has_antecedent_rule ' ] = true ;
76
+ }
77
+ }
63
78
64
- $ configurableProductIds = $ this ->configurableProductsProvider ->getIds (array_keys ($ productIds ));
65
- foreach ( $ configurableProductIds as $ productId ) {
66
- if (! isset ( $ this ->childrenProducts [$ productId ])) {
67
- $ this -> childrenProducts [ $ productId ] = $ this ->configurable ->getChildrenIds ($ productId )[0 ];
79
+ foreach ( $ this ->configurableProductsProvider ->getIds (array_keys ($ productIds )) as $ configurableProductId ) {
80
+ if (! isset ( $ this -> childrenProducts [ $ configurableProductId ]) ) {
81
+ $ this ->childrenProducts [$ configurableProductId ] =
82
+ $ this ->configurable ->getChildrenIds ($ configurableProductId )[0 ];
68
83
}
69
- $ subProductIds = $ this -> childrenProducts [ $ productId ];
70
- $ parentValidationResult = isset ($ productIds [$ productId ])
71
- ? array_filter ($ productIds [$ productId ])
84
+
85
+ $ parentValidationResult = isset ($ productIds [$ configurableProductId ])
86
+ ? array_filter ($ productIds [$ configurableProductId ])
72
87
: [];
73
- $ processAllChildren = !$ productsFilter || in_array ($ productId , $ productsFilter );
74
- foreach ($ subProductIds as $ subProductId ) {
75
- if ($ processAllChildren || in_array ($ subProductId , $ productsFilter )) {
76
- $ childValidationResult = isset ($ productIds [$ subProductId ])
77
- ? array_filter ($ productIds [$ subProductId ])
88
+ $ processAllChildren = !$ productsFilter || in_array ($ configurableProductId , $ productsFilter );
89
+ foreach ($ this -> childrenProducts [ $ configurableProductId ] as $ childrenProductId ) {
90
+ if ($ processAllChildren || in_array ($ childrenProductId , $ productsFilter )) {
91
+ $ childValidationResult = isset ($ productIds [$ childrenProductId ])
92
+ ? array_filter ($ productIds [$ childrenProductId ])
78
93
: [];
79
- $ productIds [$ subProductId ] = $ parentValidationResult + $ childValidationResult ;
94
+ $ productIds [$ childrenProductId ] = $ parentValidationResult + $ childValidationResult ;
80
95
}
81
-
82
96
}
83
- unset($ productIds [$ productId ]);
97
+ unset($ productIds [$ configurableProductId ]);
84
98
}
99
+
85
100
return $ productIds ;
86
101
}
102
+
103
+ /**
104
+ * Check if simple product has previously applied rule.
105
+ *
106
+ * @param int $productId
107
+ * @return bool
108
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
109
+ */
110
+ private function hasAntecedentRule (int $ productId ): bool
111
+ {
112
+ foreach ($ this ->childrenProducts as $ parent => $ children ) {
113
+ if (in_array ($ productId , $ children )) {
114
+ return true ;
115
+ }
116
+ }
117
+
118
+ return false ;
119
+ }
87
120
}
0 commit comments