8
8
namespace Magento \SalesGraphQl \Model \Resolver ;
9
9
10
10
use Magento \Framework \Api \SearchCriteriaBuilder ;
11
+ use Magento \Framework \App \ObjectManager ;
11
12
use Magento \Framework \Exception \InputException ;
12
13
use Magento \Framework \GraphQl \Config \Element \Field ;
13
14
use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
14
15
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
15
16
use Magento \Framework \GraphQl \Query \ResolverInterface ;
16
17
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
18
+ use Magento \Sales \Api \Data \OrderSearchResultInterface ;
17
19
use Magento \Sales \Api \OrderRepositoryInterface ;
18
20
use Magento \SalesGraphQl \Model \Formatter \Order as OrderFormatter ;
19
21
use Magento \SalesGraphQl \Model \Resolver \CustomerOrders \Query \OrderFilter ;
20
22
use Magento \Store \Api \Data \StoreInterface ;
23
+ use Magento \Store \Model \StoreManagerInterface ;
21
24
22
25
/**
23
26
* Orders data resolver
@@ -44,22 +47,30 @@ class CustomerOrders implements ResolverInterface
44
47
*/
45
48
private $ orderFormatter ;
46
49
50
+ /**
51
+ * @var StoreManagerInterface|mixed|null
52
+ */
53
+ private $ storeManager ;
54
+
47
55
/**
48
56
* @param OrderRepositoryInterface $orderRepository
49
57
* @param SearchCriteriaBuilder $searchCriteriaBuilder
50
58
* @param OrderFilter $orderFilter
51
59
* @param OrderFormatter $orderFormatter
60
+ * @param StoreManagerInterface|null $storeManager
52
61
*/
53
62
public function __construct (
54
63
OrderRepositoryInterface $ orderRepository ,
55
64
SearchCriteriaBuilder $ searchCriteriaBuilder ,
56
65
OrderFilter $ orderFilter ,
57
- OrderFormatter $ orderFormatter
66
+ OrderFormatter $ orderFormatter ,
67
+ StoreManagerInterface $ storeManager = null
58
68
) {
59
69
$ this ->orderRepository = $ orderRepository ;
60
70
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
61
71
$ this ->orderFilter = $ orderFilter ;
62
72
$ this ->orderFormatter = $ orderFormatter ;
73
+ $ this ->storeManager = ObjectManager::getInstance ()->get (StoreManagerInterface::class) ?? null ;
63
74
}
64
75
65
76
/**
@@ -81,11 +92,15 @@ public function resolve(
81
92
if ($ args ['pageSize ' ] < 1 ) {
82
93
throw new GraphQlInputException (__ ('pageSize value must be greater than 0. ' ));
83
94
}
95
+ $ storeIds = [];
84
96
$ userId = $ context ->getUserId ();
85
97
/** @var StoreInterface $store */
86
98
$ store = $ context ->getExtensionAttributes ()->getStore ();
99
+ if (isset ($ args ['scope ' ])) {
100
+ $ storeIds = $ this ->getStoresByScope ($ args ['scope ' ], $ store );
101
+ }
87
102
try {
88
- $ searchResult = $ this ->getSearchResult ($ args , (int )$ userId , (int )$ store ->getId ());
103
+ $ searchResult = $ this ->getSearchResult ($ args , (int )$ userId , (int )$ store ->getId (), $ storeIds );
89
104
$ maxPages = (int )ceil ($ searchResult ->getTotalCount () / $ searchResult ->getPageSize ());
90
105
} catch (InputException $ e ) {
91
106
throw new GraphQlInputException (__ ($ e ->getMessage ()));
@@ -113,12 +128,13 @@ public function resolve(
113
128
* @param array $args
114
129
* @param int $userId
115
130
* @param int $storeId
116
- * @return \Magento\Sales\Api\Data\OrderSearchResultInterface
131
+ * @param array $scope
132
+ * @return OrderSearchResultInterface
117
133
* @throws InputException
118
134
*/
119
- private function getSearchResult (array $ args , int $ userId , int $ storeId )
135
+ private function getSearchResult (array $ args , int $ userId , int $ storeId, array $ storeIds )
120
136
{
121
- $ filterGroups = $ this ->orderFilter ->createFilterGroups ($ args , $ userId , (int )$ storeId );
137
+ $ filterGroups = $ this ->orderFilter ->createFilterGroups ($ args , $ userId , (int )$ storeId, $ storeIds );
122
138
$ this ->searchCriteriaBuilder ->setFilterGroups ($ filterGroups );
123
139
if (isset ($ args ['currentPage ' ])) {
124
140
$ this ->searchCriteriaBuilder ->setCurrentPage ($ args ['currentPage ' ]);
@@ -128,4 +144,52 @@ private function getSearchResult(array $args, int $userId, int $storeId)
128
144
}
129
145
return $ this ->orderRepository ->getList ($ this ->searchCriteriaBuilder ->create ());
130
146
}
147
+
148
+ /**
149
+ * @param string $scope
150
+ * @param StoreInterface $store
151
+ * @return void
152
+ */
153
+ private function getStoresByScope (string $ scope , StoreInterface $ store )
154
+ {
155
+ $ storeIds = [];
156
+ switch ($ scope ) {
157
+ case 'global ' :
158
+ $ storeIds = $ this ->getStoresByFilter (null , null );
159
+ break ;
160
+ case 'website ' :
161
+ $ websiteId = $ store ->getWebsiteId ();
162
+ $ storeIds = $ this ->getStoresByFilter ((int )$ websiteId , null );
163
+ break ;
164
+ case 'store ' :
165
+ $ storeGroupId = $ store ->getStoreGroupId ();
166
+ $ storeIds = $ this ->getStoresByFilter (null , (int )$ storeGroupId );
167
+ break ;
168
+ default :
169
+ break ;
170
+ }
171
+ return $ storeIds ;
172
+ }
173
+
174
+ /**
175
+ * @param int|null $websiteId
176
+ * @param int|null $storeGroupId
177
+ * @return array
178
+ */
179
+ private function getStoresByFilter (?int $ websiteId , ?int $ storeGroupId )
180
+ {
181
+ $ stores = $ this ->storeManager ->getStores (true , true );
182
+ $ storeIds = [];
183
+ foreach ($ stores as $ store ) {
184
+ if (isset ($ websiteId ) && $ websiteId === (int )$ store ->getWebsiteId ()
185
+ ||
186
+ isset ($ storeGroupId ) && $ storeGroupId === (int )$ store ->getStoreGroupId ()
187
+ ) {
188
+ $ storeIds [] = $ store ->getId ();
189
+ } elseif (!isset ($ websiteId ) && !isset ($ storeGroupId )) {
190
+ $ storeIds [] = $ store ->getId ();
191
+ }
192
+ }
193
+ return $ storeIds ;
194
+ }
131
195
}
0 commit comments