• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-woocommerce / 9122745284

17 May 2024 04:03AM UTC coverage: 85.048% (+0.4%) from 84.647%
9122745284

push

github

web-flow
feat: Queries and mutations for shipping zones, tax classes, and tax rates. (#856)

* fix: General bugfixes and improvements

* devops: New mutations and types tested and compliance with Linter and PHPStan

* chore: hooks added to the mutation resolvers

* feat: permission checks added

* chore: Linter and compliance met

* chore: Linter and compliance met

1252 of 1399 new or added lines in 39 files covered. (89.49%)

9 existing lines in 2 files now uncovered.

12423 of 14607 relevant lines covered (85.05%)

70.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.65
/includes/type/object/class-root-query.php
1
<?php
2
/**
3
 * Registers WooCommerce fields on the RootQuery object.
4
 *
5
 * @package WPGraphQL\WooCommerce\Type\WPObject
6
 * @since   0.6.0
7
 */
8

9
namespace WPGraphQL\WooCommerce\Type\WPObject;
10

11
use Automattic\WooCommerce\StoreApi\Utilities\ProductQueryFilters;
12
use Automattic\WooCommerce\Utilities\OrderUtil;
13
use GraphQL\Error\UserError;
14
use GraphQLRelay\Relay;
15
use WPGraphQL\AppContext;
16
use WPGraphQL\WooCommerce\Data\Factory;
17
use WPGraphQL\WooCommerce\WP_GraphQL_WooCommerce as WooGraphQL;
18

19
/**
20
 * Class - Root_Query
21
 */
22
class Root_Query {
23
        /**
24
         * Registers WC-related root queries.
25
         *
26
         * @return void
27
         */
28
        public static function register_fields() {
29
                register_graphql_fields(
138✔
30
                        'RootQuery',
138✔
31
                        [
138✔
32
                                'cart'             => [
138✔
33
                                        'type'        => 'Cart',
138✔
34
                                        'args'        => [
138✔
35
                                                'recalculateTotals' => [
138✔
36
                                                        'type'        => 'Boolean',
138✔
37
                                                        'description' => __( 'Should cart totals be recalculated.', 'wp-graphql-woocommerce' ),
138✔
38
                                                ],
138✔
39
                                        ],
138✔
40
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
138✔
41
                                        'resolve'     => static function ( $_, $args ) {
138✔
42
                                                $token_invalid = apply_filters( 'graphql_woocommerce_session_token_errors', null );
9✔
43
                                                if ( $token_invalid ) {
9✔
44
                                                        throw new UserError( $token_invalid );
×
45
                                                }
46

47
                                                $cart = Factory::resolve_cart();
9✔
48
                                                if ( ! empty( $args['recalculateTotals'] ) ) {
9✔
49
                                                        $cart->calculate_totals();
×
50
                                                }
51

52
                                                return $cart;
9✔
53
                                        },
138✔
54
                                ],
138✔
55
                                'cartItem'         => [
138✔
56
                                        'type'        => 'CartItem',
138✔
57
                                        'args'        => [
138✔
58
                                                'key' => [
138✔
59
                                                        'type' => [ 'non_null' => 'ID' ],
138✔
60
                                                ],
138✔
61
                                        ],
138✔
62
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
138✔
63
                                        'resolve'     => static function ( $source, array $args ) {
138✔
64
                                                $item = Factory::resolve_cart()->get_cart_item( $args['key'] );
1✔
65
                                                if ( empty( $item ) || empty( $item['key'] ) ) {
1✔
66
                                                        throw new UserError( __( 'Failed to retrieve cart item.', 'wp-graphql-woocommerce' ) );
×
67
                                                }
68

69
                                                return $item;
1✔
70
                                        },
138✔
71
                                ],
138✔
72
                                'cartFee'          => [
138✔
73
                                        'type'        => 'CartFee',
138✔
74
                                        'args'        => [
138✔
75
                                                'id' => [
138✔
76
                                                        'type' => [ 'non_null' => 'ID' ],
138✔
77
                                                ],
138✔
78
                                        ],
138✔
79
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
138✔
80
                                        'resolve'     => static function ( $source, array $args ) {
138✔
81
                                                $fees   = Factory::resolve_cart()->get_fees();
1✔
82
                                                $fee_id = $args['id'];
1✔
83

84
                                                if ( empty( $fees[ $fee_id ] ) ) {
1✔
85
                                                        throw new UserError( __( 'The ID input is invalid', 'wp-graphql-woocommerce' ) );
×
86
                                                }
87

88
                                                return $fees[ $fee_id ];
1✔
89
                                        },
138✔
90
                                ],
138✔
91
                                'coupon'           => [
138✔
92
                                        'type'        => 'Coupon',
138✔
93
                                        'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ),
138✔
94
                                        'args'        => [
138✔
95
                                                'id'     => [ 'type' => [ 'non_null' => 'ID' ] ],
138✔
96
                                                'idType' => [
138✔
97
                                                        'type'        => 'CouponIdTypeEnum',
138✔
98
                                                        'description' => __( 'Type of ID being used identify coupon', 'wp-graphql-woocommerce' ),
138✔
99
                                                ],
138✔
100
                                        ],
138✔
101
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
138✔
102
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
3✔
103
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
3✔
104

105
                                                $coupon_id = null;
3✔
106
                                                switch ( $id_type ) {
107
                                                        case 'code':
3✔
108
                                                                $coupon_id = \wc_get_coupon_id_by_code( $id );
1✔
109
                                                                break;
1✔
110
                                                        case 'database_id':
3✔
111
                                                                $coupon_id = absint( $id );
1✔
112
                                                                break;
1✔
113
                                                        case 'global_id':
3✔
114
                                                        default:
115
                                                                $id_components = Relay::fromGlobalId( $args['id'] );
3✔
116
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
3✔
117
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
118
                                                                }
119
                                                                $coupon_id = absint( $id_components['id'] );
3✔
120
                                                                break;
3✔
121
                                                }
122

123
                                                // Check if user authorized to view coupon.
124
                                                /**
125
                                                 * Get coupon post type.
126
                                                 *
127
                                                 * @var \WP_Post_Type $post_type
128
                                                 */
129
                                                $post_type     = get_post_type_object( 'shop_coupon' );
3✔
130
                                                $is_authorized = current_user_can( $post_type->cap->edit_others_posts );
3✔
131
                                                if ( ! $is_authorized ) {
3✔
132
                                                        return null;
1✔
133
                                                }
134

135
                                                if ( empty( $coupon_id ) ) {
3✔
136
                                                        /* translators: %1$s: ID type, %2$s: ID value */
137
                                                        throw new UserError( sprintf( __( 'No coupon ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
138
                                                }
139

140
                                                $coupon = get_post( $coupon_id );
3✔
141
                                                if ( ! is_object( $coupon ) || 'shop_coupon' !== $coupon->post_type ) {
3✔
142
                                                        /* translators: %1$s: ID type, %2$s: ID value */
143
                                                        throw new UserError( sprintf( __( 'No coupon exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
144
                                                }
145

146
                                                return Factory::resolve_crud_object( $coupon_id, $context );
3✔
147
                                        },
138✔
148
                                ],
138✔
149
                                'customer'         => [
138✔
150
                                        'type'        => 'Customer',
138✔
151
                                        'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
138✔
152
                                        'args'        => [
138✔
153
                                                'id'         => [
138✔
154
                                                        'type'        => 'ID',
138✔
155
                                                        'description' => __( 'Get the customer by their global ID', 'wp-graphql-woocommerce' ),
138✔
156
                                                ],
138✔
157
                                                'customerId' => [
138✔
158
                                                        'type'        => 'Int',
138✔
159
                                                        'description' => __( 'Get the customer by their database ID', 'wp-graphql-woocommerce' ),
138✔
160
                                                ],
138✔
161
                                        ],
138✔
162
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
138✔
163
                                                $current_user_id = get_current_user_id();
10✔
164

165
                                                // Default the customer to the current user.
166
                                                $customer_id = $current_user_id;
10✔
167

168
                                                // If a customer ID has been provided, resolve to that ID instead.
169
                                                if ( ! empty( $args['id'] ) ) {
10✔
170
                                                        $id_components = Relay::fromGlobalId( $args['id'] );
3✔
171
                                                        if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
3✔
172
                                                                throw new UserError( __( 'The ID input is invalid', 'wp-graphql-woocommerce' ) );
×
173
                                                        }
174

175
                                                        $customer_id = absint( $id_components['id'] );
3✔
176
                                                } elseif ( ! empty( $args['customerId'] ) ) {
10✔
177
                                                        $customer_id = absint( $args['customerId'] );
1✔
178
                                                }
179

180
                                                // If a user does not have the ability to list users, they can only view their own customer object.
181
                                                $unauthorized = ! empty( $customer_id )
10✔
182
                                                        && ! current_user_can( 'list_users' )
10✔
183
                                                        && $current_user_id !== $customer_id;
10✔
184
                                                if ( $unauthorized ) {
10✔
185
                                                        throw new UserError( __( 'Not authorized to access this customer', 'wp-graphql-woocommerce' ) );
2✔
186
                                                }
187

188
                                                // If we have a customer ID, resolve to that customer.
189
                                                if ( $customer_id ) {
10✔
190
                                                        return Factory::resolve_customer( $customer_id, $context );
10✔
191
                                                }
192

193
                                                // Resolve to the session customer.
194
                                                return Factory::resolve_session_customer();
1✔
195
                                        },
138✔
196
                                ],
138✔
197
                                'order'            => [
138✔
198
                                        'type'        => 'Order',
138✔
199
                                        'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
138✔
200
                                        'args'        => [
138✔
201
                                                'id'     => [
138✔
202
                                                        'type'        => 'ID',
138✔
203
                                                        'description' => __( 'The ID for identifying the order', 'wp-graphql-woocommerce' ),
138✔
204
                                                ],
138✔
205
                                                'idType' => [
138✔
206
                                                        'type'        => 'OrderIdTypeEnum',
138✔
207
                                                        'description' => __( 'Type of ID being used identify order', 'wp-graphql-woocommerce' ),
138✔
208
                                                ],
138✔
209
                                        ],
138✔
210
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
138✔
211
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
10✔
212
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
10✔
213

214
                                                $order_id = null;
10✔
215
                                                switch ( $id_type ) {
216
                                                        case 'order_key':
10✔
217
                                                                $order_id = \wc_get_order_id_by_order_key( $id );
1✔
218
                                                                break;
1✔
219
                                                        case 'database_id':
10✔
220
                                                                $order_id = absint( $id );
2✔
221
                                                                break;
2✔
222
                                                        case 'global_id':
9✔
223
                                                        default:
224
                                                                $id_components = Relay::fromGlobalId( $id );
9✔
225
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
9✔
226
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
227
                                                                }
228
                                                                $order_id = absint( $id_components['id'] );
9✔
229
                                                                break;
9✔
230
                                                }
231

232
                                                if ( empty( $order_id ) ) {
10✔
233
                                                        /* translators: %1$s: ID type, %2$s: ID value */
234
                                                        throw new UserError( sprintf( __( 'No order ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
235
                                                }
236

237
                                                if ( 'shop_order' !== OrderUtil::get_order_type( $order_id ) ) {
10✔
238
                                                        /* translators: %1$s: ID type, %2$s: ID value */
239
                                                        throw new UserError( sprintf( __( 'No order exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
240
                                                }
241

242
                                                // Check if user authorized to view order.
243
                                                /**
244
                                                 * Get order post type.
245
                                                 *
246
                                                 * @var \WP_Post_Type $post_type
247
                                                 */
248
                                                $post_type     = get_post_type_object( 'shop_order' );
10✔
249
                                                $is_authorized = current_user_can( $post_type->cap->edit_others_posts );
10✔
250
                                                if ( ! $is_authorized && get_current_user_id() ) {
10✔
251
                                                        /** @var \WC_Order[] $orders */
252
                                                        $orders = wc_get_orders(
2✔
253
                                                                [
2✔
254
                                                                        'type'          => 'shop_order',
2✔
255
                                                                        'post__in'      => [ $order_id ],
2✔
256
                                                                        'customer_id'   => get_current_user_id(),
2✔
257
                                                                        'no_rows_found' => true,
2✔
258
                                                                        'return'        => 'ids',
2✔
259
                                                                ]
2✔
260
                                                        );
2✔
261

262
                                                        if ( in_array( $order_id, $orders, true ) ) {
2✔
263
                                                                $is_authorized = true;
1✔
264
                                                        }
265
                                                }
266

267
                                                // Throw if authorized to view order.
268
                                                if ( ! $is_authorized ) {
10✔
269
                                                        throw new UserError( __( 'Not authorized to access this order', 'wp-graphql-woocommerce' ) );
1✔
270
                                                }
271

272
                                                return Factory::resolve_crud_object( $order_id, $context );
10✔
273
                                        },
138✔
274
                                ],
138✔
275
                                'productVariation' => [
138✔
276
                                        'type'        => 'ProductVariation',
138✔
277
                                        'description' => __( 'A product variation object', 'wp-graphql-woocommerce' ),
138✔
278
                                        'args'        => [
138✔
279
                                                'id'     => [
138✔
280
                                                        'type'        => 'ID',
138✔
281
                                                        'description' => __( 'The ID for identifying the product variation', 'wp-graphql-woocommerce' ),
138✔
282
                                                ],
138✔
283
                                                'idType' => [
138✔
284
                                                        'type'        => 'ProductVariationIdTypeEnum',
138✔
285
                                                        'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
138✔
286
                                                ],
138✔
287
                                        ],
138✔
288
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
138✔
289
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
6✔
290
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
6✔
291

292
                                                $variation_id = null;
6✔
293
                                                switch ( $id_type ) {
294
                                                        case 'database_id':
6✔
295
                                                                $variation_id = absint( $id );
2✔
296
                                                                break;
2✔
297
                                                        case 'global_id':
5✔
298
                                                        default:
299
                                                                $id_components = Relay::fromGlobalId( $id );
5✔
300
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
5✔
301
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
302
                                                                }
303
                                                                $variation_id = absint( $id_components['id'] );
5✔
304
                                                                break;
5✔
305
                                                }
306

307
                                                if ( empty( $variation_id ) ) {
6✔
308
                                                        /* translators: %1$s: ID type, %2$s: ID value */
309
                                                        throw new UserError( sprintf( __( 'No product variation ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
310
                                                }
311

312
                                                $variation = get_post( $variation_id );
6✔
313
                                                if ( ! is_object( $variation ) || 'product_variation' !== $variation->post_type ) {
6✔
314
                                                        /* translators: %1$s: ID type, %2$s: ID value */
315
                                                        throw new UserError( sprintf( __( 'No product variation exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
316
                                                }
317

318
                                                return Factory::resolve_crud_object( $variation_id, $context );
6✔
319
                                        },
138✔
320
                                ],
138✔
321
                                'refund'           => [
138✔
322
                                        'type'        => 'Refund',
138✔
323
                                        'description' => __( 'A refund object', 'wp-graphql-woocommerce' ),
138✔
324
                                        'args'        => [
138✔
325
                                                'id'     => [
138✔
326
                                                        'type'        => [ 'non_null' => 'ID' ],
138✔
327
                                                        'description' => __( 'The ID for identifying the refund', 'wp-graphql-woocommerce' ),
138✔
328
                                                ],
138✔
329
                                                'idType' => [
138✔
330
                                                        'type'        => 'RefundIdTypeEnum',
138✔
331
                                                        'description' => __( 'Type of ID being used identify refund', 'wp-graphql-woocommerce' ),
138✔
332
                                                ],
138✔
333
                                        ],
138✔
334
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
138✔
335
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
3✔
336
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
3✔
337

338
                                                $refund_id = null;
3✔
339
                                                switch ( $id_type ) {
340
                                                        case 'database_id':
3✔
341
                                                                $refund_id = absint( $id );
1✔
342
                                                                break;
1✔
343
                                                        case 'global_id':
3✔
344
                                                        default:
345
                                                                $id_components = Relay::fromGlobalId( $id );
3✔
346
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
3✔
347
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
348
                                                                }
349
                                                                $refund_id = absint( $id_components['id'] );
3✔
350
                                                                break;
3✔
351
                                                }
352

353
                                                if ( empty( $refund_id ) ) {
3✔
354
                                                        /* translators: %1$s: ID type, %2$s: ID value */
355
                                                        throw new UserError( sprintf( __( 'No refund ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
356
                                                }
357

358
                                                if ( 'shop_order_refund' !== OrderUtil::get_order_type( $refund_id ) ) {
3✔
359
                                                        /* translators: %1$s: ID type, %2$s: ID value */
360
                                                        throw new UserError( sprintf( __( 'No refund exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
361
                                                }
362

363
                                                // Check if user authorized to view order.
364
                                                /**
365
                                                 * Get refund post type.
366
                                                 *
367
                                                 * @var \WP_Post_Type $post_type
368
                                                 */
369
                                                $post_type     = get_post_type_object( 'shop_order_refund' );
3✔
370
                                                $is_authorized = current_user_can( $post_type->cap->edit_others_posts );
3✔
371
                                                if ( get_current_user_id() ) {
3✔
372
                                                        $refund = \wc_get_order( $refund_id );
3✔
373
                                                        if ( ! is_object( $refund ) || ! is_a( $refund, \WC_Order_Refund::class ) ) {
3✔
374
                                                                throw new UserError( __( 'Failed to retrieve refund', 'wp-graphql-woocommerce' ) );
×
375
                                                        }
376
                                                        $order_id = $refund->get_parent_id();
3✔
377

378
                                                        /** @var \WC_Order[] $orders */
379
                                                        $orders = wc_get_orders(
3✔
380
                                                                [
3✔
381
                                                                        'type'          => 'shop_order',
3✔
382
                                                                        'post__in'      => [ $order_id ],
3✔
383
                                                                        'customer_id'   => get_current_user_id(),
3✔
384
                                                                        'no_rows_found' => true,
3✔
385
                                                                        'return'        => 'ids',
3✔
386
                                                                ]
3✔
387
                                                        );
3✔
388

389
                                                        if ( in_array( $order_id, $orders, true ) ) {
3✔
390
                                                                $is_authorized = true;
3✔
391
                                                        }
392
                                                }//end if
393

394
                                                // Throw if authorized to view refund.
395
                                                if ( ! $is_authorized ) {
3✔
396
                                                        throw new UserError( __( 'Not authorized to access this refund', 'wp-graphql-woocommerce' ) );
1✔
397
                                                }
398

399
                                                return Factory::resolve_crud_object( $refund_id, $context );
3✔
400
                                        },
138✔
401
                                ],
138✔
402
                                'shippingMethod'   => [
138✔
403
                                        'type'        => 'ShippingMethod',
138✔
404
                                        'description' => __( 'A shipping method object', 'wp-graphql-woocommerce' ),
138✔
405
                                        'args'        => [
138✔
406
                                                'id'     => [
138✔
407
                                                        'type'        => 'ID',
138✔
408
                                                        'description' => __( 'The ID for identifying the shipping method', 'wp-graphql-woocommerce' ),
138✔
409
                                                ],
138✔
410
                                                'idType' => [
138✔
411
                                                        'type'        => 'ShippingMethodIdTypeEnum',
138✔
412
                                                        'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
138✔
413
                                                ],
138✔
414
                                        ],
138✔
415
                                        'resolve'     => static function ( $source, array $args ) {
138✔
416
                                                if ( ! \wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) {
1✔
417
                                                        throw new UserError( __( 'Sorry, you cannot view shipping methods.', 'wp-graphql-woocommerce' ), \rest_authorization_required_code() );
1✔
418
                                                }
419

420
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
421
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
422

423
                                                $method_id = null;
1✔
424
                                                switch ( $id_type ) {
425
                                                        case 'database_id':
1✔
426
                                                                $method_id = $id;
1✔
427
                                                                break;
1✔
428
                                                        case 'global_id':
1✔
429
                                                        default:
430
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
431
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
432
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
433
                                                                }
434
                                                                $method_id = $id_components['id'];
1✔
435
                                                                break;
1✔
436
                                                }
437

438
                                                return Factory::resolve_shipping_method( $method_id );
1✔
439
                                        },
138✔
440
                                ],
138✔
441
                                'shippingZone'     => [
138✔
442
                                        'type'        => 'ShippingZone',
138✔
443
                                        'description' => __( 'A shipping zone object', 'wp-graphql-woocommerce' ),
138✔
444
                                        'args'        => [
138✔
445
                                                'id'     => [
138✔
446
                                                        'type'        => 'ID',
138✔
447
                                                        'description' => __( 'The ID for identifying the shipping zone', 'wp-graphql-woocommerce' ),
138✔
448
                                                ],
138✔
449
                                                'idType' => [
138✔
450
                                                        'type'        => 'ShippingZoneIdTypeEnum',
138✔
451
                                                        'description' => __( 'Type of ID being used identify shipping zone', 'wp-graphql-woocommerce' ),
138✔
452
                                                ],
138✔
453
                                        ],
138✔
454
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
138✔
455
                                                if ( ! \wc_shipping_enabled() ) {
1✔
NEW
456
                                                        throw new UserError( __( 'Shipping is disabled.', 'wp-graphql-woocommerce' ), 404 );
×
457
                                                }
458

459
                                                if ( ! \wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
1✔
460
                                                        throw new UserError( __( 'Permission denied.', 'wp-graphql-woocommerce' ), \rest_authorization_required_code() );
1✔
461
                                                }
462

463
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
464
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
465

466
                                                $zone_id = null;
1✔
467
                                                switch ( $id_type ) {
468
                                                        case 'database_id':
1✔
NEW
469
                                                                $zone_id = $id;
×
NEW
470
                                                                break;
×
471
                                                        case 'global_id':
1✔
472
                                                        default:
473
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
474
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
NEW
475
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
476
                                                                }
477
                                                                $zone_id = $id_components['id'];
1✔
478
                                                                break;
1✔
479
                                                }
480

481
                                                return $context->get_loader( 'shipping_zone' )->load( $zone_id );
1✔
482
                                        },
138✔
483
                                ],
138✔
484
                                'taxRate'          => [
138✔
485
                                        'type'        => 'TaxRate',
138✔
486
                                        'description' => __( 'A tax rate object', 'wp-graphql-woocommerce' ),
138✔
487
                                        'args'        => [
138✔
488
                                                'id'     => [
138✔
489
                                                        'type'        => 'ID',
138✔
490
                                                        'description' => __( 'The ID for identifying the tax rate', 'wp-graphql-woocommerce' ),
138✔
491
                                                ],
138✔
492
                                                'idType' => [
138✔
493
                                                        'type'        => 'TaxRateIdTypeEnum',
138✔
494
                                                        'description' => __( 'Type of ID being used identify tax rate', 'wp-graphql-woocommerce' ),
138✔
495
                                                ],
138✔
496
                                        ],
138✔
497
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
138✔
498
                                                if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
1✔
499
                                                        throw new UserError( __( 'Sorry, you cannot view tax rates.', 'wp-graphql-woocommerce' ), \rest_authorization_required_code() );
1✔
500
                                                }
501
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
502
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
503

504
                                                $rate_id = null;
1✔
505
                                                switch ( $id_type ) {
506
                                                        case 'database_id':
1✔
507
                                                                $rate_id = absint( $id );
1✔
508
                                                                break;
1✔
509
                                                        case 'global_id':
1✔
510
                                                        default:
511
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
512
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
513
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
514
                                                                }
515
                                                                $rate_id = absint( $id_components['id'] );
1✔
516
                                                                break;
1✔
517
                                                }
518

519
                                                return Factory::resolve_tax_rate( $rate_id, $context );
1✔
520
                                        },
138✔
521
                                ],
138✔
522
                                'countries'        => [
138✔
523
                                        'type'        => [ 'list_of' => 'CountriesEnum' ],
138✔
524
                                        'description' => __( 'Countries', 'wp-graphql-woocommerce' ),
138✔
525
                                        'resolve'     => static function () {
138✔
526
                                                $wc_countries = new \WC_Countries();
1✔
527
                                                $countries    = $wc_countries->get_countries();
1✔
528

529
                                                return array_keys( $countries );
1✔
530
                                        },
138✔
531
                                ],
138✔
532
                                'allowedCountries' => [
138✔
533
                                        'type'        => [ 'list_of' => 'CountriesEnum' ],
138✔
534
                                        'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
138✔
535
                                        'resolve'     => static function () {
138✔
536
                                                $wc_countries = new \WC_Countries();
1✔
537
                                                $countries    = $wc_countries->get_allowed_countries();
1✔
538

539
                                                return array_keys( $countries );
1✔
540
                                        },
138✔
541
                                ],
138✔
542
                                'countryStates'    => [
138✔
543
                                        'type'        => [ 'list_of' => 'CountryState' ],
138✔
544
                                        'args'        => [
138✔
545
                                                'country' => [
138✔
546
                                                        'type'        => [ 'non_null' => 'CountriesEnum' ],
138✔
547
                                                        'description' => __( 'Target country', 'wp-graphql-woocommerce' ),
138✔
548
                                                ],
138✔
549
                                        ],
138✔
550
                                        'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
138✔
551
                                        'resolve'     => static function ( $_, $args ) {
138✔
552
                                                $country      = $args['country'];
1✔
553
                                                $wc_countries = new \WC_Countries();
1✔
554
                                                $states       = $wc_countries->get_shipping_country_states();
1✔
555

556
                                                if ( ! empty( $states ) && ! empty( $states[ $country ] ) ) {
1✔
557
                                                        $formatted_states = [];
1✔
558
                                                        foreach ( $states[ $country ] as $code => $name ) {
1✔
559
                                                                $formatted_states[] = compact( 'name', 'code' );
1✔
560
                                                        }
561

562
                                                        return $formatted_states;
1✔
563
                                                }
564

565
                                                return [];
×
566
                                        },
138✔
567
                                ],
138✔
568
                                'collectionStats'  => [
138✔
569
                                        'type'        => 'CollectionStats',
138✔
570
                                        'args'        => [
138✔
571
                                                'calculatePriceRange'        => [
138✔
572
                                                        'type'        => 'Boolean',
138✔
573
                                                        'description' => __( 'If true, calculates the minimum and maximum product prices for the collection.', 'wp-graphql-woocommerce' ),
138✔
574
                                                ],
138✔
575
                                                'calculateRatingCounts'      => [
138✔
576
                                                        'type'        => 'Boolean',
138✔
577
                                                        'description' => __( 'If true, calculates rating counts for products in the collection.', 'wp-graphql-woocommerce' ),
138✔
578
                                                ],
138✔
579
                                                'calculateStockStatusCounts' => [
138✔
580
                                                        'type'        => 'Boolean',
138✔
581
                                                        'description' => __( 'If true, calculates stock counts for products in the collection.', 'wp-graphql-woocommerce' ),
138✔
582
                                                ],
138✔
583
                                                'taxonomies'                 => [
138✔
584
                                                        'type' => [ 'list_of' => 'CollectionStatsQueryInput' ],
138✔
585
                                                ],
138✔
586
                                                'where'                      => [
138✔
587
                                                        'type' => 'CollectionStatsWhereArgs',
138✔
588
                                                ],
138✔
589
                                        ],
138✔
590
                                        'description' => __( 'Statistics for a product taxonomy query', 'wp-graphql-woocommerce' ),
138✔
591
                                        'resolve'     => static function ( $_, $args ) {
138✔
592
                                                $data    = [
4✔
593
                                                        'min_price'           => null,
4✔
594
                                                        'max_price'           => null,
4✔
595
                                                        'attribute_counts'    => null,
4✔
596
                                                        'stock_status_counts' => null,
4✔
597
                                                        'rating_counts'       => null,
4✔
598
                                                ];
4✔
599
                                                $filters = new ProductQueryFilters(); // @phpstan-ignore-line
4✔
600

601
                                                // Process client-side filters.
602
                                                $request = Collection_Stats_Type::prepare_rest_request( $args['where'] ?? [] );
4✔
603

604
                                                // Format taxonomies.
605
                                                if ( ! empty( $args['taxonomies'] ) ) {
4✔
606
                                                        $calculate_attribute_counts = [];
4✔
607
                                                        foreach ( $args['taxonomies'] as $attribute_to_count ) {
4✔
608
                                                                $attribute = [ 'taxonomy' => $attribute_to_count['taxonomy'] ];
4✔
609
                                                                // Set the query type.
610
                                                                if ( ! empty( $attribute_to_count['relation'] ) ) {
4✔
611
                                                                        $attribute['query_type'] = strtolower( $attribute_to_count['relation'] );
4✔
612
                                                                }
613

614
                                                                // Add the attribute to the list of attributes to count.
615
                                                                $calculate_attribute_counts[] = $attribute;
4✔
616
                                                        }
617

618
                                                        // Set the attribute counts to calculate.
619
                                                        $request->set_param( 'calculate_attribute_counts', $calculate_attribute_counts );
4✔
620
                                                }
621

622
                                                $request->set_param( 'calculate_price_range', $args['calculatePriceRange'] ?? false );
4✔
623
                                                $request->set_param( 'calculate_stock_status_counts', $args['calculateStockStatusCounts'] ?? false );
4✔
624
                                                $request->set_param( 'calculate_rating_counts', $args['calculateRatingCounts'] ?? false );
4✔
625

626
                                                if ( ! empty( $request['calculate_price_range'] ) ) {
4✔
627
                                                        /**
628
                                                         * A Rest request object for external filtering
629
                                                         *
630
                                                         * @var \WP_REST_Request $filter_request
631
                                                         */
632
                                                        $filter_request = clone $request;
1✔
633
                                                        $filter_request->set_param( 'min_price', null );
1✔
634
                                                        $filter_request->set_param( 'max_price', null );
1✔
635

636
                                                        $price_results     = $filters->get_filtered_price( $filter_request ); // @phpstan-ignore-line
1✔
637
                                                        $data['min_price'] = $price_results->min_price;
1✔
638
                                                        $data['max_price'] = $price_results->max_price;
1✔
639
                                                }
640

641
                                                if ( ! empty( $request['calculate_stock_status_counts'] ) ) {
4✔
642
                                                        /**
643
                                                         * A Rest request object for external filtering
644
                                                         *
645
                                                         * @var \WP_REST_Request $filter_request
646
                                                         */
647
                                                        $filter_request = clone $request;
1✔
648
                                                        $counts         = $filters->get_stock_status_counts( $filter_request ); // @phpstan-ignore-line
1✔
649

650
                                                        $data['stock_status_counts'] = [];
1✔
651

652
                                                        foreach ( $counts as $key => $value ) {
1✔
653
                                                                $data['stock_status_counts'][] = (object) [
1✔
654
                                                                        'status' => $key,
1✔
655
                                                                        'count'  => $value,
1✔
656
                                                                ];
1✔
657
                                                        }
658
                                                }
659

660
                                                if ( ! empty( $request['calculate_attribute_counts'] ) ) {
4✔
661
                                                        $taxonomy__or_queries  = [];
4✔
662
                                                        $taxonomy__and_queries = [];
4✔
663
                                                        foreach ( $request['calculate_attribute_counts'] as $attributes_to_count ) {
4✔
664
                                                                if ( ! isset( $attributes_to_count['taxonomy'] ) ) {
4✔
665
                                                                        continue;
×
666
                                                                }
667

668
                                                                if ( empty( $attributes_to_count['query_type'] ) || 'or' === $attributes_to_count['query_type'] ) {
4✔
669
                                                                        $taxonomy__or_queries[] = $attributes_to_count['taxonomy'];
3✔
670
                                                                } else {
671
                                                                        $taxonomy__and_queries[] = $attributes_to_count['taxonomy'];
3✔
672
                                                                }
673
                                                        }
674

675
                                                        $data['attribute_counts'] = [];
4✔
676
                                                        if ( ! empty( $taxonomy__or_queries ) ) {
4✔
677
                                                                foreach ( $taxonomy__or_queries as $taxonomy ) {
3✔
678
                                                                        /**
679
                                                                         * A Rest request object for external filtering
680
                                                                         *
681
                                                                         * @var \WP_REST_Request $filter_request
682
                                                                         */
683
                                                                        $filter_request    = clone $request;
3✔
684
                                                                        $filter_attributes = $filter_request->get_param( 'attributes' );
3✔
685

686
                                                                        if ( ! empty( $filter_attributes ) ) {
3✔
687
                                                                                $filter_attributes = array_filter(
×
688
                                                                                        $filter_attributes,
×
689
                                                                                        static function ( $query ) use ( $taxonomy ) {
×
690
                                                                                                return $query['attribute'] !== $taxonomy;
×
691
                                                                                        }
×
692
                                                                                );
×
693
                                                                        }
694

695
                                                                        $filter_request->set_param( 'attributes', $filter_attributes );
3✔
696
                                                                        $counts = $filters->get_attribute_counts( $filter_request, [ $taxonomy ] ); // @phpstan-ignore-line
3✔
697

698
                                                                        $data['attribute_counts'][ $taxonomy ] = [];
3✔
699
                                                                        foreach ( $counts as $key => $value ) {
3✔
700
                                                                                $data['attribute_counts'][ $taxonomy ][] = (object) [
3✔
701
                                                                                        'taxonomy' => $taxonomy,
3✔
702
                                                                                        'termId'   => $key,
3✔
703
                                                                                        'count'    => $value,
3✔
704
                                                                                ];
3✔
705
                                                                        }
706
                                                                }
707
                                                        }
708

709
                                                        if ( ! empty( $taxonomy__and_queries ) ) {
4✔
710
                                                                $counts = $filters->get_attribute_counts( $request, $taxonomy__and_queries ); // @phpstan-ignore-line
3✔
711

712
                                                                foreach ( $taxonomy__and_queries as $taxonomy ) {
3✔
713
                                                                        $data['attribute_counts'][ $taxonomy ] = [];
3✔
714
                                                                        foreach ( $counts as $key => $value ) {
3✔
715
                                                                                $data['attribute_counts'][ $taxonomy ][] = (object) [
3✔
716
                                                                                        'taxonomy' => $taxonomy,
3✔
717
                                                                                        'termId'   => $key,
3✔
718
                                                                                        'count'    => $value,
3✔
719
                                                                                ];
3✔
720
                                                                        }
721
                                                                }
722
                                                        }
723
                                                }
724

725
                                                if ( ! empty( $request['calculate_rating_counts'] ) ) {
4✔
726
                                                        /**
727
                                                         * A Rest request object for external filtering
728
                                                         *
729
                                                         * @var \WP_REST_Request $filter_request
730
                                                         */
731
                                                        $filter_request        = clone $request;
4✔
732
                                                        $counts                = $filters->get_rating_counts( $filter_request ); // @phpstan-ignore-line
4✔
733
                                                        $data['rating_counts'] = [];
4✔
734

735
                                                        foreach ( $counts as $key => $value ) {
4✔
736
                                                                $data['rating_counts'][] = (object) [
×
737
                                                                        'rating' => $key,
×
738
                                                                        'count'  => $value,
×
739
                                                                ];
×
740
                                                        }
741
                                                }
742

743
                                                return $data;
4✔
744
                                        },
138✔
745
                                ],
138✔
746
                        ]
138✔
747
                );
138✔
748

749
                // Product queries.
750
                $unsupported_type_enabled = woographql_setting( 'enable_unsupported_product_type', 'off' );
138✔
751

752
                $product_type_keys = array_keys( WooGraphQL::get_enabled_product_types() );
138✔
753
                if ( 'on' === $unsupported_type_enabled ) {
138✔
754
                        $product_type_keys[] = 'unsupported';
1✔
755
                }
756

757
                $product_type_keys = apply_filters( 'woographql_register_product_queries', $product_type_keys );
138✔
758

759
                $product_types = WooGraphQL::get_enabled_product_types();
138✔
760
                if ( 'on' === $unsupported_type_enabled ) {
138✔
761
                        $product_types['unsupported'] = WooGraphQL::get_supported_product_type();
1✔
762
                }
763

764
                foreach ( $product_type_keys as $type_key ) {
138✔
765
                        $field_name = "{$type_key}Product";
138✔
766
                        $type_name  = $product_types[ $type_key ] ?? null;
138✔
767

768
                        if ( empty( $type_name ) ) {
138✔
769
                                continue;
×
770
                        }
771

772
                        register_graphql_field(
138✔
773
                                'RootQuery',
138✔
774
                                $field_name,
138✔
775
                                [
138✔
776
                                        'type'              => $type_name,
138✔
777
                                        /* translators: Product type slug */
778
                                        'description'       => sprintf( __( 'A %s product object', 'wp-graphql-woocommerce' ), $type_key ),
138✔
779
                                        'deprecationReason' => 'Use "product" instead.',
138✔
780
                                        'args'              => [
138✔
781
                                                'id'     => [
138✔
782
                                                        'type'        => 'ID',
138✔
783
                                                        'description' => sprintf(
138✔
784
                                                                /* translators: %s: product type */
785
                                                                __( 'The ID for identifying the %s product', 'wp-graphql-woocommerce' ),
138✔
786
                                                                $type_name
138✔
787
                                                        ),
138✔
788
                                                ],
138✔
789
                                                'idType' => [
138✔
790
                                                        'type'        => 'ProductIdTypeEnum',
138✔
791
                                                        'description' => __( 'Type of ID being used identify product', 'wp-graphql-woocommerce' ),
138✔
792
                                                ],
138✔
793
                                        ],
138✔
794
                                        'resolve'           => static function ( $source, array $args, AppContext $context ) use ( $type_key, $unsupported_type_enabled ) {
138✔
795
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
796
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
797

798
                                                $product_id = null;
1✔
799
                                                switch ( $id_type ) {
800
                                                        case 'sku':
1✔
801
                                                                $product_id = \wc_get_product_id_by_sku( $id );
×
802
                                                                break;
×
803
                                                        case 'slug':
1✔
804
                                                                $post       = get_page_by_path( $id, OBJECT, 'product' );
×
805
                                                                $product_id = ! empty( $post ) ? absint( $post->ID ) : 0;
×
806
                                                                break;
×
807
                                                        case 'database_id':
1✔
808
                                                                $product_id = absint( $id );
×
809
                                                                break;
×
810
                                                        case 'global_id':
1✔
811
                                                        default:
812
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
813
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
814
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
815
                                                                }
816
                                                                $product_id = absint( $id_components['id'] );
1✔
817
                                                                break;
1✔
818
                                                }
819

820
                                                if ( empty( $product_id ) ) {
1✔
821
                                                        /* translators: %1$s: ID type, %2$s: ID value */
822
                                                        throw new UserError( sprintf( __( 'No product ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $product_id ) );
×
823
                                                }
824

825
                                                if ( \WC()->product_factory->get_product_type( $product_id ) !== $type_key && 'off' === $unsupported_type_enabled ) {
1✔
826
                                                        /* translators: Invalid product type message %1$s: Product ID, %2$s: Product type */
827
                                                        throw new UserError( sprintf( __( 'This product of ID %1$s is not a %2$s product', 'wp-graphql-woocommerce' ), $product_id, $type_key ) );
×
828
                                                }
829

830
                                                $product = get_post( $product_id );
1✔
831
                                                if ( ! is_object( $product ) || 'product' !== $product->post_type ) {
1✔
832
                                                        /* translators: %1$s: ID type, %2$s: ID value */
833
                                                        throw new UserError( sprintf( __( 'No product exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $product_id ) );
×
834
                                                }
835

836
                                                return Factory::resolve_crud_object( $product_id, $context );
1✔
837
                                        },
138✔
838
                                ]
138✔
839
                        );
138✔
840
                }//end foreach
841
        }
842
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc