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

wp-graphql / wp-graphql-woocommerce / 27452430870

13 Jun 2026 01:26AM UTC coverage: 91.8%. Remained the same
27452430870

Pull #1019

github

web-flow
Merge f03617ca3 into 2ce9424e1
Pull Request #1019: fix: address WordPress.org plugin review (rename + prefixing + headers)

1330 of 1587 new or added lines in 201 files covered. (83.81%)

1 existing line in 1 file now uncovered.

18528 of 20183 relevant lines covered (91.8%)

152.68 hits per line

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

93.06
/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(
298✔
30
                        'RootQuery',
298✔
31
                        [
298✔
32
                                'cart'             => [
298✔
33
                                        'type'        => 'Cart',
298✔
34
                                        'args'        => [
298✔
35
                                                'recalculateTotals' => [
298✔
36
                                                        'type'        => 'Boolean',
298✔
37
                                                        'description' => static function () {
298✔
38
                                                                return __( 'Should cart totals be recalculated.', 'graphql-for-ecommerce' );
3✔
39
                                                        },
298✔
40
                                                ],
298✔
41
                                                'fees'              => [
298✔
42
                                                        'type'        => [ 'list_of' => 'FeeInput' ],
298✔
43
                                                        'description' => static function () {
298✔
44
                                                                return __( 'Fees to add to the cart.', 'graphql-for-ecommerce' );
3✔
45
                                                        },
298✔
46
                                                ],
298✔
47
                                        ],
298✔
48
                                        'description' => static function () {
298✔
49
                                                return __( 'The cart object', 'graphql-for-ecommerce' );
3✔
50
                                        },
298✔
51
                                        'resolve'     => static function ( $_, $args ) {
298✔
52
                                                $token_invalid = apply_filters( 'graphql_woocommerce_session_token_errors', null );
22✔
53
                                                if ( $token_invalid ) {
22✔
54
                                                        throw new UserError( $token_invalid );
1✔
55
                                                }
56

57
                                                $cart = Factory::resolve_cart();
21✔
58

59
                                                if ( ! empty( $args['fees'] ) ) {
21✔
60
                                                        $fees = $args['fees'];
1✔
61
                                                        add_action(
1✔
62
                                                                'woocommerce_cart_calculate_fees',
1✔
63
                                                                static function () use ( $fees ) {
1✔
64
                                                                        foreach ( $fees as $fee_input ) {
1✔
65
                                                                                if ( empty( $fee_input['name'] ) || empty( $fee_input['amount'] ) ) {
1✔
66
                                                                                        // TODO: Log invalid fee input.
67
                                                                                        continue;
×
68
                                                                                }
69

70
                                                                                $fee_args = [
1✔
71
                                                                                        $fee_input['name'],
1✔
72
                                                                                        $fee_input['amount'],
1✔
73
                                                                                        isset( $fee_input['taxable'] ) ? $fee_input['taxable'] : false,
1✔
74
                                                                                        isset( $fee_input['taxClass'] ) ? $fee_input['taxClass'] : '',
1✔
75
                                                                                ];
1✔
76

77
                                                                                \WC()->cart->add_fee( ...$fee_args );
1✔
78
                                                                        }
79
                                                                }
1✔
80
                                                        );
1✔
81
                                                }
82

83
                                                if ( ! empty( $args['recalculateTotals'] ) ) {
21✔
84
                                                        $cart->calculate_totals();
×
85
                                                }
86

87
                                                return $cart;
21✔
88
                                        },
298✔
89
                                ],
298✔
90
                                'cartItem'         => [
298✔
91
                                        'type'        => 'CartItem',
298✔
92
                                        'args'        => [
298✔
93
                                                'key' => [
298✔
94
                                                        'type' => [ 'non_null' => 'ID' ],
298✔
95
                                                ],
298✔
96
                                        ],
298✔
97
                                        'description' => static function () {
298✔
98
                                                return __( 'The cart object', 'graphql-for-ecommerce' );
3✔
99
                                        },
298✔
100
                                        'resolve'     => static function ( $source, array $args ) {
298✔
101
                                                $item = Factory::resolve_cart()->get_cart_item( $args['key'] );
1✔
102
                                                if ( empty( $item ) || empty( $item['key'] ) ) {
1✔
NEW
103
                                                        throw new UserError( __( 'Failed to retrieve cart item.', 'graphql-for-ecommerce' ) );
×
104
                                                }
105

106
                                                return $item;
1✔
107
                                        },
298✔
108
                                ],
298✔
109
                                'cartFee'          => [
298✔
110
                                        'type'        => 'CartFee',
298✔
111
                                        'args'        => [
298✔
112
                                                'id' => [
298✔
113
                                                        'type' => [ 'non_null' => 'ID' ],
298✔
114
                                                ],
298✔
115
                                        ],
298✔
116
                                        'description' => static function () {
298✔
117
                                                return __( 'The cart object', 'graphql-for-ecommerce' );
3✔
118
                                        },
298✔
119
                                        'resolve'     => static function ( $source, array $args ) {
298✔
120
                                                $fees   = Factory::resolve_cart()->get_fees();
1✔
121
                                                $fee_id = $args['id'];
1✔
122

123
                                                if ( empty( $fees[ $fee_id ] ) ) {
1✔
NEW
124
                                                        throw new UserError( __( 'The ID input is invalid', 'graphql-for-ecommerce' ) );
×
125
                                                }
126

127
                                                return $fees[ $fee_id ];
1✔
128
                                        },
298✔
129
                                ],
298✔
130
                                'coupon'           => [
298✔
131
                                        'type'        => 'Coupon',
298✔
132
                                        'description' => static function () {
298✔
133
                                                return __( 'A coupon object', 'graphql-for-ecommerce' );
3✔
134
                                        },
298✔
135
                                        'args'        => [
298✔
136
                                                'id'     => [ 'type' => [ 'non_null' => 'ID' ] ],
298✔
137
                                                'idType' => [
298✔
138
                                                        'type'        => 'CouponIdTypeEnum',
298✔
139
                                                        'description' => static function () {
298✔
140
                                                                return __( 'Type of ID being used identify coupon', 'graphql-for-ecommerce' );
3✔
141
                                                        },
298✔
142
                                                ],
298✔
143
                                        ],
298✔
144
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
298✔
145
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
4✔
146
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
4✔
147

148
                                                $coupon_id = null;
4✔
149
                                                switch ( $id_type ) {
150
                                                        case 'code':
4✔
151
                                                                $coupon_id = \wc_get_coupon_id_by_code( $id );
1✔
152
                                                                break;
1✔
153
                                                        case 'database_id':
4✔
154
                                                                $coupon_id = absint( $id );
1✔
155
                                                                break;
1✔
156
                                                        case 'global_id':
4✔
157
                                                        default:
158
                                                                $id_components = Relay::fromGlobalId( $args['id'] );
4✔
159
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
4✔
NEW
160
                                                                        throw new UserError( __( 'The "id" is invalid', 'graphql-for-ecommerce' ) );
×
161
                                                                }
162
                                                                $coupon_id = absint( $id_components['id'] );
4✔
163
                                                                break;
4✔
164
                                                }
165

166
                                                // Check if user authorized to view coupon.
167
                                                /**
168
                                                 * Get coupon post type.
169
                                                 *
170
                                                 * @var \WP_Post_Type $post_type
171
                                                 */
172
                                                $post_type     = get_post_type_object( 'shop_coupon' );
4✔
173
                                                $is_authorized = current_user_can( $post_type->cap->edit_others_posts );
4✔
174
                                                if ( ! $is_authorized ) {
4✔
175
                                                        return null;
1✔
176
                                                }
177

178
                                                if ( empty( $coupon_id ) ) {
4✔
179
                                                        /* translators: %1$s: ID type, %2$s: ID value */
NEW
180
                                                        throw new UserError( sprintf( __( 'No coupon ID was found corresponding to the %1$s: %2$s', 'graphql-for-ecommerce' ), $id_type, $id ) );
×
181
                                                }
182

183
                                                $coupon = get_post( $coupon_id );
4✔
184
                                                if ( ! is_object( $coupon ) || 'shop_coupon' !== $coupon->post_type ) {
4✔
185
                                                        /* translators: %1$s: ID type, %2$s: ID value */
NEW
186
                                                        throw new UserError( sprintf( __( 'No coupon exists with the %1$s: %2$s', 'graphql-for-ecommerce' ), $id_type, $id ) );
×
187
                                                }
188

189
                                                return Factory::resolve_crud_object( $coupon_id, $context );
4✔
190
                                        },
298✔
191
                                ],
298✔
192
                                'customer'         => [
298✔
193
                                        'type'        => 'Customer',
298✔
194
                                        'description' => static function () {
298✔
195
                                                return __( 'A customer object', 'graphql-for-ecommerce' );
3✔
196
                                        },
298✔
197
                                        'args'        => [
298✔
198
                                                'id'         => [
298✔
199
                                                        'type'        => 'ID',
298✔
200
                                                        'description' => static function () {
298✔
201
                                                                return __( 'Get the customer by their global ID', 'graphql-for-ecommerce' );
3✔
202
                                                        },
298✔
203
                                                ],
298✔
204
                                                'customerId' => [
298✔
205
                                                        'type'        => 'Int',
298✔
206
                                                        'description' => static function () {
298✔
207
                                                                return __( 'Get the customer by their database ID', 'graphql-for-ecommerce' );
3✔
208
                                                        },
298✔
209
                                                ],
298✔
210
                                        ],
298✔
211
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
298✔
212
                                                $current_user_id = get_current_user_id();
22✔
213

214
                                                // Default the customer to the current user.
215
                                                $customer_id = $current_user_id;
22✔
216

217
                                                // If a customer ID has been provided, resolve to that ID instead.
218
                                                if ( ! empty( $args['id'] ) ) {
22✔
219
                                                        $id_components = Relay::fromGlobalId( $args['id'] );
3✔
220
                                                        if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
3✔
NEW
221
                                                                throw new UserError( __( 'The ID input is invalid', 'graphql-for-ecommerce' ) );
×
222
                                                        }
223

224
                                                        $customer_id = absint( $id_components['id'] );
3✔
225
                                                } elseif ( ! empty( $args['customerId'] ) ) {
22✔
226
                                                        $customer_id = absint( $args['customerId'] );
1✔
227
                                                }
228

229
                                                // If a user does not have the ability to list users, they can only view their own customer object.
230
                                                $unauthorized = ! empty( $customer_id )
22✔
231
                                                        && ! current_user_can( 'list_users' )
22✔
232
                                                        && $current_user_id !== $customer_id;
22✔
233
                                                if ( $unauthorized ) {
22✔
234
                                                        throw new UserError( __( 'Not authorized to access this customer', 'graphql-for-ecommerce' ) );
2✔
235
                                                }
236

237
                                                // If we have a customer ID, resolve to that customer.
238
                                                if ( $customer_id ) {
22✔
239
                                                        return Factory::resolve_customer( $customer_id, $context );
19✔
240
                                                }
241

242
                                                // Resolve to the session customer.
243
                                                return Factory::resolve_session_customer();
4✔
244
                                        },
298✔
245
                                ],
298✔
246
                                'order'            => [
298✔
247
                                        'type'        => 'Order',
298✔
248
                                        'description' => static function () {
298✔
249
                                                return __( 'A order object', 'graphql-for-ecommerce' );
3✔
250
                                        },
298✔
251
                                        'args'        => [
298✔
252
                                                'id'     => [
298✔
253
                                                        'type'        => 'ID',
298✔
254
                                                        'description' => static function () {
298✔
255
                                                                return __( 'The ID for identifying the order', 'graphql-for-ecommerce' );
3✔
256
                                                        },
298✔
257
                                                ],
298✔
258
                                                'idType' => [
298✔
259
                                                        'type'        => 'OrderIdTypeEnum',
298✔
260
                                                        'description' => static function () {
298✔
261
                                                                return __( 'Type of ID being used identify order', 'graphql-for-ecommerce' );
3✔
262
                                                        },
298✔
263
                                                ],
298✔
264
                                        ],
298✔
265
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
298✔
266
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
12✔
267
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
12✔
268

269
                                                $order_id = null;
12✔
270
                                                switch ( $id_type ) {
271
                                                        case 'order_key':
12✔
272
                                                                $order_id = \wc_get_order_id_by_order_key( $id );
1✔
273
                                                                break;
1✔
274
                                                        case 'database_id':
12✔
275
                                                                $order_id = absint( $id );
2✔
276
                                                                break;
2✔
277
                                                        case 'global_id':
11✔
278
                                                        default:
279
                                                                $id_components = Relay::fromGlobalId( $id );
11✔
280
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
11✔
NEW
281
                                                                        throw new UserError( __( 'The "id" is invalid', 'graphql-for-ecommerce' ) );
×
282
                                                                }
283
                                                                $order_id = absint( $id_components['id'] );
11✔
284
                                                                break;
11✔
285
                                                }
286

287
                                                if ( empty( $order_id ) ) {
12✔
288
                                                        /* translators: %1$s: ID type, %2$s: ID value */
NEW
289
                                                        throw new UserError( sprintf( __( 'No order ID was found corresponding to the %1$s: %2$s', 'graphql-for-ecommerce' ), $id_type, $id ) );
×
290
                                                }
291

292
                                                if ( 'shop_order' !== OrderUtil::get_order_type( $order_id ) ) {
12✔
293
                                                        /* translators: %1$s: ID type, %2$s: ID value */
NEW
294
                                                        throw new UserError( sprintf( __( 'No order exists with the %1$s: %2$s', 'graphql-for-ecommerce' ), $id_type, $id ) );
×
295
                                                }
296

297
                                                // Check if user authorized to view order.
298
                                                /**
299
                                                 * Get order post type.
300
                                                 *
301
                                                 * @var \WP_Post_Type $post_type
302
                                                 */
303
                                                $post_type     = get_post_type_object( 'shop_order' );
12✔
304
                                                $is_authorized = current_user_can( $post_type->cap->edit_others_posts );
12✔
305
                                                if ( ! $is_authorized && get_current_user_id() ) {
12✔
306
                                                        /** @var \WC_Order[] $orders */
307
                                                        $orders = wc_get_orders(
3✔
308
                                                                [
3✔
309
                                                                        'type'          => 'shop_order',
3✔
310
                                                                        'post__in'      => [ $order_id ],
3✔
311
                                                                        'customer_id'   => get_current_user_id(),
3✔
312
                                                                        'no_rows_found' => true,
3✔
313
                                                                        'return'        => 'ids',
3✔
314
                                                                ]
3✔
315
                                                        );
3✔
316

317
                                                        if ( in_array( $order_id, $orders, true ) ) {
3✔
318
                                                                $is_authorized = true;
2✔
319
                                                        }
320
                                                }
321

322
                                                // Throw if authorized to view order.
323
                                                if ( ! $is_authorized ) {
12✔
324
                                                        throw new UserError( __( 'Not authorized to access this order', 'graphql-for-ecommerce' ) );
1✔
325
                                                }
326

327
                                                return Factory::resolve_crud_object( $order_id, $context );
12✔
328
                                        },
298✔
329
                                ],
298✔
330
                                'productVariation' => [
298✔
331
                                        'type'        => 'ProductVariation',
298✔
332
                                        'description' => static function () {
298✔
333
                                                return __( 'A product variation object', 'graphql-for-ecommerce' );
3✔
334
                                        },
298✔
335
                                        'args'        => [
298✔
336
                                                'id'     => [
298✔
337
                                                        'type'        => 'ID',
298✔
338
                                                        'description' => static function () {
298✔
339
                                                                return __( 'The ID for identifying the product variation', 'graphql-for-ecommerce' );
3✔
340
                                                        },
298✔
341
                                                ],
298✔
342
                                                'idType' => [
298✔
343
                                                        'type'        => 'ProductVariationIdTypeEnum',
298✔
344
                                                        'description' => static function () {
298✔
345
                                                                return __( 'Type of ID being used identify product variation', 'graphql-for-ecommerce' );
3✔
346
                                                        },
298✔
347
                                                ],
298✔
348
                                        ],
298✔
349
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
298✔
350
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
9✔
351
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
9✔
352

353
                                                $variation_id = null;
9✔
354
                                                switch ( $id_type ) {
355
                                                        case 'database_id':
9✔
356
                                                                $variation_id = absint( $id );
2✔
357
                                                                break;
2✔
358
                                                        case 'global_id':
8✔
359
                                                        default:
360
                                                                $id_components = Relay::fromGlobalId( $id );
8✔
361
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
8✔
NEW
362
                                                                        throw new UserError( __( 'The "id" is invalid', 'graphql-for-ecommerce' ) );
×
363
                                                                }
364
                                                                $variation_id = absint( $id_components['id'] );
8✔
365
                                                                break;
8✔
366
                                                }
367

368
                                                if ( empty( $variation_id ) ) {
9✔
369
                                                        /* translators: %1$s: ID type, %2$s: ID value */
NEW
370
                                                        throw new UserError( sprintf( __( 'No product variation ID was found corresponding to the %1$s: %2$s', 'graphql-for-ecommerce' ), $id_type, $id ) );
×
371
                                                }
372

373
                                                $variation = get_post( $variation_id );
9✔
374
                                                if ( ! is_object( $variation ) || 'product_variation' !== $variation->post_type ) {
9✔
375
                                                        /* translators: %1$s: ID type, %2$s: ID value */
NEW
376
                                                        throw new UserError( sprintf( __( 'No product variation exists with the %1$s: %2$s', 'graphql-for-ecommerce' ), $id_type, $id ) );
×
377
                                                }
378

379
                                                return Factory::resolve_crud_object( $variation_id, $context );
9✔
380
                                        },
298✔
381
                                ],
298✔
382
                                'refund'           => [
298✔
383
                                        'type'        => 'Refund',
298✔
384
                                        'description' => static function () {
298✔
385
                                                return __( 'A refund object', 'graphql-for-ecommerce' );
3✔
386
                                        },
298✔
387
                                        'args'        => [
298✔
388
                                                'id'     => [
298✔
389
                                                        'type'        => [ 'non_null' => 'ID' ],
298✔
390
                                                        'description' => static function () {
298✔
391
                                                                return __( 'The ID for identifying the refund', 'graphql-for-ecommerce' );
3✔
392
                                                        },
298✔
393
                                                ],
298✔
394
                                                'idType' => [
298✔
395
                                                        'type'        => 'RefundIdTypeEnum',
298✔
396
                                                        'description' => static function () {
298✔
397
                                                                return __( 'Type of ID being used identify refund', 'graphql-for-ecommerce' );
3✔
398
                                                        },
298✔
399
                                                ],
298✔
400
                                        ],
298✔
401
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
298✔
402
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
3✔
403
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
3✔
404

405
                                                $refund_id = null;
3✔
406
                                                switch ( $id_type ) {
407
                                                        case 'database_id':
3✔
408
                                                                $refund_id = absint( $id );
1✔
409
                                                                break;
1✔
410
                                                        case 'global_id':
3✔
411
                                                        default:
412
                                                                $id_components = Relay::fromGlobalId( $id );
3✔
413
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
3✔
NEW
414
                                                                        throw new UserError( __( 'The "id" is invalid', 'graphql-for-ecommerce' ) );
×
415
                                                                }
416
                                                                $refund_id = absint( $id_components['id'] );
3✔
417
                                                                break;
3✔
418
                                                }
419

420
                                                if ( empty( $refund_id ) ) {
3✔
421
                                                        /* translators: %1$s: ID type, %2$s: ID value */
NEW
422
                                                        throw new UserError( sprintf( __( 'No refund ID was found corresponding to the %1$s: %2$s', 'graphql-for-ecommerce' ), $id_type, $id ) );
×
423
                                                }
424

425
                                                if ( 'shop_order_refund' !== OrderUtil::get_order_type( $refund_id ) ) {
3✔
426
                                                        /* translators: %1$s: ID type, %2$s: ID value */
NEW
427
                                                        throw new UserError( sprintf( __( 'No refund exists with the %1$s: %2$s', 'graphql-for-ecommerce' ), $id_type, $id ) );
×
428
                                                }
429

430
                                                // Check if user authorized to view order.
431
                                                /**
432
                                                 * Get refund post type.
433
                                                 *
434
                                                 * @var \WP_Post_Type $post_type
435
                                                 */
436
                                                $post_type     = get_post_type_object( 'shop_order_refund' );
3✔
437
                                                $is_authorized = current_user_can( $post_type->cap->edit_others_posts );
3✔
438
                                                if ( get_current_user_id() ) {
3✔
439
                                                        $refund = \wc_get_order( $refund_id );
3✔
440
                                                        if ( ! is_object( $refund ) || ! is_a( $refund, \WC_Order_Refund::class ) ) {
3✔
NEW
441
                                                                throw new UserError( __( 'Failed to retrieve refund', 'graphql-for-ecommerce' ) );
×
442
                                                        }
443
                                                        $order_id = $refund->get_parent_id();
3✔
444

445
                                                        /** @var \WC_Order[] $orders */
446
                                                        $orders = wc_get_orders(
3✔
447
                                                                [
3✔
448
                                                                        'type'          => 'shop_order',
3✔
449
                                                                        'post__in'      => [ $order_id ],
3✔
450
                                                                        'customer_id'   => get_current_user_id(),
3✔
451
                                                                        'no_rows_found' => true,
3✔
452
                                                                        'return'        => 'ids',
3✔
453
                                                                ]
3✔
454
                                                        );
3✔
455

456
                                                        if ( in_array( $order_id, $orders, true ) ) {
3✔
457
                                                                $is_authorized = true;
3✔
458
                                                        }
459
                                                }//end if
460

461
                                                // Throw if authorized to view refund.
462
                                                if ( ! $is_authorized ) {
3✔
463
                                                        throw new UserError( __( 'Not authorized to access this refund', 'graphql-for-ecommerce' ) );
1✔
464
                                                }
465

466
                                                return Factory::resolve_crud_object( $refund_id, $context );
3✔
467
                                        },
298✔
468
                                ],
298✔
469
                                'shippingMethod'   => [
298✔
470
                                        'type'        => 'ShippingMethod',
298✔
471
                                        'description' => static function () {
298✔
472
                                                return __( 'A shipping method object', 'graphql-for-ecommerce' );
3✔
473
                                        },
298✔
474
                                        'args'        => [
298✔
475
                                                'id'     => [
298✔
476
                                                        'type'        => 'ID',
298✔
477
                                                        'description' => static function () {
298✔
478
                                                                return __( 'The ID for identifying the shipping method', 'graphql-for-ecommerce' );
3✔
479
                                                        },
298✔
480
                                                ],
298✔
481
                                                'idType' => [
298✔
482
                                                        'type'        => 'ShippingMethodIdTypeEnum',
298✔
483
                                                        'description' => static function () {
298✔
484
                                                                return __( 'Type of ID being used identify product variation', 'graphql-for-ecommerce' );
3✔
485
                                                        },
298✔
486
                                                ],
298✔
487
                                        ],
298✔
488
                                        'resolve'     => static function ( $source, array $args ) {
298✔
489
                                                if ( ! \wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) {
1✔
490
                                                        throw new UserError( __( 'Sorry, you cannot view shipping methods.', 'graphql-for-ecommerce' ), \rest_authorization_required_code() );
1✔
491
                                                }
492

493
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
494
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
495

496
                                                $method_id = null;
1✔
497
                                                switch ( $id_type ) {
498
                                                        case 'database_id':
1✔
499
                                                                $method_id = $id;
1✔
500
                                                                break;
1✔
501
                                                        case 'global_id':
1✔
502
                                                        default:
503
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
504
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
NEW
505
                                                                        throw new UserError( __( 'The "id" is invalid', 'graphql-for-ecommerce' ) );
×
506
                                                                }
507
                                                                $method_id = $id_components['id'];
1✔
508
                                                                break;
1✔
509
                                                }
510

511
                                                return Factory::resolve_shipping_method( $method_id );
1✔
512
                                        },
298✔
513
                                ],
298✔
514
                                'shippingZone'     => [
298✔
515
                                        'type'        => 'ShippingZone',
298✔
516
                                        'description' => static function () {
298✔
517
                                                return __( 'A shipping zone object', 'graphql-for-ecommerce' );
3✔
518
                                        },
298✔
519
                                        'args'        => [
298✔
520
                                                'id'     => [
298✔
521
                                                        'type'        => 'ID',
298✔
522
                                                        'description' => static function () {
298✔
523
                                                                return __( 'The ID for identifying the shipping zone', 'graphql-for-ecommerce' );
3✔
524
                                                        },
298✔
525
                                                ],
298✔
526
                                                'idType' => [
298✔
527
                                                        'type'        => 'ShippingZoneIdTypeEnum',
298✔
528
                                                        'description' => static function () {
298✔
529
                                                                return __( 'Type of ID being used identify shipping zone', 'graphql-for-ecommerce' );
3✔
530
                                                        },
298✔
531
                                                ],
298✔
532
                                        ],
298✔
533
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
298✔
534
                                                if ( ! \wc_shipping_enabled() ) {
1✔
NEW
535
                                                        throw new UserError( __( 'Shipping is disabled.', 'graphql-for-ecommerce' ), 404 );
×
536
                                                }
537

538
                                                if ( ! \wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
1✔
539
                                                        throw new UserError( __( 'Permission denied.', 'graphql-for-ecommerce' ), \rest_authorization_required_code() );
1✔
540
                                                }
541

542
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
543
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
544

545
                                                $zone_id = null;
1✔
546
                                                switch ( $id_type ) {
547
                                                        case 'database_id':
1✔
548
                                                                $zone_id = $id;
×
549
                                                                break;
×
550
                                                        case 'global_id':
1✔
551
                                                        default:
552
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
553
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
NEW
554
                                                                        throw new UserError( __( 'The "id" is invalid', 'graphql-for-ecommerce' ) );
×
555
                                                                }
556
                                                                $zone_id = $id_components['id'];
1✔
557
                                                                break;
1✔
558
                                                }
559

560
                                                return $context->get_loader( 'shipping_zone' )->load( $zone_id );
1✔
561
                                        },
298✔
562
                                ],
298✔
563
                                'taxRate'          => [
298✔
564
                                        'type'        => 'TaxRate',
298✔
565
                                        'description' => static function () {
298✔
566
                                                return __( 'A tax rate object', 'graphql-for-ecommerce' );
3✔
567
                                        },
298✔
568
                                        'args'        => [
298✔
569
                                                'id'     => [
298✔
570
                                                        'type'        => 'ID',
298✔
571
                                                        'description' => static function () {
298✔
572
                                                                return __( 'The ID for identifying the tax rate', 'graphql-for-ecommerce' );
3✔
573
                                                        },
298✔
574
                                                ],
298✔
575
                                                'idType' => [
298✔
576
                                                        'type'        => 'TaxRateIdTypeEnum',
298✔
577
                                                        'description' => static function () {
298✔
578
                                                                return __( 'Type of ID being used identify tax rate', 'graphql-for-ecommerce' );
3✔
579
                                                        },
298✔
580
                                                ],
298✔
581
                                        ],
298✔
582
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
298✔
583
                                                if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
1✔
584
                                                        throw new UserError( __( 'Sorry, you cannot view tax rates.', 'graphql-for-ecommerce' ), \rest_authorization_required_code() );
1✔
585
                                                }
586
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
587
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
588

589
                                                $rate_id = null;
1✔
590
                                                switch ( $id_type ) {
591
                                                        case 'database_id':
1✔
592
                                                                $rate_id = absint( $id );
1✔
593
                                                                break;
1✔
594
                                                        case 'global_id':
1✔
595
                                                        default:
596
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
597
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
NEW
598
                                                                        throw new UserError( __( 'The "id" is invalid', 'graphql-for-ecommerce' ) );
×
599
                                                                }
600
                                                                $rate_id = absint( $id_components['id'] );
1✔
601
                                                                break;
1✔
602
                                                }
603

604
                                                return Factory::resolve_tax_rate( $rate_id, $context );
1✔
605
                                        },
298✔
606
                                ],
298✔
607
                                'countries'        => [
298✔
608
                                        'type'        => [ 'list_of' => 'CountriesEnum' ],
298✔
609
                                        'description' => static function () {
298✔
610
                                                return __( 'Countries', 'graphql-for-ecommerce' );
3✔
611
                                        },
298✔
612
                                        'resolve'     => static function () {
298✔
613
                                                $wc_countries = new \WC_Countries();
1✔
614
                                                $countries    = $wc_countries->get_countries();
1✔
615

616
                                                return array_keys( $countries );
1✔
617
                                        },
298✔
618
                                ],
298✔
619
                                'allowedCountries' => [
298✔
620
                                        'type'        => [ 'list_of' => 'CountriesEnum' ],
298✔
621
                                        'description' => static function () {
298✔
622
                                                return __( 'Countries that the store sells to', 'graphql-for-ecommerce' );
3✔
623
                                        },
298✔
624
                                        'resolve'     => static function () {
298✔
625
                                                $wc_countries = new \WC_Countries();
1✔
626
                                                $countries    = $wc_countries->get_allowed_countries();
1✔
627

628
                                                return array_keys( $countries );
1✔
629
                                        },
298✔
630
                                ],
298✔
631
                                'countryStates'    => [
298✔
632
                                        'type'        => [ 'list_of' => 'CountryState' ],
298✔
633
                                        'args'        => [
298✔
634
                                                'country' => [
298✔
635
                                                        'type'        => [ 'non_null' => 'CountriesEnum' ],
298✔
636
                                                        'description' => static function () {
298✔
637
                                                                return __( 'Target country', 'graphql-for-ecommerce' );
3✔
638
                                                        },
298✔
639
                                                ],
298✔
640
                                        ],
298✔
641
                                        'description' => static function () {
298✔
642
                                                return __( 'Countries that the store sells to', 'graphql-for-ecommerce' );
3✔
643
                                        },
298✔
644
                                        'resolve'     => static function ( $_, $args ) {
298✔
645
                                                $country      = $args['country'];
1✔
646
                                                $wc_countries = new \WC_Countries();
1✔
647
                                                $states       = $wc_countries->get_shipping_country_states();
1✔
648

649
                                                if ( ! empty( $states ) && ! empty( $states[ $country ] ) ) {
1✔
650
                                                        $formatted_states = [];
1✔
651
                                                        foreach ( $states[ $country ] as $code => $name ) {
1✔
652
                                                                $formatted_states[] = compact( 'name', 'code' );
1✔
653
                                                        }
654

655
                                                        return $formatted_states;
1✔
656
                                                }
657

658
                                                return [];
×
659
                                        },
298✔
660
                                ],
298✔
661
                                'wcSettingGroups'  => [
298✔
662
                                        'type'        => [ 'list_of' => 'WCSettingGroup' ],
298✔
663
                                        'description' => static function () {
298✔
664
                                                return __( 'WooCommerce setting groups', 'graphql-for-ecommerce' );
3✔
665
                                        },
298✔
666
                                        'resolve'     => static function () {
298✔
667
                                                if ( ! \wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
3✔
668
                                                        throw new UserError( __( 'Sorry, you cannot view settings.', 'graphql-for-ecommerce' ) );
1✔
669
                                                }
670

671
                                                $groups = apply_filters( 'woocommerce_settings_groups', [] ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
2✔
672
                                                return array_values( $groups );
2✔
673
                                        },
298✔
674
                                ],
298✔
675
                                'wcSettings'       => [
298✔
676
                                        'type'        => [ 'list_of' => 'WCSetting' ],
298✔
677
                                        'description' => static function () {
298✔
678
                                                return __( 'WooCommerce settings for a specific group', 'graphql-for-ecommerce' );
3✔
679
                                        },
298✔
680
                                        'args'        => [
298✔
681
                                                'group' => [
298✔
682
                                                        'type'        => [ 'non_null' => 'String' ],
298✔
683
                                                        'description' => static function () {
298✔
684
                                                                return __( 'Settings group ID', 'graphql-for-ecommerce' );
3✔
685
                                                        },
298✔
686
                                                ],
298✔
687
                                        ],
298✔
688
                                        'resolve'     => static function ( $_, $args ) {
298✔
689
                                                if ( ! \wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
3✔
NEW
690
                                                        throw new UserError( __( 'Sorry, you cannot view settings.', 'graphql-for-ecommerce' ) );
×
691
                                                }
692

693
                                                $controller = new \WC_REST_Setting_Options_Controller();
3✔
694
                                                $settings   = $controller->get_group_settings( $args['group'] );
3✔
695

696
                                                if ( is_wp_error( $settings ) ) {
3✔
697
                                                        throw new UserError( $settings->get_error_message() );
1✔
698
                                                }
699

700
                                                return $settings;
2✔
701
                                        },
298✔
702
                                ],
298✔
703
                                'collectionStats'  => [
298✔
704
                                        'type'        => 'CollectionStats',
298✔
705
                                        'args'        => [
298✔
706
                                                'calculatePriceRange'        => [
298✔
707
                                                        'type'        => 'Boolean',
298✔
708
                                                        'description' => static function () {
298✔
709
                                                                return __( 'If true, calculates the minimum and maximum product prices for the collection.', 'graphql-for-ecommerce' );
3✔
710
                                                        },
298✔
711
                                                ],
298✔
712
                                                'calculateRatingCounts'      => [
298✔
713
                                                        'type'        => 'Boolean',
298✔
714
                                                        'description' => static function () {
298✔
715
                                                                return __( 'If true, calculates rating counts for products in the collection.', 'graphql-for-ecommerce' );
3✔
716
                                                        },
298✔
717
                                                ],
298✔
718
                                                'calculateStockStatusCounts' => [
298✔
719
                                                        'type'        => 'Boolean',
298✔
720
                                                        'description' => static function () {
298✔
721
                                                                return __( 'If true, calculates stock counts for products in the collection.', 'graphql-for-ecommerce' );
3✔
722
                                                        },
298✔
723
                                                ],
298✔
724
                                                'taxonomies'                 => [
298✔
725
                                                        'type' => [ 'list_of' => 'CollectionStatsQueryInput' ],
298✔
726
                                                ],
298✔
727
                                                'where'                      => [
298✔
728
                                                        'type' => 'CollectionStatsWhereArgs',
298✔
729
                                                ],
298✔
730
                                        ],
298✔
731
                                        'description' => static function () {
298✔
732
                                                return __( 'Statistics for a product taxonomy query', 'graphql-for-ecommerce' );
3✔
733
                                        },
298✔
734
                                        'resolve'     => static function ( $_, $args ) {
298✔
735
                                                /** @var array<string, mixed> $data */
736
                                                $data    = [
4✔
737
                                                        'min_price'           => null,
4✔
738
                                                        'max_price'           => null,
4✔
739
                                                        'attribute_counts'    => null,
4✔
740
                                                        'stock_status_counts' => null,
4✔
741
                                                        'rating_counts'       => null,
4✔
742
                                                ];
4✔
743
                                                $filters = new ProductQueryFilters();
4✔
744

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

748
                                                // Format taxonomies.
749
                                                if ( ! empty( $args['taxonomies'] ) ) {
4✔
750
                                                        $calculate_attribute_counts = [];
4✔
751
                                                        foreach ( $args['taxonomies'] as $attribute_to_count ) {
4✔
752
                                                                $attribute = [ 'taxonomy' => $attribute_to_count['taxonomy'] ];
4✔
753
                                                                // Set the query type.
754
                                                                if ( ! empty( $attribute_to_count['relation'] ) ) {
4✔
755
                                                                        $attribute['query_type'] = strtolower( $attribute_to_count['relation'] );
4✔
756
                                                                }
757

758
                                                                // Add the attribute to the list of attributes to count.
759
                                                                $calculate_attribute_counts[] = $attribute;
4✔
760
                                                        }
761

762
                                                        // Set the attribute counts to calculate.
763
                                                        $request->set_param( 'calculate_attribute_counts', $calculate_attribute_counts );
4✔
764
                                                }
765

766
                                                $request->set_param( 'calculate_price_range', $args['calculatePriceRange'] ?? false );
4✔
767
                                                $request->set_param( 'calculate_stock_status_counts', $args['calculateStockStatusCounts'] ?? false );
4✔
768
                                                $request->set_param( 'calculate_rating_counts', $args['calculateRatingCounts'] ?? false );
4✔
769

770
                                                if ( ! empty( $request['calculate_price_range'] ) ) {
4✔
771
                                                        /**
772
                                                         * A Rest request object for external filtering
773
                                                         *
774
                                                         * @var \WP_REST_Request $filter_request
775
                                                         */
776
                                                        $filter_request = clone $request;
1✔
777
                                                        $filter_request->set_param( 'min_price', null );
1✔
778
                                                        $filter_request->set_param( 'max_price', null );
1✔
779

780
                                                        /** @var object{min_price: float, max_price: float} */
781
                                                        $price_results     = $filters->get_filtered_price( $filter_request );
1✔
782
                                                        $data['min_price'] = $price_results->min_price;
1✔
783
                                                        $data['max_price'] = $price_results->max_price;
1✔
784
                                                }
785

786
                                                if ( ! empty( $request['calculate_stock_status_counts'] ) ) {
4✔
787
                                                        /**
788
                                                         * A Rest request object for external filtering
789
                                                         *
790
                                                         * @var \WP_REST_Request $filter_request
791
                                                         */
792
                                                        $filter_request = clone $request;
1✔
793
                                                        $counts         = $filters->get_stock_status_counts( $filter_request );
1✔
794

795
                                                        $data['stock_status_counts'] = [];
1✔
796

797
                                                        foreach ( $counts as $key => $value ) {
1✔
798
                                                                $data['stock_status_counts'][] = (object) [
1✔
799
                                                                        'status' => $key,
1✔
800
                                                                        'count'  => $value,
1✔
801
                                                                ];
1✔
802
                                                        }
803
                                                }
804

805
                                                if ( ! empty( $request['calculate_attribute_counts'] ) ) {
4✔
806
                                                        $taxonomy__or_queries  = [];
4✔
807
                                                        $taxonomy__and_queries = [];
4✔
808
                                                        foreach ( $request['calculate_attribute_counts'] as $attributes_to_count ) {
4✔
809
                                                                if ( ! isset( $attributes_to_count['taxonomy'] ) ) {
4✔
810
                                                                        continue;
×
811
                                                                }
812

813
                                                                if ( empty( $attributes_to_count['query_type'] ) || 'or' === $attributes_to_count['query_type'] ) {
4✔
814
                                                                        $taxonomy__or_queries[] = $attributes_to_count['taxonomy'];
3✔
815
                                                                } else {
816
                                                                        $taxonomy__and_queries[] = $attributes_to_count['taxonomy'];
3✔
817
                                                                }
818
                                                        }
819

820
                                                        $data['attribute_counts'] = [];
4✔
821
                                                        if ( ! empty( $taxonomy__or_queries ) ) {
4✔
822
                                                                foreach ( $taxonomy__or_queries as $taxonomy ) {
3✔
823
                                                                        /**
824
                                                                         * A Rest request object for external filtering
825
                                                                         *
826
                                                                         * @var \WP_REST_Request $filter_request
827
                                                                         */
828
                                                                        $filter_request    = clone $request;
3✔
829
                                                                        $filter_attributes = $filter_request->get_param( 'attributes' );
3✔
830

831
                                                                        if ( ! empty( $filter_attributes ) ) {
3✔
832
                                                                                $filter_attributes = array_filter(
×
833
                                                                                        $filter_attributes,
×
834
                                                                                        static function ( $query ) use ( $taxonomy ) {
×
835
                                                                                                return $query['attribute'] !== $taxonomy;
×
836
                                                                                        }
×
837
                                                                                );
×
838
                                                                        }
839

840
                                                                        $filter_request->set_param( 'attributes', $filter_attributes );
3✔
841
                                                                        $counts = $filters->get_attribute_counts( $filter_request, [ $taxonomy ] );
3✔
842

843
                                                                        $data['attribute_counts'][ $taxonomy ] = [];
3✔
844
                                                                        foreach ( $counts as $key => $value ) {
3✔
845
                                                                                $data['attribute_counts'][ $taxonomy ][] = (object) [
3✔
846
                                                                                        'taxonomy' => $taxonomy,
3✔
847
                                                                                        'termId'   => $key,
3✔
848
                                                                                        'count'    => $value,
3✔
849
                                                                                ];
3✔
850
                                                                        }
851
                                                                }
852
                                                        }
853

854
                                                        if ( ! empty( $taxonomy__and_queries ) ) {
4✔
855
                                                                $counts = $filters->get_attribute_counts( $request, $taxonomy__and_queries );
3✔
856

857
                                                                foreach ( $taxonomy__and_queries as $taxonomy ) {
3✔
858
                                                                        $data['attribute_counts'][ $taxonomy ] = [];
3✔
859
                                                                        foreach ( $counts as $key => $value ) {
3✔
860
                                                                                $data['attribute_counts'][ $taxonomy ][] = (object) [
3✔
861
                                                                                        'taxonomy' => $taxonomy,
3✔
862
                                                                                        'termId'   => $key,
3✔
863
                                                                                        'count'    => $value,
3✔
864
                                                                                ];
3✔
865
                                                                        }
866
                                                                }
867
                                                        }
868
                                                }
869

870
                                                if ( ! empty( $request['calculate_rating_counts'] ) ) {
4✔
871
                                                        /**
872
                                                         * A Rest request object for external filtering
873
                                                         *
874
                                                         * @var \WP_REST_Request $filter_request
875
                                                         */
876
                                                        $filter_request        = clone $request;
4✔
877
                                                        $counts                = $filters->get_rating_counts( $filter_request );
4✔
878
                                                        $data['rating_counts'] = [];
4✔
879

880
                                                        foreach ( $counts as $key => $value ) {
4✔
881
                                                                $data['rating_counts'][] = (object) [
×
882
                                                                        'rating' => $key,
×
883
                                                                        'count'  => $value,
×
884
                                                                ];
×
885
                                                        }
886
                                                }
887

888
                                                return $data;
4✔
889
                                        },
298✔
890
                                ],
298✔
891
                        ]
298✔
892
                );
298✔
893

894
                // Product queries.
895
                $unsupported_type_enabled = woographql_setting( 'enable_unsupported_product_type', 'off' );
298✔
896

897
                $product_type_keys = array_keys( WooGraphQL::get_enabled_product_types() );
298✔
898
                if ( 'on' === $unsupported_type_enabled ) {
298✔
899
                        $product_type_keys[] = 'unsupported';
1✔
900
                }
901

902
                $product_type_keys = apply_filters( 'woographql_register_product_queries', $product_type_keys );
298✔
903

904
                $product_types = WooGraphQL::get_enabled_product_types();
298✔
905
                if ( 'on' === $unsupported_type_enabled ) {
298✔
906
                        $product_types['unsupported'] = WooGraphQL::get_supported_product_type();
1✔
907
                }
908

909
                foreach ( $product_type_keys as $type_key ) {
298✔
910
                        $field_name = "{$type_key}Product";
298✔
911
                        $type_name  = $product_types[ $type_key ] ?? null;
298✔
912

913
                        if ( empty( $type_name ) ) {
298✔
914
                                continue;
×
915
                        }
916

917
                        register_graphql_field(
298✔
918
                                'RootQuery',
298✔
919
                                $field_name,
298✔
920
                                [
298✔
921
                                        'type'              => $type_name,
298✔
922
                                        'description'       => static function () use ( $type_key ) {
298✔
923
                                                /* translators: %s: Product type slug */
924
                                                return sprintf( __( 'A %s product object', 'graphql-for-ecommerce' ), $type_key );
3✔
925
                                        },
298✔
926
                                        'deprecationReason' => 'Use "product" instead.',
298✔
927
                                        'args'              => [
298✔
928
                                                'id'     => [
298✔
929
                                                        'type'        => 'ID',
298✔
930
                                                        'description' => static function () use ( $type_name ) {
298✔
931
                                                                /* translators: %s: product type */
932
                                                                return sprintf( __( 'The ID for identifying the %s product', 'graphql-for-ecommerce' ), $type_name );
3✔
933
                                                        },
298✔
934
                                                ],
298✔
935
                                                'idType' => [
298✔
936
                                                        'type'        => 'ProductIdTypeEnum',
298✔
937
                                                        'description' => static function () {
298✔
938
                                                                return __( 'Type of ID being used identify product', 'graphql-for-ecommerce' );
3✔
939
                                                        },
298✔
940
                                                ],
298✔
941
                                        ],
298✔
942
                                        'resolve'           => static function ( $source, array $args, AppContext $context ) use ( $type_key, $unsupported_type_enabled ) {
298✔
943
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
944
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
945

946
                                                $product_id = null;
1✔
947
                                                switch ( $id_type ) {
948
                                                        case 'sku':
1✔
949
                                                                $product_id = \wc_get_product_id_by_sku( $id );
×
950
                                                                break;
×
951
                                                        case 'slug':
1✔
952
                                                                $post       = get_page_by_path( $id, OBJECT, 'product' );
×
953
                                                                $product_id = ! empty( $post ) ? absint( $post->ID ) : 0;
×
954
                                                                break;
×
955
                                                        case 'database_id':
1✔
956
                                                                $product_id = absint( $id );
×
957
                                                                break;
×
958
                                                        case 'global_id':
1✔
959
                                                        default:
960
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
961
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
NEW
962
                                                                        throw new UserError( __( 'The "id" is invalid', 'graphql-for-ecommerce' ) );
×
963
                                                                }
964
                                                                $product_id = absint( $id_components['id'] );
1✔
965
                                                                break;
1✔
966
                                                }
967

968
                                                if ( empty( $product_id ) ) {
1✔
969
                                                        /* translators: %1$s: ID type, %2$s: ID value */
NEW
970
                                                        throw new UserError( sprintf( __( 'No product ID was found corresponding to the %1$s: %2$s', 'graphql-for-ecommerce' ), $id_type, $product_id ) );
×
971
                                                }
972

973
                                                if ( \WC()->product_factory->get_product_type( $product_id ) !== $type_key && 'off' === $unsupported_type_enabled ) {
1✔
974
                                                        /* translators: Invalid product type message %1$s: Product ID, %2$s: Product type */
NEW
975
                                                        throw new UserError( sprintf( __( 'This product of ID %1$s is not a %2$s product', 'graphql-for-ecommerce' ), $product_id, $type_key ) );
×
976
                                                }
977

978
                                                $product = get_post( $product_id );
1✔
979
                                                if ( ! is_object( $product ) || 'product' !== $product->post_type ) {
1✔
980
                                                        /* translators: %1$s: ID type, %2$s: ID value */
NEW
981
                                                        throw new UserError( sprintf( __( 'No product exists with the %1$s: %2$s', 'graphql-for-ecommerce' ), $id_type, $product_id ) );
×
982
                                                }
983

984
                                                return Factory::resolve_crud_object( $product_id, $context );
1✔
985
                                        },
298✔
986
                                ]
298✔
987
                        );
298✔
988
                }//end foreach
989
        }
990
}
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

© 2026 Coveralls, Inc