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 \CatalogInventory \Helper \Stock ;
16
17
use Magento \CatalogWidget \Model \Rule ;
17
18
use Magento \Framework \Exception \LocalizedException ;
19
+ use Magento \Framework \Exception \NoSuchEntityException ;
18
20
use Magento \Rule \Model \Condition \Combine ;
19
21
use Magento \Rule \Model \Condition \Sql \Builder ;
20
22
use Magento \Widget \Helper \Conditions ;
@@ -50,25 +52,33 @@ class ProductTotals
50
52
*/
51
53
private $ stockFilter ;
52
54
55
+ /**
56
+ * @var CategoryRepositoryInterface
57
+ */
58
+ private $ categoryRepository ;
59
+
53
60
/**
54
61
* @param CollectionFactory $productCollectionFactory
55
62
* @param Builder $sqlBuilder
56
63
* @param Rule $rule
57
64
* @param Conditions $conditionsHelper
58
65
* @param Stock $stockFilter
66
+ * @param CategoryRepositoryInterface $categoryRepository
59
67
*/
60
68
public function __construct (
61
69
CollectionFactory $ productCollectionFactory ,
62
70
Builder $ sqlBuilder ,
63
71
Rule $ rule ,
64
72
Conditions $ conditionsHelper ,
65
- Stock $ stockFilter
73
+ Stock $ stockFilter ,
74
+ CategoryRepositoryInterface $ categoryRepository
66
75
) {
67
76
$ this ->productCollectionFactory = $ productCollectionFactory ;
68
77
$ this ->sqlBuilder = $ sqlBuilder ;
69
78
$ this ->rule = $ rule ;
70
79
$ this ->conditionsHelper = $ conditionsHelper ;
71
80
$ this ->stockFilter = $ stockFilter ;
81
+ $ this ->categoryRepository = $ categoryRepository ;
72
82
}
73
83
74
84
/**
@@ -84,17 +94,49 @@ private function decodeConditions(string $conditions): Combine
84
94
}
85
95
86
96
foreach ($ conditions as $ key => $ condition ) {
87
- if (!empty ($ condition ['attribute ' ])
88
- && in_array ($ condition ['attribute ' ], ['special_from_date ' , 'special_to_date ' ])
89
- ) {
90
- $ conditions [$ key ]['value ' ] = date ('Y-m-d H:i:s ' , strtotime ($ condition ['value ' ]));
97
+ if (!empty ($ condition ['attribute ' ])) {
98
+ if (in_array ($ condition ['attribute ' ], ['special_from_date ' , 'special_to_date ' ])) {
99
+ $ conditions [$ key ]['value ' ] = date ('Y-m-d H:i:s ' , strtotime ($ condition ['value ' ]));
100
+ }
101
+
102
+ if ($ condition ['attribute ' ] == 'category_ids ' ) {
103
+ $ conditions [$ key ] = $ this ->updateAnchorCategoryConditions ($ condition );
104
+ }
91
105
}
92
106
}
93
107
94
108
$ this ->rule ->loadPost (['conditions ' => $ conditions ]);
95
109
return $ this ->rule ->getConditions ();
96
110
}
97
111
112
+ /**
113
+ * Update conditions if the category is an anchor category
114
+ *
115
+ * @param array $condition
116
+ * @return array
117
+ */
118
+ private function updateAnchorCategoryConditions (array $ condition ): array
119
+ {
120
+ if (array_key_exists ('value ' , $ condition )) {
121
+ $ categoryId = $ condition ['value ' ];
122
+
123
+ try {
124
+ $ category = $ this ->categoryRepository ->get ($ categoryId );
125
+ } catch (NoSuchEntityException $ e ) {
126
+ return $ condition ;
127
+ }
128
+
129
+ if ($ category ->getIsAnchor () && $ category ->getChildren ()) {
130
+ $ children = explode (', ' , $ category ->getChildren ());
131
+
132
+ $ condition ['operator ' ] = "() " ;
133
+ $ condition ['value ' ] = array_merge ([$ categoryId ], $ children );
134
+ }
135
+ }
136
+
137
+ return $ condition ;
138
+ }
139
+
98
140
/**
99
141
* Retrieve product collection based on provided conditions
100
142
*
0 commit comments