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

wp-graphql / wp-graphql-woocommerce / 6226194617

18 Sep 2023 06:19PM UTC coverage: 84.684% (+1.9%) from 82.817%
6226194617

push

github

GitHub
chore: version numbers set and readmes updated. (#805)

10533 of 12438 relevant lines covered (84.68%)

57.78 hits per line

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

92.25
/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\Utilities\OrderUtil;
12
use GraphQL\Error\UserError;
13
use GraphQL\Type\Definition\ResolveInfo;
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(
113✔
30
                        'RootQuery',
113✔
31
                        [
113✔
32
                                'cart'             => [
113✔
33
                                        'type'        => 'Cart',
113✔
34
                                        'args'        => [
113✔
35
                                                'recalculateTotals' => [
113✔
36
                                                        'type'        => 'Boolean',
113✔
37
                                                        'description' => __( 'Should cart totals be recalculated.', 'wp-graphql-woocommerce' ),
113✔
38
                                                ],
113✔
39
                                        ],
113✔
40
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
113✔
41
                                        'resolve'     => static function ( $_, $args ) {
113✔
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
                                        },
113✔
54
                                ],
113✔
55
                                'cartItem'         => [
113✔
56
                                        'type'        => 'CartItem',
113✔
57
                                        'args'        => [
113✔
58
                                                'key' => [
113✔
59
                                                        'type' => [ 'non_null' => 'ID' ],
113✔
60
                                                ],
113✔
61
                                        ],
113✔
62
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
113✔
63
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
113✔
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
                                        },
113✔
71
                                ],
113✔
72
                                'cartFee'          => [
113✔
73
                                        'type'        => 'CartFee',
113✔
74
                                        'args'        => [
113✔
75
                                                'id' => [
113✔
76
                                                        'type' => [ 'non_null' => 'ID' ],
113✔
77
                                                ],
113✔
78
                                        ],
113✔
79
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
113✔
80
                                        'resolve'     => static function ( $source, array $args ) {
113✔
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
                                        },
113✔
90
                                ],
113✔
91
                                'coupon'           => [
113✔
92
                                        'type'        => 'Coupon',
113✔
93
                                        'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ),
113✔
94
                                        'args'        => [
113✔
95
                                                'id'     => [ 'type' => [ 'non_null' => 'ID' ] ],
113✔
96
                                                'idType' => [
113✔
97
                                                        'type'        => 'CouponIdTypeEnum',
113✔
98
                                                        'description' => __( 'Type of ID being used identify coupon', 'wp-graphql-woocommerce' ),
113✔
99
                                                ],
113✔
100
                                        ],
113✔
101
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
113✔
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
                                        },
113✔
148
                                ],
113✔
149
                                'customer'         => [
113✔
150
                                        'type'        => 'Customer',
113✔
151
                                        'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
113✔
152
                                        'args'        => [
113✔
153
                                                'id'         => [
113✔
154
                                                        'type'        => 'ID',
113✔
155
                                                        'description' => __( 'Get the customer by their global ID', 'wp-graphql-woocommerce' ),
113✔
156
                                                ],
113✔
157
                                                'customerId' => [
113✔
158
                                                        'type'        => 'Int',
113✔
159
                                                        'description' => __( 'Get the customer by their database ID', 'wp-graphql-woocommerce' ),
113✔
160
                                                ],
113✔
161
                                        ],
113✔
162
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
113✔
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
                                        },
113✔
196
                                ],
113✔
197
                                'order'            => [
113✔
198
                                        'type'        => 'Order',
113✔
199
                                        'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
113✔
200
                                        'args'        => [
113✔
201
                                                'id'     => [
113✔
202
                                                        'type'        => 'ID',
113✔
203
                                                        'description' => __( 'The ID for identifying the order', 'wp-graphql-woocommerce' ),
113✔
204
                                                ],
113✔
205
                                                'idType' => [
113✔
206
                                                        'type'        => 'OrderIdTypeEnum',
113✔
207
                                                        'description' => __( 'Type of ID being used identify order', 'wp-graphql-woocommerce' ),
113✔
208
                                                ],
113✔
209
                                        ],
113✔
210
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
113✔
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_number':
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
                                        },
113✔
274
                                ],
113✔
275
                                'productVariation' => [
113✔
276
                                        'type'        => 'ProductVariation',
113✔
277
                                        'description' => __( 'A product variation object', 'wp-graphql-woocommerce' ),
113✔
278
                                        'args'        => [
113✔
279
                                                'id'     => [
113✔
280
                                                        'type'        => 'ID',
113✔
281
                                                        'description' => __( 'The ID for identifying the product variation', 'wp-graphql-woocommerce' ),
113✔
282
                                                ],
113✔
283
                                                'idType' => [
113✔
284
                                                        'type'        => 'ProductVariationIdTypeEnum',
113✔
285
                                                        'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
113✔
286
                                                ],
113✔
287
                                        ],
113✔
288
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
113✔
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
                                        },
113✔
320
                                ],
113✔
321
                                'refund'           => [
113✔
322
                                        'type'        => 'Refund',
113✔
323
                                        'description' => __( 'A refund object', 'wp-graphql-woocommerce' ),
113✔
324
                                        'args'        => [
113✔
325
                                                'id'     => [
113✔
326
                                                        'type'        => [ 'non_null' => 'ID' ],
113✔
327
                                                        'description' => __( 'The ID for identifying the refund', 'wp-graphql-woocommerce' ),
113✔
328
                                                ],
113✔
329
                                                'idType' => [
113✔
330
                                                        'type'        => 'RefundIdTypeEnum',
113✔
331
                                                        'description' => __( 'Type of ID being used identify refund', 'wp-graphql-woocommerce' ),
113✔
332
                                                ],
113✔
333
                                        ],
113✔
334
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
113✔
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
                                        },
113✔
401
                                ],
113✔
402
                                'shippingMethod'   => [
113✔
403
                                        'type'        => 'ShippingMethod',
113✔
404
                                        'description' => __( 'A shipping method object', 'wp-graphql-woocommerce' ),
113✔
405
                                        'args'        => [
113✔
406
                                                'id'     => [
113✔
407
                                                        'type'        => 'ID',
113✔
408
                                                        'description' => __( 'The ID for identifying the shipping method', 'wp-graphql-woocommerce' ),
113✔
409
                                                ],
113✔
410
                                                'idType' => [
113✔
411
                                                        'type'        => 'ShippingMethodIdTypeEnum',
113✔
412
                                                        'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
113✔
413
                                                ],
113✔
414
                                        ],
113✔
415
                                        'resolve'     => static function ( $source, array $args ) {
113✔
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
                                        },
113✔
436
                                ],
113✔
437
                                'taxRate'          => [
113✔
438
                                        'type'        => 'TaxRate',
113✔
439
                                        'description' => __( 'A tax rate object', 'wp-graphql-woocommerce' ),
113✔
440
                                        'args'        => [
113✔
441
                                                'id'     => [
113✔
442
                                                        'type'        => 'ID',
113✔
443
                                                        'description' => __( 'The ID for identifying the tax rate', 'wp-graphql-woocommerce' ),
113✔
444
                                                ],
113✔
445
                                                'idType' => [
113✔
446
                                                        'type'        => 'TaxRateIdTypeEnum',
113✔
447
                                                        'description' => __( 'Type of ID being used identify tax rate', 'wp-graphql-woocommerce' ),
113✔
448
                                                ],
113✔
449
                                        ],
113✔
450
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
113✔
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
                                        },
113✔
471
                                ],
113✔
472
                                'countries'        => [
113✔
473
                                        'type'        => [ 'list_of' => 'CountriesEnum' ],
113✔
474
                                        'description' => __( 'Countries', 'wp-graphql-woocommerce' ),
113✔
475
                                        'resolve'     => static function () {
113✔
476
                                                $wc_countries = new \WC_Countries();
1✔
477
                                                $countries    = $wc_countries->get_countries();
1✔
478

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

489
                                                return array_keys( $countries );
1✔
490
                                        },
113✔
491
                                ],
113✔
492
                                'countryStates'    => [
113✔
493
                                        'type'        => [ 'list_of' => 'CountryState' ],
113✔
494
                                        'args'        => [
113✔
495
                                                'country' => [
113✔
496
                                                        'type'        => [ 'non_null' => 'CountriesEnum' ],
113✔
497
                                                        'description' => __( 'Target country', 'wp-graphql-woocommerce' ),
113✔
498
                                                ],
113✔
499
                                        ],
113✔
500
                                        'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
113✔
501
                                        'resolve'     => static function ( $_, $args ) {
113✔
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
                                        },
113✔
517
                                ],
113✔
518
                        ]
113✔
519
                );
113✔
520

521
                // Product queries.
522
                $unsupported_type_enabled = woographql_setting( 'enable_unsupported_product_type', 'off' );
113✔
523

524
                $product_type_keys = array_keys( WooGraphQL::get_enabled_product_types() );
113✔
525
                if ( 'on' === $unsupported_type_enabled ) {
113✔
526
                        $product_type_keys[] = 'unsupported';
1✔
527
                }
528

529
                $product_type_keys = apply_filters( 'woographql_register_product_queries', $product_type_keys );
113✔
530

531
                $product_types = WooGraphQL::get_enabled_product_types();
113✔
532
                if ( 'on' === $unsupported_type_enabled ) {
113✔
533
                        $product_types['unsupported'] = WooGraphQL::get_supported_product_type();
1✔
534
                }
535

536
                foreach ( $product_type_keys as $type_key ) {
113✔
537
                        $field_name = "{$type_key}Product";
113✔
538
                        $type_name  = $product_types[ $type_key ] ?? null;
113✔
539

540
                        if ( empty( $type_name ) ) {
113✔
541
                                continue;
×
542
                        }
543

544
                        register_graphql_field(
113✔
545
                                'RootQuery',
113✔
546
                                $field_name,
113✔
547
                                [
113✔
548
                                        'type'              => $type_name,
113✔
549
                                        /* translators: Product type slug */
550
                                        'description'       => sprintf( __( 'A %s product object', 'wp-graphql-woocommerce' ), $type_key ),
113✔
551
                                        'deprecationReason' => 'Use "product" instead.',
113✔
552
                                        'args'              => [
113✔
553
                                                'id'     => [
113✔
554
                                                        'type'        => 'ID',
113✔
555
                                                        'description' => sprintf(
113✔
556
                                                                /* translators: %s: product type */
557
                                                                __( 'The ID for identifying the %s product', 'wp-graphql-woocommerce' ),
113✔
558
                                                                $type_name
113✔
559
                                                        ),
113✔
560
                                                ],
113✔
561
                                                'idType' => [
113✔
562
                                                        'type'        => 'ProductIdTypeEnum',
113✔
563
                                                        'description' => __( 'Type of ID being used identify product', 'wp-graphql-woocommerce' ),
113✔
564
                                                ],
113✔
565
                                        ],
113✔
566
                                        'resolve'           => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $type_key, $unsupported_type_enabled ) {
113✔
567
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
568
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
569

570
                                                $product_id = null;
1✔
571
                                                switch ( $id_type ) {
572
                                                        case 'sku':
1✔
573
                                                                $product_id = \wc_get_product_id_by_sku( $id );
×
574
                                                                break;
×
575
                                                        case 'slug':
1✔
576
                                                                $post       = get_page_by_path( $id, OBJECT, 'product' );
×
577
                                                                $product_id = ! empty( $post ) ? absint( $post->ID ) : 0;
×
578
                                                                break;
×
579
                                                        case 'database_id':
1✔
580
                                                                $product_id = absint( $id );
×
581
                                                                break;
×
582
                                                        case 'global_id':
1✔
583
                                                        default:
584
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
585
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
586
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
587
                                                                }
588
                                                                $product_id = absint( $id_components['id'] );
1✔
589
                                                                break;
1✔
590
                                                }
591

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

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

602
                                                $product = get_post( $product_id );
1✔
603
                                                if ( ! is_object( $product ) || 'product' !== $product->post_type ) {
1✔
604
                                                        /* translators: %1$s: ID type, %2$s: ID value */
605
                                                        throw new UserError( sprintf( __( 'No product exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $product_id ) );
×
606
                                                }
607

608
                                                return Factory::resolve_crud_object( $product_id, $context );
1✔
609
                                        },
113✔
610
                                ]
113✔
611
                        );
113✔
612
                }//end foreach
613
        }
614
}
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