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

wp-graphql / wp-graphql-woocommerce / 9121529590

17 May 2024 01:27AM UTC coverage: 84.681% (+0.1%) from 84.581%
9121529590

push

github

web-flow
`collectionStats` query fully implemented (#849)

* fix: WPGraphQL v1.24.x support added

* fix: WPGraphQL v1.24.x support added

* chore: Linter and PHPStan compliance met

* chore: composer.json fixed

* fix: Unnecessary type check restored

* chore: Linter and PHPStan compliance met

* devops: CollectionStatsQueryTest patched

138 of 168 new or added lines in 10 files covered. (82.14%)

1 existing line in 1 file now uncovered.

11199 of 13225 relevant lines covered (84.68%)

60.39 hits per line

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

91.82
/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(
118✔
30
                        'RootQuery',
118✔
31
                        [
118✔
32
                                'cart'             => [
118✔
33
                                        'type'        => 'Cart',
118✔
34
                                        'args'        => [
118✔
35
                                                'recalculateTotals' => [
118✔
36
                                                        'type'        => 'Boolean',
118✔
37
                                                        'description' => __( 'Should cart totals be recalculated.', 'wp-graphql-woocommerce' ),
118✔
38
                                                ],
118✔
39
                                        ],
118✔
40
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
118✔
41
                                        'resolve'     => static function ( $_, $args ) {
118✔
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
                                        },
118✔
54
                                ],
118✔
55
                                'cartItem'         => [
118✔
56
                                        'type'        => 'CartItem',
118✔
57
                                        'args'        => [
118✔
58
                                                'key' => [
118✔
59
                                                        'type' => [ 'non_null' => 'ID' ],
118✔
60
                                                ],
118✔
61
                                        ],
118✔
62
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
118✔
63
                                        'resolve'     => static function ( $source, array $args ) {
118✔
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
                                        },
118✔
71
                                ],
118✔
72
                                'cartFee'          => [
118✔
73
                                        'type'        => 'CartFee',
118✔
74
                                        'args'        => [
118✔
75
                                                'id' => [
118✔
76
                                                        'type' => [ 'non_null' => 'ID' ],
118✔
77
                                                ],
118✔
78
                                        ],
118✔
79
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
118✔
80
                                        'resolve'     => static function ( $source, array $args ) {
118✔
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
                                        },
118✔
90
                                ],
118✔
91
                                'coupon'           => [
118✔
92
                                        'type'        => 'Coupon',
118✔
93
                                        'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ),
118✔
94
                                        'args'        => [
118✔
95
                                                'id'     => [ 'type' => [ 'non_null' => 'ID' ] ],
118✔
96
                                                'idType' => [
118✔
97
                                                        'type'        => 'CouponIdTypeEnum',
118✔
98
                                                        'description' => __( 'Type of ID being used identify coupon', 'wp-graphql-woocommerce' ),
118✔
99
                                                ],
118✔
100
                                        ],
118✔
101
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
118✔
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
                                        },
118✔
148
                                ],
118✔
149
                                'customer'         => [
118✔
150
                                        'type'        => 'Customer',
118✔
151
                                        'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
118✔
152
                                        'args'        => [
118✔
153
                                                'id'         => [
118✔
154
                                                        'type'        => 'ID',
118✔
155
                                                        'description' => __( 'Get the customer by their global ID', 'wp-graphql-woocommerce' ),
118✔
156
                                                ],
118✔
157
                                                'customerId' => [
118✔
158
                                                        'type'        => 'Int',
118✔
159
                                                        'description' => __( 'Get the customer by their database ID', 'wp-graphql-woocommerce' ),
118✔
160
                                                ],
118✔
161
                                        ],
118✔
162
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
118✔
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
                                        },
118✔
196
                                ],
118✔
197
                                'order'            => [
118✔
198
                                        'type'        => 'Order',
118✔
199
                                        'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
118✔
200
                                        'args'        => [
118✔
201
                                                'id'     => [
118✔
202
                                                        'type'        => 'ID',
118✔
203
                                                        'description' => __( 'The ID for identifying the order', 'wp-graphql-woocommerce' ),
118✔
204
                                                ],
118✔
205
                                                'idType' => [
118✔
206
                                                        'type'        => 'OrderIdTypeEnum',
118✔
207
                                                        'description' => __( 'Type of ID being used identify order', 'wp-graphql-woocommerce' ),
118✔
208
                                                ],
118✔
209
                                        ],
118✔
210
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
118✔
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
                                        },
118✔
274
                                ],
118✔
275
                                'productVariation' => [
118✔
276
                                        'type'        => 'ProductVariation',
118✔
277
                                        'description' => __( 'A product variation object', 'wp-graphql-woocommerce' ),
118✔
278
                                        'args'        => [
118✔
279
                                                'id'     => [
118✔
280
                                                        'type'        => 'ID',
118✔
281
                                                        'description' => __( 'The ID for identifying the product variation', 'wp-graphql-woocommerce' ),
118✔
282
                                                ],
118✔
283
                                                'idType' => [
118✔
284
                                                        'type'        => 'ProductVariationIdTypeEnum',
118✔
285
                                                        'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
118✔
286
                                                ],
118✔
287
                                        ],
118✔
288
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
118✔
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
                                        },
118✔
320
                                ],
118✔
321
                                'refund'           => [
118✔
322
                                        'type'        => 'Refund',
118✔
323
                                        'description' => __( 'A refund object', 'wp-graphql-woocommerce' ),
118✔
324
                                        'args'        => [
118✔
325
                                                'id'     => [
118✔
326
                                                        'type'        => [ 'non_null' => 'ID' ],
118✔
327
                                                        'description' => __( 'The ID for identifying the refund', 'wp-graphql-woocommerce' ),
118✔
328
                                                ],
118✔
329
                                                'idType' => [
118✔
330
                                                        'type'        => 'RefundIdTypeEnum',
118✔
331
                                                        'description' => __( 'Type of ID being used identify refund', 'wp-graphql-woocommerce' ),
118✔
332
                                                ],
118✔
333
                                        ],
118✔
334
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
118✔
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
                                        },
118✔
401
                                ],
118✔
402
                                'shippingMethod'   => [
118✔
403
                                        'type'        => 'ShippingMethod',
118✔
404
                                        'description' => __( 'A shipping method object', 'wp-graphql-woocommerce' ),
118✔
405
                                        'args'        => [
118✔
406
                                                'id'     => [
118✔
407
                                                        'type'        => 'ID',
118✔
408
                                                        'description' => __( 'The ID for identifying the shipping method', 'wp-graphql-woocommerce' ),
118✔
409
                                                ],
118✔
410
                                                'idType' => [
118✔
411
                                                        'type'        => 'ShippingMethodIdTypeEnum',
118✔
412
                                                        'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
118✔
413
                                                ],
118✔
414
                                        ],
118✔
415
                                        'resolve'     => static function ( $source, array $args ) {
118✔
416
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
417
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
418

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

434
                                                return Factory::resolve_shipping_method( $method_id );
1✔
435
                                        },
118✔
436
                                ],
118✔
437
                                'taxRate'          => [
118✔
438
                                        'type'        => 'TaxRate',
118✔
439
                                        'description' => __( 'A tax rate object', 'wp-graphql-woocommerce' ),
118✔
440
                                        'args'        => [
118✔
441
                                                'id'     => [
118✔
442
                                                        'type'        => 'ID',
118✔
443
                                                        'description' => __( 'The ID for identifying the tax rate', 'wp-graphql-woocommerce' ),
118✔
444
                                                ],
118✔
445
                                                'idType' => [
118✔
446
                                                        'type'        => 'TaxRateIdTypeEnum',
118✔
447
                                                        'description' => __( 'Type of ID being used identify tax rate', 'wp-graphql-woocommerce' ),
118✔
448
                                                ],
118✔
449
                                        ],
118✔
450
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
118✔
451
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
452
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
453

454
                                                $rate_id = null;
1✔
455
                                                switch ( $id_type ) {
456
                                                        case 'database_id':
1✔
457
                                                                $rate_id = absint( $id );
1✔
458
                                                                break;
1✔
459
                                                        case 'global_id':
1✔
460
                                                        default:
461
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
462
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
463
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
464
                                                                }
465
                                                                $rate_id = absint( $id_components['id'] );
1✔
466
                                                                break;
1✔
467
                                                }
468

469
                                                return Factory::resolve_tax_rate( $rate_id, $context );
1✔
470
                                        },
118✔
471
                                ],
118✔
472
                                'countries'        => [
118✔
473
                                        'type'        => [ 'list_of' => 'CountriesEnum' ],
118✔
474
                                        'description' => __( 'Countries', 'wp-graphql-woocommerce' ),
118✔
475
                                        'resolve'     => static function () {
118✔
476
                                                $wc_countries = new \WC_Countries();
1✔
477
                                                $countries    = $wc_countries->get_countries();
1✔
478

479
                                                return array_keys( $countries );
1✔
480
                                        },
118✔
481
                                ],
118✔
482
                                'allowedCountries' => [
118✔
483
                                        'type'        => [ 'list_of' => 'CountriesEnum' ],
118✔
484
                                        'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
118✔
485
                                        'resolve'     => static function () {
118✔
486
                                                $wc_countries = new \WC_Countries();
1✔
487
                                                $countries    = $wc_countries->get_allowed_countries();
1✔
488

489
                                                return array_keys( $countries );
1✔
490
                                        },
118✔
491
                                ],
118✔
492
                                'countryStates'    => [
118✔
493
                                        'type'        => [ 'list_of' => 'CountryState' ],
118✔
494
                                        'args'        => [
118✔
495
                                                'country' => [
118✔
496
                                                        'type'        => [ 'non_null' => 'CountriesEnum' ],
118✔
497
                                                        'description' => __( 'Target country', 'wp-graphql-woocommerce' ),
118✔
498
                                                ],
118✔
499
                                        ],
118✔
500
                                        'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
118✔
501
                                        'resolve'     => static function ( $_, $args ) {
118✔
502
                                                $country      = $args['country'];
1✔
503
                                                $wc_countries = new \WC_Countries();
1✔
504
                                                $states       = $wc_countries->get_shipping_country_states();
1✔
505

506
                                                if ( ! empty( $states ) && ! empty( $states[ $country ] ) ) {
1✔
507
                                                        $formatted_states = [];
1✔
508
                                                        foreach ( $states[ $country ] as $code => $name ) {
1✔
509
                                                                $formatted_states[] = compact( 'name', 'code' );
1✔
510
                                                        }
511

512
                                                        return $formatted_states;
1✔
513
                                                }
514

515
                                                return [];
×
516
                                        },
118✔
517
                                ],
118✔
518
                                'collectionStats'  => [
118✔
519
                                        'type'        => 'CollectionStats',
118✔
520
                                        'args'        => [
118✔
521
                                                'calculatePriceRange'        => [
118✔
522
                                                        'type'        => 'Boolean',
118✔
523
                                                        'description' => __( 'If true, calculates the minimum and maximum product prices for the collection.', 'wp-graphql-woocommerce' ),
118✔
524
                                                ],
118✔
525
                                                'calculateRatingCounts'      => [
118✔
526
                                                        'type'        => 'Boolean',
118✔
527
                                                        'description' => __( 'If true, calculates rating counts for products in the collection.', 'wp-graphql-woocommerce' ),
118✔
528
                                                ],
118✔
529
                                                'calculateStockStatusCounts' => [
118✔
530
                                                        'type'        => 'Boolean',
118✔
531
                                                        'description' => __( 'If true, calculates stock counts for products in the collection.', 'wp-graphql-woocommerce' ),
118✔
532
                                                ],
118✔
533
                                                'taxonomies'                 => [
118✔
534
                                                        'type' => [ 'list_of' => 'CollectionStatsQueryInput' ],
118✔
535
                                                ],
118✔
536
                                                'where'                      => [
118✔
537
                                                        'type' => 'CollectionStatsWhereArgs',
118✔
538
                                                ],
118✔
539
                                        ],
118✔
540
                                        'description' => __( 'Statistics for a product taxonomy query', 'wp-graphql-woocommerce' ),
118✔
541
                                        'resolve'     => static function ( $_, $args ) {
118✔
542
                                                $data    = [
4✔
543
                                                        'min_price'           => null,
4✔
544
                                                        'max_price'           => null,
4✔
545
                                                        'attribute_counts'    => null,
4✔
546
                                                        'stock_status_counts' => null,
4✔
547
                                                        'rating_counts'       => null,
4✔
548
                                                ];
4✔
549
                                                $filters = new ProductQueryFilters(); // @phpstan-ignore-line
4✔
550

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

554
                                                // Format taxonomies.
555
                                                if ( ! empty( $args['taxonomies'] ) ) {
4✔
556
                                                        $calculate_attribute_counts = [];
4✔
557
                                                        foreach ( $args['taxonomies'] as $attribute_to_count ) {
4✔
558
                                                                $attribute = [ 'taxonomy' => $attribute_to_count['taxonomy'] ];
4✔
559
                                                                // Set the query type.
560
                                                                if ( ! empty( $attribute_to_count['relation'] ) ) {
4✔
561
                                                                        $attribute['query_type'] = strtolower( $attribute_to_count['relation'] );
4✔
562
                                                                }
563

564
                                                                // Add the attribute to the list of attributes to count.
565
                                                                $calculate_attribute_counts[] = $attribute;
4✔
566
                                                        }
567

568
                                                        // Set the attribute counts to calculate.
569
                                                        $request->set_param( 'calculate_attribute_counts', $calculate_attribute_counts );
4✔
570
                                                }
571

572
                                                $request->set_param( 'calculate_price_range', $args['calculatePriceRange'] ?? false );
4✔
573
                                                $request->set_param( 'calculate_stock_status_counts', $args['calculateStockStatusCounts'] ?? false );
4✔
574
                                                $request->set_param( 'calculate_rating_counts', $args['calculateRatingCounts'] ?? false );
4✔
575

576
                                                if ( ! empty( $request['calculate_price_range'] ) ) {
4✔
577
                                                        /**
578
                                                         * A Rest request object for external filtering
579
                                                         *
580
                                                         * @var \WP_REST_Request $filter_request
581
                                                         */
582
                                                        $filter_request = clone $request;
1✔
583
                                                        $filter_request->set_param( 'min_price', null );
1✔
584
                                                        $filter_request->set_param( 'max_price', null );
1✔
585

586
                                                        $price_results     = $filters->get_filtered_price( $filter_request ); // @phpstan-ignore-line
1✔
587
                                                        $data['min_price'] = $price_results->min_price;
1✔
588
                                                        $data['max_price'] = $price_results->max_price;
1✔
589
                                                }
590

591
                                                if ( ! empty( $request['calculate_stock_status_counts'] ) ) {
4✔
592
                                                        /**
593
                                                         * A Rest request object for external filtering
594
                                                         *
595
                                                         * @var \WP_REST_Request $filter_request
596
                                                         */
597
                                                        $filter_request = clone $request;
1✔
598
                                                        $counts         = $filters->get_stock_status_counts( $filter_request ); // @phpstan-ignore-line
1✔
599

600
                                                        $data['stock_status_counts'] = [];
1✔
601

602
                                                        foreach ( $counts as $key => $value ) {
1✔
603
                                                                $data['stock_status_counts'][] = (object) [
1✔
604
                                                                        'status' => $key,
1✔
605
                                                                        'count'  => $value,
1✔
606
                                                                ];
1✔
607
                                                        }
608
                                                }
609

610
                                                if ( ! empty( $request['calculate_attribute_counts'] ) ) {
4✔
611
                                                        $taxonomy__or_queries  = [];
4✔
612
                                                        $taxonomy__and_queries = [];
4✔
613
                                                        foreach ( $request['calculate_attribute_counts'] as $attributes_to_count ) {
4✔
614
                                                                if ( ! isset( $attributes_to_count['taxonomy'] ) ) {
4✔
615
                                                                        continue;
×
616
                                                                }
617

618
                                                                if ( empty( $attributes_to_count['query_type'] ) || 'or' === $attributes_to_count['query_type'] ) {
4✔
619
                                                                        $taxonomy__or_queries[] = $attributes_to_count['taxonomy'];
3✔
620
                                                                } else {
621
                                                                        $taxonomy__and_queries[] = $attributes_to_count['taxonomy'];
3✔
622
                                                                }
623
                                                        }
624

625
                                                        $data['attribute_counts'] = [];
4✔
626
                                                        if ( ! empty( $taxonomy__or_queries ) ) {
4✔
627
                                                                foreach ( $taxonomy__or_queries as $taxonomy ) {
3✔
628
                                                                        /**
629
                                                                         * A Rest request object for external filtering
630
                                                                         *
631
                                                                         * @var \WP_REST_Request $filter_request
632
                                                                         */
633
                                                                        $filter_request    = clone $request;
3✔
634
                                                                        $filter_attributes = $filter_request->get_param( 'attributes' );
3✔
635

636
                                                                        if ( ! empty( $filter_attributes ) ) {
3✔
NEW
637
                                                                                $filter_attributes = array_filter(
×
NEW
638
                                                                                        $filter_attributes,
×
NEW
639
                                                                                        static function ( $query ) use ( $taxonomy ) {
×
NEW
640
                                                                                                return $query['attribute'] !== $taxonomy;
×
NEW
641
                                                                                        }
×
NEW
642
                                                                                );
×
643
                                                                        }
644

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

648
                                                                        $data['attribute_counts'][ $taxonomy ] = [];
3✔
649
                                                                        foreach ( $counts as $key => $value ) {
3✔
650
                                                                                $data['attribute_counts'][ $taxonomy ][] = (object) [
3✔
651
                                                                                        'taxonomy' => $taxonomy,
3✔
652
                                                                                        'termId'   => $key,
3✔
653
                                                                                        'count'    => $value,
3✔
654
                                                                                ];
3✔
655
                                                                        }
656
                                                                }
657
                                                        }
658

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

662
                                                                foreach ( $taxonomy__and_queries as $taxonomy ) {
3✔
663
                                                                        $data['attribute_counts'][ $taxonomy ] = [];
3✔
664
                                                                        foreach ( $counts as $key => $value ) {
3✔
665
                                                                                $data['attribute_counts'][ $taxonomy ][] = (object) [
3✔
666
                                                                                        'taxonomy' => $taxonomy,
3✔
667
                                                                                        'termId'   => $key,
3✔
668
                                                                                        'count'    => $value,
3✔
669
                                                                                ];
3✔
670
                                                                        }
671
                                                                }
672
                                                        }
673
                                                }
674

675
                                                if ( ! empty( $request['calculate_rating_counts'] ) ) {
4✔
676
                                                        /**
677
                                                         * A Rest request object for external filtering
678
                                                         *
679
                                                         * @var \WP_REST_Request $filter_request
680
                                                         */
681
                                                        $filter_request        = clone $request;
4✔
682
                                                        $counts                = $filters->get_rating_counts( $filter_request ); // @phpstan-ignore-line
4✔
683
                                                        $data['rating_counts'] = [];
4✔
684

685
                                                        foreach ( $counts as $key => $value ) {
4✔
686
                                                                $data['rating_counts'][] = (object) [
×
687
                                                                        'rating' => $key,
×
688
                                                                        'count'  => $value,
×
689
                                                                ];
×
690
                                                        }
691
                                                }
692

693
                                                return $data;
4✔
694
                                        },
118✔
695
                                ],
118✔
696
                        ]
118✔
697
                );
118✔
698

699
                // Product queries.
700
                $unsupported_type_enabled = woographql_setting( 'enable_unsupported_product_type', 'off' );
118✔
701

702
                $product_type_keys = array_keys( WooGraphQL::get_enabled_product_types() );
118✔
703
                if ( 'on' === $unsupported_type_enabled ) {
118✔
704
                        $product_type_keys[] = 'unsupported';
1✔
705
                }
706

707
                $product_type_keys = apply_filters( 'woographql_register_product_queries', $product_type_keys );
118✔
708

709
                $product_types = WooGraphQL::get_enabled_product_types();
118✔
710
                if ( 'on' === $unsupported_type_enabled ) {
118✔
711
                        $product_types['unsupported'] = WooGraphQL::get_supported_product_type();
1✔
712
                }
713

714
                foreach ( $product_type_keys as $type_key ) {
118✔
715
                        $field_name = "{$type_key}Product";
118✔
716
                        $type_name  = $product_types[ $type_key ] ?? null;
118✔
717

718
                        if ( empty( $type_name ) ) {
118✔
719
                                continue;
×
720
                        }
721

722
                        register_graphql_field(
118✔
723
                                'RootQuery',
118✔
724
                                $field_name,
118✔
725
                                [
118✔
726
                                        'type'              => $type_name,
118✔
727
                                        /* translators: Product type slug */
728
                                        'description'       => sprintf( __( 'A %s product object', 'wp-graphql-woocommerce' ), $type_key ),
118✔
729
                                        'deprecationReason' => 'Use "product" instead.',
118✔
730
                                        'args'              => [
118✔
731
                                                'id'     => [
118✔
732
                                                        'type'        => 'ID',
118✔
733
                                                        'description' => sprintf(
118✔
734
                                                                /* translators: %s: product type */
735
                                                                __( 'The ID for identifying the %s product', 'wp-graphql-woocommerce' ),
118✔
736
                                                                $type_name
118✔
737
                                                        ),
118✔
738
                                                ],
118✔
739
                                                'idType' => [
118✔
740
                                                        'type'        => 'ProductIdTypeEnum',
118✔
741
                                                        'description' => __( 'Type of ID being used identify product', 'wp-graphql-woocommerce' ),
118✔
742
                                                ],
118✔
743
                                        ],
118✔
744
                                        'resolve'           => static function ( $source, array $args, AppContext $context ) use ( $type_key, $unsupported_type_enabled ) {
118✔
745
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
746
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
747

748
                                                $product_id = null;
1✔
749
                                                switch ( $id_type ) {
750
                                                        case 'sku':
1✔
751
                                                                $product_id = \wc_get_product_id_by_sku( $id );
×
752
                                                                break;
×
753
                                                        case 'slug':
1✔
754
                                                                $post       = get_page_by_path( $id, OBJECT, 'product' );
×
755
                                                                $product_id = ! empty( $post ) ? absint( $post->ID ) : 0;
×
756
                                                                break;
×
757
                                                        case 'database_id':
1✔
758
                                                                $product_id = absint( $id );
×
759
                                                                break;
×
760
                                                        case 'global_id':
1✔
761
                                                        default:
762
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
763
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
764
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
765
                                                                }
766
                                                                $product_id = absint( $id_components['id'] );
1✔
767
                                                                break;
1✔
768
                                                }
769

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

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

780
                                                $product = get_post( $product_id );
1✔
781
                                                if ( ! is_object( $product ) || 'product' !== $product->post_type ) {
1✔
782
                                                        /* translators: %1$s: ID type, %2$s: ID value */
783
                                                        throw new UserError( sprintf( __( 'No product exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $product_id ) );
×
784
                                                }
785

786
                                                return Factory::resolve_crud_object( $product_id, $context );
1✔
787
                                        },
118✔
788
                                ]
118✔
789
                        );
118✔
790
                }//end foreach
791
        }
792
}
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