8
8
9
9
namespace Magento \PageBuilder \Model \Catalog ;
10
10
11
+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
11
12
use Magento \Catalog \Model \Product \Attribute \Source \Status ;
12
13
use Magento \Catalog \Model \Product \Visibility ;
13
14
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
14
15
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
15
16
use Magento \CatalogWidget \Model \Rule ;
16
17
use Magento \Framework \Exception \LocalizedException ;
18
+ use Magento \Framework \Exception \NoSuchEntityException ;
17
19
use Magento \Rule \Model \Condition \Combine ;
18
20
use Magento \Rule \Model \Condition \Sql \Builder ;
19
21
use Magento \Widget \Helper \Conditions ;
20
22
use Zend_Db_Select_Exception ;
21
23
22
24
/**
23
25
* Product totals for Products content type
26
+ *
27
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24
28
*/
25
29
class ProductTotals
26
30
{
@@ -44,22 +48,30 @@ class ProductTotals
44
48
*/
45
49
private $ conditionsHelper ;
46
50
51
+ /**
52
+ * @var CategoryRepositoryInterface
53
+ */
54
+ private $ categoryRepository ;
55
+
47
56
/**
48
57
* @param CollectionFactory $productCollectionFactory
49
58
* @param Builder $sqlBuilder
50
59
* @param Rule $rule
51
60
* @param Conditions $conditionsHelper
61
+ * @param CategoryRepositoryInterface $categoryRepository
52
62
*/
53
63
public function __construct (
54
64
CollectionFactory $ productCollectionFactory ,
55
65
Builder $ sqlBuilder ,
56
66
Rule $ rule ,
57
- Conditions $ conditionsHelper
67
+ Conditions $ conditionsHelper ,
68
+ CategoryRepositoryInterface $ categoryRepository
58
69
) {
59
70
$ this ->productCollectionFactory = $ productCollectionFactory ;
60
71
$ this ->sqlBuilder = $ sqlBuilder ;
61
72
$ this ->rule = $ rule ;
62
73
$ this ->conditionsHelper = $ conditionsHelper ;
74
+ $ this ->categoryRepository = $ categoryRepository ;
63
75
}
64
76
65
77
/**
@@ -75,17 +87,49 @@ private function decodeConditions(string $conditions): Combine
75
87
}
76
88
77
89
foreach ($ conditions as $ key => $ condition ) {
78
- if (!empty ($ condition ['attribute ' ])
79
- && in_array ($ condition ['attribute ' ], ['special_from_date ' , 'special_to_date ' ])
80
- ) {
81
- $ conditions [$ key ]['value ' ] = date ('Y-m-d H:i:s ' , strtotime ($ condition ['value ' ]));
90
+ if (!empty ($ condition ['attribute ' ])) {
91
+ if (in_array ($ condition ['attribute ' ], ['special_from_date ' , 'special_to_date ' ])) {
92
+ $ conditions [$ key ]['value ' ] = date ('Y-m-d H:i:s ' , strtotime ($ condition ['value ' ]));
93
+ }
94
+
95
+ if ($ condition ['attribute ' ] == 'category_ids ' ) {
96
+ $ conditions [$ key ] = $ this ->updateAnchorCategoryConditions ($ condition );
97
+ }
82
98
}
83
99
}
84
100
85
101
$ this ->rule ->loadPost (['conditions ' => $ conditions ]);
86
102
return $ this ->rule ->getConditions ();
87
103
}
88
104
105
+ /**
106
+ * Update conditions if the category is an anchor category
107
+ *
108
+ * @param array $condition
109
+ * @return array
110
+ */
111
+ private function updateAnchorCategoryConditions (array $ condition ): array
112
+ {
113
+ if (array_key_exists ('value ' , $ condition )) {
114
+ $ categoryId = $ condition ['value ' ];
115
+
116
+ try {
117
+ $ category = $ this ->categoryRepository ->get ($ categoryId );
118
+ } catch (NoSuchEntityException $ e ) {
119
+ return $ condition ;
120
+ }
121
+
122
+ if ($ category ->getIsAnchor () && $ category ->getChildren (true )) {
123
+ $ children = explode (', ' , $ category ->getChildren (true ));
124
+
125
+ $ condition ['operator ' ] = "() " ;
126
+ $ condition ['value ' ] = array_merge ([$ categoryId ], $ children );
127
+ }
128
+ }
129
+
130
+ return $ condition ;
131
+ }
132
+
89
133
/**
90
134
* Retrieve product collection based on provided conditions
91
135
*
0 commit comments