1
1
<?php
2
2
/**
3
- *
4
- * Copyright © Magento, Inc. All rights reserved.
5
- * See COPYING.txt for license details.
3
+ * Copyright 2015 Adobe
4
+ * All Rights Reserved.
6
5
*/
6
+ declare (strict_types=1 );
7
+
7
8
namespace Magento \CatalogRuleConfigurable \Plugin \CatalogRule \Model \Rule ;
8
9
9
- use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
10
10
use Magento \CatalogRuleConfigurable \Plugin \CatalogRule \Model \ConfigurableProductsProvider ;
11
+ use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable as ConfigurableProductsResourceModel ;
11
12
12
13
/**
13
14
* Add configurable sub products to catalog rule indexer on full reindex
14
15
*/
15
16
class ConfigurableProductHandler
16
17
{
17
18
/**
18
- * @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable
19
+ * @var ConfigurableProductsResourceModel
19
20
*/
20
- private $ configurable ;
21
+ private ConfigurableProductsResourceModel $ configurable ;
21
22
22
23
/**
23
- * @var \Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\ ConfigurableProductsProvider
24
+ * @var ConfigurableProductsProvider
24
25
*/
25
- private $ configurableProductsProvider ;
26
+ private ConfigurableProductsProvider $ configurableProductsProvider ;
26
27
27
28
/**
28
29
* @var array
29
30
*/
30
- private $ childrenProducts = [];
31
+ private array $ childrenProducts = [];
31
32
32
33
/**
33
- * @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurable
34
+ * @param ConfigurableProductsResourceModel $configurable
34
35
* @param ConfigurableProductsProvider $configurableProductsProvider
35
36
*/
36
37
public function __construct (
37
- \ Magento \ ConfigurableProduct \ Model \ ResourceModel \ Product \ Type \ Configurable $ configurable ,
38
- ConfigurableProductsProvider $ configurableProductsProvider
38
+ ConfigurableProductsResourceModel $ configurable ,
39
+ ConfigurableProductsProvider $ configurableProductsProvider
39
40
) {
40
41
$ this ->configurable = $ configurable ;
41
42
$ this ->configurableProductsProvider = $ configurableProductsProvider ;
@@ -49,40 +50,71 @@ public function __construct(
49
50
* @return array
50
51
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
51
52
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
53
+ * @SuppressWarnings(PHPMD.NPathComplexity)
54
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
52
55
*/
53
56
public function aroundGetMatchingProductIds (
54
57
\Magento \CatalogRule \Model \Rule $ rule ,
55
58
\Closure $ proceed
56
- ) {
59
+ ): array {
57
60
$ productsFilter = $ rule ->getProductsFilter () ? (array ) $ rule ->getProductsFilter () : [];
58
61
if ($ productsFilter ) {
59
- $ parentProductIds = $ this ->configurable ->getParentIdsByChild ($ productsFilter );
60
- $ 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
+ );
61
70
}
62
71
63
72
$ productIds = $ proceed ();
73
+ foreach ($ productIds as $ productId => $ productData ) {
74
+ if ($ this ->hasAntecedentRule ((int ) $ productId )) {
75
+ $ productIds [$ productId ]['has_antecedent_rule ' ] = true ;
76
+ }
77
+ }
64
78
65
- $ configurableProductIds = $ this ->configurableProductsProvider ->getIds (array_keys ($ productIds ));
66
- foreach ( $ configurableProductIds as $ productId ) {
67
- if (! isset ( $ this ->childrenProducts [$ productId ])) {
68
- $ 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 ];
69
83
}
70
- $ subProductIds = $ this -> childrenProducts [ $ productId ];
71
- $ parentValidationResult = isset ($ productIds [$ productId ])
72
- ? array_filter ($ productIds [$ productId ])
84
+
85
+ $ parentValidationResult = isset ($ productIds [$ configurableProductId ])
86
+ ? array_filter ($ productIds [$ configurableProductId ])
73
87
: [];
74
- $ processAllChildren = !$ productsFilter || in_array ($ productId , $ productsFilter );
75
- foreach ($ subProductIds as $ subProductId ) {
76
- if ($ processAllChildren || in_array ($ subProductId , $ productsFilter )) {
77
- $ childValidationResult = isset ($ productIds [$ subProductId ])
78
- ? 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 ])
79
93
: [];
80
- $ productIds [$ subProductId ] = $ parentValidationResult + $ childValidationResult ;
94
+ $ productIds [$ childrenProductId ] = $ parentValidationResult + $ childValidationResult ;
81
95
}
82
-
83
96
}
84
- unset($ productIds [$ productId ]);
97
+ unset($ productIds [$ configurableProductId ]);
85
98
}
99
+
86
100
return $ productIds ;
87
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
+ }
88
120
}
0 commit comments