1
1
<?php
2
+
2
3
/**
3
4
* Copyright © Magento, Inc. All rights reserved.
4
5
* See COPYING.txt for license details.
5
6
*/
7
+
6
8
declare (strict_types=1 );
7
9
8
10
namespace Graycore \GuestOrdersGraphQl \Model \Resolver ;
@@ -82,39 +84,37 @@ public function resolve(
82
84
array $ args = null
83
85
) {
84
86
return [
85
- 'items ' => $ this ->formatOrdersArray ($ this ->getOrdersForCart ($ args ['cartId ' ]))
87
+ 'items ' => $ this ->formatOrder ($ this ->getOrderForCart ($ args ['cartId ' ]))
86
88
];
87
89
}
88
90
89
91
/**
90
92
* Format order models for graphql schema.
91
93
* Copied from the Magento resolver.
92
94
*
93
- * @param OrderInterface[] $orderModels
95
+ * @param \Magento\Sales\Model\Order $order
94
96
* @return array
95
97
*/
96
- private function formatOrdersArray ( array $ orderModels )
98
+ private function formatOrder ( \ Magento \ Sales \ Model \ Order $ order )
97
99
{
98
- $ ordersArray = [];
99
- foreach ($ orderModels as $ orderModel ) {
100
- $ ordersArray [] = [
101
- 'created_at ' => $ orderModel ->getCreatedAt (),
102
- 'grand_total ' => $ orderModel ->getGrandTotal (),
103
- 'id ' => base64_encode ($ orderModel ->getEntityId ()),
104
- 'increment_id ' => $ orderModel ->getIncrementId (),
105
- 'number ' => $ orderModel ->getIncrementId (),
106
- 'order_date ' => $ orderModel ->getCreatedAt (),
107
- 'order_number ' => $ orderModel ->getIncrementId (),
108
- 'status ' => $ orderModel ->getStatusLabel (),
109
- 'shipping_method ' => $ orderModel ->getShippingDescription (),
110
- 'shipping_address ' => $ this ->orderAddress ->getOrderShippingAddress ($ orderModel ),
111
- 'billing_address ' => $ this ->orderAddress ->getOrderBillingAddress ($ orderModel ),
112
- 'payment_methods ' => $ this ->orderPayments ->getOrderPaymentMethod ($ orderModel ),
113
- 'model ' => $ orderModel ,
114
- 'email ' => $ orderModel ->getCustomerEmail ()
115
- ];
116
- }
117
- return $ ordersArray ;
100
+ return [
101
+ [
102
+ 'created_at ' => $ order ->getCreatedAt (),
103
+ 'grand_total ' => $ order ->getGrandTotal (),
104
+ 'id ' => base64_encode ($ order ->getEntityId () ?? '' ),
105
+ 'increment_id ' => $ order ->getIncrementId (),
106
+ 'number ' => $ order ->getIncrementId (),
107
+ 'order_date ' => $ order ->getCreatedAt (),
108
+ 'order_number ' => $ order ->getIncrementId (),
109
+ 'status ' => $ order ->getStatusLabel (),
110
+ 'shipping_method ' => $ order ->getShippingDescription (),
111
+ 'shipping_address ' => $ this ->orderAddress ->getOrderShippingAddress ($ order ),
112
+ 'billing_address ' => $ this ->orderAddress ->getOrderBillingAddress ($ order ),
113
+ 'payment_methods ' => $ this ->orderPayments ->getOrderPaymentMethod ($ order ),
114
+ 'model ' => $ order ,
115
+ 'email ' => $ order ->getCustomerEmail ()
116
+ ]
117
+ ];
118
118
}
119
119
120
120
/**
@@ -137,10 +137,8 @@ private function getCart(string $cartHash)
137
137
);
138
138
}
139
139
140
- $ cartCustomerId = (int )$ cart ->getCustomerId ();
141
-
142
140
/* Not a guest cart, throw */
143
- if (0 !== $ cartCustomerId ) {
141
+ if (! $ cart -> getCustomerIsGuest () ) {
144
142
throw new GraphQlAuthorizationException (
145
143
__ (
146
144
'The cart "%masked_cart_id" is not a guest cart ' ,
@@ -158,7 +156,7 @@ private function getCart(string $cartHash)
158
156
* @param string $cartHash the hashed cart ID
159
157
* @throws GraphQlNoSuchEntityException
160
158
*/
161
- private function getOrdersForCart (string $ cartHash )
159
+ private function getOrderForCart (string $ cartHash )
162
160
{
163
161
$ orderId = $ this ->getCart ($ cartHash )->getReservedOrderId ();
164
162
@@ -171,13 +169,18 @@ private function getOrdersForCart(string $cartHash)
171
169
);
172
170
}
173
171
174
- $ orders = $ this ->collectionFactory ->create (null )->getItems ();
175
172
176
- /** @param OrderInterface $order */
177
- $ isCartOrder = function ($ order ) use ($ orderId ) {
178
- return $ order ->getIncrementId () === $ orderId ;
179
- };
180
173
181
- return array_values (array_filter ($ orders , $ isCartOrder ));
174
+ $ order = $ this ->collectionFactory ->create ()->addFieldToSearchFilter ('increment_id ' , $ orderId )->getFirstItem ();
175
+
176
+ if (!$ order ->getIncrementId ()) {
177
+ throw new GraphQlNoSuchEntityException (
178
+ __ (
179
+ 'Could not find an order associated with cart with ID "%masked_cart_id" ' ,
180
+ ['masked_cart_id ' => $ cartHash ]
181
+ )
182
+ );
183
+ }
184
+ return $ order ;
182
185
}
183
186
}
0 commit comments