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

wp-graphql / wp-graphql-woocommerce / 23266749290

18 Mar 2026 08:59PM UTC coverage: 83.127% (-0.06%) from 83.184%
23266749290

push

github

web-flow
feat: New fees input fields add to `cart` query and `checkout` mutation (#929)

* feat: New fees input fields add to `cart` query and `checkout` mutation

* chore: Linter compliance met

* devops: CartQueriesTest tests updated

105 of 108 new or added lines in 8 files covered. (97.22%)

18 existing lines in 2 files now uncovered.

12652 of 15220 relevant lines covered (83.13%)

76.97 hits per line

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

91.78
/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(
155✔
30
                        'RootQuery',
155✔
31
                        [
155✔
32
                                'cart'             => [
155✔
33
                                        'type'        => 'Cart',
155✔
34
                                        'args'        => [
155✔
35
                                                'recalculateTotals' => [
155✔
36
                                                        'type'        => 'Boolean',
155✔
37
                                                        'description' => __( 'Should cart totals be recalculated.', 'wp-graphql-woocommerce' ),
155✔
38
                                                ],
155✔
39
                                                'fees'              => [
155✔
40
                                                        'type'        => [ 'list_of' => 'FeeInput' ],
155✔
41
                                                        'description' => __( 'Fees to add to the cart.', 'wp-graphql-woocommerce' ),
155✔
42
                                                ],
155✔
43
                                        ],
155✔
44
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
155✔
45
                                        'resolve'     => static function ( $_, $args ) {
155✔
46
                                                $token_invalid = apply_filters( 'graphql_woocommerce_session_token_errors', null );
10✔
47
                                                if ( $token_invalid ) {
10✔
48
                                                        throw new UserError( $token_invalid );
×
49
                                                }
50

51
                                                $cart = Factory::resolve_cart();
10✔
52

53
                                                if ( ! empty( $args['fees'] ) ) {
10✔
54
                                                        $fees = $args['fees'];
1✔
55
                                                        add_action(
1✔
56
                                                                'woocommerce_cart_calculate_fees',
1✔
57
                                                                static function () use ( $fees ) {
1✔
58
                                                                        foreach ( $fees as $fee_input ) {
1✔
59
                                                                                if ( empty( $fee_input['name'] ) || empty( $fee_input['amount'] ) ) {
1✔
60
                                                                                        // TODO: Log invalid fee input.
NEW
61
                                                                                        continue;
×
62
                                                                                }
63

64
                                                                                $fee_args = [
1✔
65
                                                                                        $fee_input['name'],
1✔
66
                                                                                        $fee_input['amount'],
1✔
67
                                                                                        isset( $fee_input['taxable'] ) ? $fee_input['taxable'] : false,
1✔
68
                                                                                        isset( $fee_input['taxClass'] ) ? $fee_input['taxClass'] : '',
1✔
69
                                                                                ];
1✔
70

71
                                                                                \WC()->cart->add_fee( ...$fee_args );
1✔
72
                                                                        }
73
                                                                }
1✔
74
                                                        );
1✔
75
                                                }
76

77
                                                if ( ! empty( $args['recalculateTotals'] ) ) {
10✔
78
                                                        $cart->calculate_totals();
×
79
                                                }
80

81
                                                return $cart;
10✔
82
                                        },
155✔
83
                                ],
155✔
84
                                'cartItem'         => [
155✔
85
                                        'type'        => 'CartItem',
155✔
86
                                        'args'        => [
155✔
87
                                                'key' => [
155✔
88
                                                        'type' => [ 'non_null' => 'ID' ],
155✔
89
                                                ],
155✔
90
                                        ],
155✔
91
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
155✔
92
                                        'resolve'     => static function ( $source, array $args ) {
155✔
93
                                                $item = Factory::resolve_cart()->get_cart_item( $args['key'] );
1✔
94
                                                if ( empty( $item ) || empty( $item['key'] ) ) {
1✔
95
                                                        throw new UserError( __( 'Failed to retrieve cart item.', 'wp-graphql-woocommerce' ) );
×
96
                                                }
97

98
                                                return $item;
1✔
99
                                        },
155✔
100
                                ],
155✔
101
                                'cartFee'          => [
155✔
102
                                        'type'        => 'CartFee',
155✔
103
                                        'args'        => [
155✔
104
                                                'id' => [
155✔
105
                                                        'type' => [ 'non_null' => 'ID' ],
155✔
106
                                                ],
155✔
107
                                        ],
155✔
108
                                        'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
155✔
109
                                        'resolve'     => static function ( $source, array $args ) {
155✔
110
                                                $fees   = Factory::resolve_cart()->get_fees();
1✔
111
                                                $fee_id = $args['id'];
1✔
112

113
                                                if ( empty( $fees[ $fee_id ] ) ) {
1✔
114
                                                        throw new UserError( __( 'The ID input is invalid', 'wp-graphql-woocommerce' ) );
×
115
                                                }
116

117
                                                return $fees[ $fee_id ];
1✔
118
                                        },
155✔
119
                                ],
155✔
120
                                'coupon'           => [
155✔
121
                                        'type'        => 'Coupon',
155✔
122
                                        'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ),
155✔
123
                                        'args'        => [
155✔
124
                                                'id'     => [ 'type' => [ 'non_null' => 'ID' ] ],
155✔
125
                                                'idType' => [
155✔
126
                                                        'type'        => 'CouponIdTypeEnum',
155✔
127
                                                        'description' => __( 'Type of ID being used identify coupon', 'wp-graphql-woocommerce' ),
155✔
128
                                                ],
155✔
129
                                        ],
155✔
130
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
155✔
131
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
3✔
132
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
3✔
133

134
                                                $coupon_id = null;
3✔
135
                                                switch ( $id_type ) {
136
                                                        case 'code':
3✔
137
                                                                $coupon_id = \wc_get_coupon_id_by_code( $id );
1✔
138
                                                                break;
1✔
139
                                                        case 'database_id':
3✔
140
                                                                $coupon_id = absint( $id );
1✔
141
                                                                break;
1✔
142
                                                        case 'global_id':
3✔
143
                                                        default:
144
                                                                $id_components = Relay::fromGlobalId( $args['id'] );
3✔
145
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
3✔
146
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
147
                                                                }
148
                                                                $coupon_id = absint( $id_components['id'] );
3✔
149
                                                                break;
3✔
150
                                                }
151

152
                                                // Check if user authorized to view coupon.
153
                                                /**
154
                                                 * Get coupon post type.
155
                                                 *
156
                                                 * @var \WP_Post_Type $post_type
157
                                                 */
158
                                                $post_type     = get_post_type_object( 'shop_coupon' );
3✔
159
                                                $is_authorized = current_user_can( $post_type->cap->edit_others_posts );
3✔
160
                                                if ( ! $is_authorized ) {
3✔
161
                                                        return null;
1✔
162
                                                }
163

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

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

175
                                                return Factory::resolve_crud_object( $coupon_id, $context );
3✔
176
                                        },
155✔
177
                                ],
155✔
178
                                'customer'         => [
155✔
179
                                        'type'        => 'Customer',
155✔
180
                                        'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
155✔
181
                                        'args'        => [
155✔
182
                                                'id'         => [
155✔
183
                                                        'type'        => 'ID',
155✔
184
                                                        'description' => __( 'Get the customer by their global ID', 'wp-graphql-woocommerce' ),
155✔
185
                                                ],
155✔
186
                                                'customerId' => [
155✔
187
                                                        'type'        => 'Int',
155✔
188
                                                        'description' => __( 'Get the customer by their database ID', 'wp-graphql-woocommerce' ),
155✔
189
                                                ],
155✔
190
                                        ],
155✔
191
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
155✔
192
                                                $current_user_id = get_current_user_id();
11✔
193

194
                                                // Default the customer to the current user.
195
                                                $customer_id = $current_user_id;
11✔
196

197
                                                // If a customer ID has been provided, resolve to that ID instead.
198
                                                if ( ! empty( $args['id'] ) ) {
11✔
199
                                                        $id_components = Relay::fromGlobalId( $args['id'] );
3✔
200
                                                        if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
3✔
201
                                                                throw new UserError( __( 'The ID input is invalid', 'wp-graphql-woocommerce' ) );
×
202
                                                        }
203

204
                                                        $customer_id = absint( $id_components['id'] );
3✔
205
                                                } elseif ( ! empty( $args['customerId'] ) ) {
11✔
206
                                                        $customer_id = absint( $args['customerId'] );
1✔
207
                                                }
208

209
                                                // If a user does not have the ability to list users, they can only view their own customer object.
210
                                                $unauthorized = ! empty( $customer_id )
11✔
211
                                                        && ! current_user_can( 'list_users' )
11✔
212
                                                        && $current_user_id !== $customer_id;
11✔
213
                                                if ( $unauthorized ) {
11✔
214
                                                        throw new UserError( __( 'Not authorized to access this customer', 'wp-graphql-woocommerce' ) );
2✔
215
                                                }
216

217
                                                // If we have a customer ID, resolve to that customer.
218
                                                if ( $customer_id ) {
11✔
219
                                                        return Factory::resolve_customer( $customer_id, $context );
11✔
220
                                                }
221

222
                                                // Resolve to the session customer.
223
                                                return Factory::resolve_session_customer();
1✔
224
                                        },
155✔
225
                                ],
155✔
226
                                'order'            => [
155✔
227
                                        'type'        => 'Order',
155✔
228
                                        'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
155✔
229
                                        'args'        => [
155✔
230
                                                'id'     => [
155✔
231
                                                        'type'        => 'ID',
155✔
232
                                                        'description' => __( 'The ID for identifying the order', 'wp-graphql-woocommerce' ),
155✔
233
                                                ],
155✔
234
                                                'idType' => [
155✔
235
                                                        'type'        => 'OrderIdTypeEnum',
155✔
236
                                                        'description' => __( 'Type of ID being used identify order', 'wp-graphql-woocommerce' ),
155✔
237
                                                ],
155✔
238
                                        ],
155✔
239
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
155✔
240
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
12✔
241
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
12✔
242

243
                                                $order_id = null;
12✔
244
                                                switch ( $id_type ) {
245
                                                        case 'order_key':
12✔
246
                                                                $order_id = \wc_get_order_id_by_order_key( $id );
1✔
247
                                                                break;
1✔
248
                                                        case 'database_id':
12✔
249
                                                                $order_id = absint( $id );
2✔
250
                                                                break;
2✔
251
                                                        case 'global_id':
11✔
252
                                                        default:
253
                                                                $id_components = Relay::fromGlobalId( $id );
11✔
254
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
11✔
255
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
256
                                                                }
257
                                                                $order_id = absint( $id_components['id'] );
11✔
258
                                                                break;
11✔
259
                                                }
260

261
                                                if ( empty( $order_id ) ) {
12✔
262
                                                        /* translators: %1$s: ID type, %2$s: ID value */
263
                                                        throw new UserError( sprintf( __( 'No order ID was found corresponding to the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
264
                                                }
265

266
                                                if ( 'shop_order' !== OrderUtil::get_order_type( $order_id ) ) {
12✔
267
                                                        /* translators: %1$s: ID type, %2$s: ID value */
268
                                                        throw new UserError( sprintf( __( 'No order exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $id ) );
×
269
                                                }
270

271
                                                // Check if user authorized to view order.
272
                                                /**
273
                                                 * Get order post type.
274
                                                 *
275
                                                 * @var \WP_Post_Type $post_type
276
                                                 */
277
                                                $post_type     = get_post_type_object( 'shop_order' );
12✔
278
                                                $is_authorized = current_user_can( $post_type->cap->edit_others_posts );
12✔
279
                                                if ( ! $is_authorized && get_current_user_id() ) {
12✔
280
                                                        /** @var \WC_Order[] $orders */
281
                                                        $orders = wc_get_orders(
3✔
282
                                                                [
3✔
283
                                                                        'type'          => 'shop_order',
3✔
284
                                                                        'post__in'      => [ $order_id ],
3✔
285
                                                                        'customer_id'   => get_current_user_id(),
3✔
286
                                                                        'no_rows_found' => true,
3✔
287
                                                                        'return'        => 'ids',
3✔
288
                                                                ]
3✔
289
                                                        );
3✔
290

291
                                                        if ( in_array( $order_id, $orders, true ) ) {
3✔
292
                                                                $is_authorized = true;
2✔
293
                                                        }
294
                                                }
295

296
                                                // Throw if authorized to view order.
297
                                                if ( ! $is_authorized ) {
12✔
298
                                                        throw new UserError( __( 'Not authorized to access this order', 'wp-graphql-woocommerce' ) );
1✔
299
                                                }
300

301
                                                return Factory::resolve_crud_object( $order_id, $context );
12✔
302
                                        },
155✔
303
                                ],
155✔
304
                                'productVariation' => [
155✔
305
                                        'type'        => 'ProductVariation',
155✔
306
                                        'description' => __( 'A product variation object', 'wp-graphql-woocommerce' ),
155✔
307
                                        'args'        => [
155✔
308
                                                'id'     => [
155✔
309
                                                        'type'        => 'ID',
155✔
310
                                                        'description' => __( 'The ID for identifying the product variation', 'wp-graphql-woocommerce' ),
155✔
311
                                                ],
155✔
312
                                                'idType' => [
155✔
313
                                                        'type'        => 'ProductVariationIdTypeEnum',
155✔
314
                                                        'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
155✔
315
                                                ],
155✔
316
                                        ],
155✔
317
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
155✔
318
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
6✔
319
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
6✔
320

321
                                                $variation_id = null;
6✔
322
                                                switch ( $id_type ) {
323
                                                        case 'database_id':
6✔
324
                                                                $variation_id = absint( $id );
2✔
325
                                                                break;
2✔
326
                                                        case 'global_id':
5✔
327
                                                        default:
328
                                                                $id_components = Relay::fromGlobalId( $id );
5✔
329
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
5✔
330
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
331
                                                                }
332
                                                                $variation_id = absint( $id_components['id'] );
5✔
333
                                                                break;
5✔
334
                                                }
335

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

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

347
                                                return Factory::resolve_crud_object( $variation_id, $context );
6✔
348
                                        },
155✔
349
                                ],
155✔
350
                                'refund'           => [
155✔
351
                                        'type'        => 'Refund',
155✔
352
                                        'description' => __( 'A refund object', 'wp-graphql-woocommerce' ),
155✔
353
                                        'args'        => [
155✔
354
                                                'id'     => [
155✔
355
                                                        'type'        => [ 'non_null' => 'ID' ],
155✔
356
                                                        'description' => __( 'The ID for identifying the refund', 'wp-graphql-woocommerce' ),
155✔
357
                                                ],
155✔
358
                                                'idType' => [
155✔
359
                                                        'type'        => 'RefundIdTypeEnum',
155✔
360
                                                        'description' => __( 'Type of ID being used identify refund', 'wp-graphql-woocommerce' ),
155✔
361
                                                ],
155✔
362
                                        ],
155✔
363
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
155✔
364
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
3✔
365
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
3✔
366

367
                                                $refund_id = null;
3✔
368
                                                switch ( $id_type ) {
369
                                                        case 'database_id':
3✔
370
                                                                $refund_id = absint( $id );
1✔
371
                                                                break;
1✔
372
                                                        case 'global_id':
3✔
373
                                                        default:
374
                                                                $id_components = Relay::fromGlobalId( $id );
3✔
375
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
3✔
376
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
377
                                                                }
378
                                                                $refund_id = absint( $id_components['id'] );
3✔
379
                                                                break;
3✔
380
                                                }
381

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

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

392
                                                // Check if user authorized to view order.
393
                                                /**
394
                                                 * Get refund post type.
395
                                                 *
396
                                                 * @var \WP_Post_Type $post_type
397
                                                 */
398
                                                $post_type     = get_post_type_object( 'shop_order_refund' );
3✔
399
                                                $is_authorized = current_user_can( $post_type->cap->edit_others_posts );
3✔
400
                                                if ( get_current_user_id() ) {
3✔
401
                                                        $refund = \wc_get_order( $refund_id );
3✔
402
                                                        if ( ! is_object( $refund ) || ! is_a( $refund, \WC_Order_Refund::class ) ) {
3✔
403
                                                                throw new UserError( __( 'Failed to retrieve refund', 'wp-graphql-woocommerce' ) );
×
404
                                                        }
405
                                                        $order_id = $refund->get_parent_id();
3✔
406

407
                                                        /** @var \WC_Order[] $orders */
408
                                                        $orders = wc_get_orders(
3✔
409
                                                                [
3✔
410
                                                                        'type'          => 'shop_order',
3✔
411
                                                                        'post__in'      => [ $order_id ],
3✔
412
                                                                        'customer_id'   => get_current_user_id(),
3✔
413
                                                                        'no_rows_found' => true,
3✔
414
                                                                        'return'        => 'ids',
3✔
415
                                                                ]
3✔
416
                                                        );
3✔
417

418
                                                        if ( in_array( $order_id, $orders, true ) ) {
3✔
419
                                                                $is_authorized = true;
3✔
420
                                                        }
421
                                                }//end if
422

423
                                                // Throw if authorized to view refund.
424
                                                if ( ! $is_authorized ) {
3✔
425
                                                        throw new UserError( __( 'Not authorized to access this refund', 'wp-graphql-woocommerce' ) );
1✔
426
                                                }
427

428
                                                return Factory::resolve_crud_object( $refund_id, $context );
3✔
429
                                        },
155✔
430
                                ],
155✔
431
                                'shippingMethod'   => [
155✔
432
                                        'type'        => 'ShippingMethod',
155✔
433
                                        'description' => __( 'A shipping method object', 'wp-graphql-woocommerce' ),
155✔
434
                                        'args'        => [
155✔
435
                                                'id'     => [
155✔
436
                                                        'type'        => 'ID',
155✔
437
                                                        'description' => __( 'The ID for identifying the shipping method', 'wp-graphql-woocommerce' ),
155✔
438
                                                ],
155✔
439
                                                'idType' => [
155✔
440
                                                        'type'        => 'ShippingMethodIdTypeEnum',
155✔
441
                                                        'description' => __( 'Type of ID being used identify product variation', 'wp-graphql-woocommerce' ),
155✔
442
                                                ],
155✔
443
                                        ],
155✔
444
                                        'resolve'     => static function ( $source, array $args ) {
155✔
445
                                                if ( ! \wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) {
1✔
446
                                                        throw new UserError( __( 'Sorry, you cannot view shipping methods.', 'wp-graphql-woocommerce' ), \rest_authorization_required_code() );
1✔
447
                                                }
448

449
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
450
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
451

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

467
                                                return Factory::resolve_shipping_method( $method_id );
1✔
468
                                        },
155✔
469
                                ],
155✔
470
                                'shippingZone'     => [
155✔
471
                                        'type'        => 'ShippingZone',
155✔
472
                                        'description' => __( 'A shipping zone object', 'wp-graphql-woocommerce' ),
155✔
473
                                        'args'        => [
155✔
474
                                                'id'     => [
155✔
475
                                                        'type'        => 'ID',
155✔
476
                                                        'description' => __( 'The ID for identifying the shipping zone', 'wp-graphql-woocommerce' ),
155✔
477
                                                ],
155✔
478
                                                'idType' => [
155✔
479
                                                        'type'        => 'ShippingZoneIdTypeEnum',
155✔
480
                                                        'description' => __( 'Type of ID being used identify shipping zone', 'wp-graphql-woocommerce' ),
155✔
481
                                                ],
155✔
482
                                        ],
155✔
483
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
155✔
484
                                                if ( ! \wc_shipping_enabled() ) {
1✔
485
                                                        throw new UserError( __( 'Shipping is disabled.', 'wp-graphql-woocommerce' ), 404 );
×
486
                                                }
487

488
                                                if ( ! \wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
1✔
489
                                                        throw new UserError( __( 'Permission denied.', 'wp-graphql-woocommerce' ), \rest_authorization_required_code() );
1✔
490
                                                }
491

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

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

510
                                                return $context->get_loader( 'shipping_zone' )->load( $zone_id );
1✔
511
                                        },
155✔
512
                                ],
155✔
513
                                'taxRate'          => [
155✔
514
                                        'type'        => 'TaxRate',
155✔
515
                                        'description' => __( 'A tax rate object', 'wp-graphql-woocommerce' ),
155✔
516
                                        'args'        => [
155✔
517
                                                'id'     => [
155✔
518
                                                        'type'        => 'ID',
155✔
519
                                                        'description' => __( 'The ID for identifying the tax rate', 'wp-graphql-woocommerce' ),
155✔
520
                                                ],
155✔
521
                                                'idType' => [
155✔
522
                                                        'type'        => 'TaxRateIdTypeEnum',
155✔
523
                                                        'description' => __( 'Type of ID being used identify tax rate', 'wp-graphql-woocommerce' ),
155✔
524
                                                ],
155✔
525
                                        ],
155✔
526
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
155✔
527
                                                if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
1✔
528
                                                        throw new UserError( __( 'Sorry, you cannot view tax rates.', 'wp-graphql-woocommerce' ), \rest_authorization_required_code() );
1✔
529
                                                }
530
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
531
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
532

533
                                                $rate_id = null;
1✔
534
                                                switch ( $id_type ) {
535
                                                        case 'database_id':
1✔
536
                                                                $rate_id = absint( $id );
1✔
537
                                                                break;
1✔
538
                                                        case 'global_id':
1✔
539
                                                        default:
540
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
541
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
542
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
543
                                                                }
544
                                                                $rate_id = absint( $id_components['id'] );
1✔
545
                                                                break;
1✔
546
                                                }
547

548
                                                return Factory::resolve_tax_rate( $rate_id, $context );
1✔
549
                                        },
155✔
550
                                ],
155✔
551
                                'countries'        => [
155✔
552
                                        'type'        => [ 'list_of' => 'CountriesEnum' ],
155✔
553
                                        'description' => __( 'Countries', 'wp-graphql-woocommerce' ),
155✔
554
                                        'resolve'     => static function () {
155✔
555
                                                $wc_countries = new \WC_Countries();
1✔
556
                                                $countries    = $wc_countries->get_countries();
1✔
557

558
                                                return array_keys( $countries );
1✔
559
                                        },
155✔
560
                                ],
155✔
561
                                'allowedCountries' => [
155✔
562
                                        'type'        => [ 'list_of' => 'CountriesEnum' ],
155✔
563
                                        'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
155✔
564
                                        'resolve'     => static function () {
155✔
565
                                                $wc_countries = new \WC_Countries();
1✔
566
                                                $countries    = $wc_countries->get_allowed_countries();
1✔
567

568
                                                return array_keys( $countries );
1✔
569
                                        },
155✔
570
                                ],
155✔
571
                                'countryStates'    => [
155✔
572
                                        'type'        => [ 'list_of' => 'CountryState' ],
155✔
573
                                        'args'        => [
155✔
574
                                                'country' => [
155✔
575
                                                        'type'        => [ 'non_null' => 'CountriesEnum' ],
155✔
576
                                                        'description' => __( 'Target country', 'wp-graphql-woocommerce' ),
155✔
577
                                                ],
155✔
578
                                        ],
155✔
579
                                        'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
155✔
580
                                        'resolve'     => static function ( $_, $args ) {
155✔
581
                                                $country      = $args['country'];
1✔
582
                                                $wc_countries = new \WC_Countries();
1✔
583
                                                $states       = $wc_countries->get_shipping_country_states();
1✔
584

585
                                                if ( ! empty( $states ) && ! empty( $states[ $country ] ) ) {
1✔
586
                                                        $formatted_states = [];
1✔
587
                                                        foreach ( $states[ $country ] as $code => $name ) {
1✔
588
                                                                $formatted_states[] = compact( 'name', 'code' );
1✔
589
                                                        }
590

591
                                                        return $formatted_states;
1✔
592
                                                }
593

594
                                                return [];
×
595
                                        },
155✔
596
                                ],
155✔
597
                                'collectionStats'  => [
155✔
598
                                        'type'        => 'CollectionStats',
155✔
599
                                        'args'        => [
155✔
600
                                                'calculatePriceRange'        => [
155✔
601
                                                        'type'        => 'Boolean',
155✔
602
                                                        'description' => __( 'If true, calculates the minimum and maximum product prices for the collection.', 'wp-graphql-woocommerce' ),
155✔
603
                                                ],
155✔
604
                                                'calculateRatingCounts'      => [
155✔
605
                                                        'type'        => 'Boolean',
155✔
606
                                                        'description' => __( 'If true, calculates rating counts for products in the collection.', 'wp-graphql-woocommerce' ),
155✔
607
                                                ],
155✔
608
                                                'calculateStockStatusCounts' => [
155✔
609
                                                        'type'        => 'Boolean',
155✔
610
                                                        'description' => __( 'If true, calculates stock counts for products in the collection.', 'wp-graphql-woocommerce' ),
155✔
611
                                                ],
155✔
612
                                                'taxonomies'                 => [
155✔
613
                                                        'type' => [ 'list_of' => 'CollectionStatsQueryInput' ],
155✔
614
                                                ],
155✔
615
                                                'where'                      => [
155✔
616
                                                        'type' => 'CollectionStatsWhereArgs',
155✔
617
                                                ],
155✔
618
                                        ],
155✔
619
                                        'description' => __( 'Statistics for a product taxonomy query', 'wp-graphql-woocommerce' ),
155✔
620
                                        'resolve'     => static function ( $_, $args ) {
155✔
621
                                                /** @var array<string, mixed> $data */
622
                                                $data    = [
4✔
623
                                                        'min_price'           => null,
4✔
624
                                                        'max_price'           => null,
4✔
625
                                                        'attribute_counts'    => null,
4✔
626
                                                        'stock_status_counts' => null,
4✔
627
                                                        'rating_counts'       => null,
4✔
628
                                                ];
4✔
629
                                                $filters = new ProductQueryFilters();
4✔
630

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

634
                                                // Format taxonomies.
635
                                                if ( ! empty( $args['taxonomies'] ) ) {
4✔
636
                                                        $calculate_attribute_counts = [];
4✔
637
                                                        foreach ( $args['taxonomies'] as $attribute_to_count ) {
4✔
638
                                                                $attribute = [ 'taxonomy' => $attribute_to_count['taxonomy'] ];
4✔
639
                                                                // Set the query type.
640
                                                                if ( ! empty( $attribute_to_count['relation'] ) ) {
4✔
641
                                                                        $attribute['query_type'] = strtolower( $attribute_to_count['relation'] );
4✔
642
                                                                }
643

644
                                                                // Add the attribute to the list of attributes to count.
645
                                                                $calculate_attribute_counts[] = $attribute;
4✔
646
                                                        }
647

648
                                                        // Set the attribute counts to calculate.
649
                                                        $request->set_param( 'calculate_attribute_counts', $calculate_attribute_counts );
4✔
650
                                                }
651

652
                                                $request->set_param( 'calculate_price_range', $args['calculatePriceRange'] ?? false );
4✔
653
                                                $request->set_param( 'calculate_stock_status_counts', $args['calculateStockStatusCounts'] ?? false );
4✔
654
                                                $request->set_param( 'calculate_rating_counts', $args['calculateRatingCounts'] ?? false );
4✔
655

656
                                                if ( ! empty( $request['calculate_price_range'] ) ) {
4✔
657
                                                        /**
658
                                                         * A Rest request object for external filtering
659
                                                         *
660
                                                         * @var \WP_REST_Request $filter_request
661
                                                         */
662
                                                        $filter_request = clone $request;
1✔
663
                                                        $filter_request->set_param( 'min_price', null );
1✔
664
                                                        $filter_request->set_param( 'max_price', null );
1✔
665

666
                                                        /** @var object{min_price: float, max_price: float} */
667
                                                        $price_results     = $filters->get_filtered_price( $filter_request );
1✔
668
                                                        $data['min_price'] = $price_results->min_price;
1✔
669
                                                        $data['max_price'] = $price_results->max_price;
1✔
670
                                                }
671

672
                                                if ( ! empty( $request['calculate_stock_status_counts'] ) ) {
4✔
673
                                                        /**
674
                                                         * A Rest request object for external filtering
675
                                                         *
676
                                                         * @var \WP_REST_Request $filter_request
677
                                                         */
678
                                                        $filter_request = clone $request;
1✔
679
                                                        $counts         = $filters->get_stock_status_counts( $filter_request );
1✔
680

681
                                                        $data['stock_status_counts'] = [];
1✔
682

683
                                                        foreach ( $counts as $key => $value ) {
1✔
684
                                                                $data['stock_status_counts'][] = (object) [
1✔
685
                                                                        'status' => $key,
1✔
686
                                                                        'count'  => $value,
1✔
687
                                                                ];
1✔
688
                                                        }
689
                                                }
690

691
                                                if ( ! empty( $request['calculate_attribute_counts'] ) ) {
4✔
692
                                                        $taxonomy__or_queries  = [];
4✔
693
                                                        $taxonomy__and_queries = [];
4✔
694
                                                        foreach ( $request['calculate_attribute_counts'] as $attributes_to_count ) {
4✔
695
                                                                if ( ! isset( $attributes_to_count['taxonomy'] ) ) {
4✔
696
                                                                        continue;
×
697
                                                                }
698

699
                                                                if ( empty( $attributes_to_count['query_type'] ) || 'or' === $attributes_to_count['query_type'] ) {
4✔
700
                                                                        $taxonomy__or_queries[] = $attributes_to_count['taxonomy'];
3✔
701
                                                                } else {
702
                                                                        $taxonomy__and_queries[] = $attributes_to_count['taxonomy'];
3✔
703
                                                                }
704
                                                        }
705

706
                                                        $data['attribute_counts'] = [];
4✔
707
                                                        if ( ! empty( $taxonomy__or_queries ) ) {
4✔
708
                                                                foreach ( $taxonomy__or_queries as $taxonomy ) {
3✔
709
                                                                        /**
710
                                                                         * A Rest request object for external filtering
711
                                                                         *
712
                                                                         * @var \WP_REST_Request $filter_request
713
                                                                         */
714
                                                                        $filter_request    = clone $request;
3✔
715
                                                                        $filter_attributes = $filter_request->get_param( 'attributes' );
3✔
716

717
                                                                        if ( ! empty( $filter_attributes ) ) {
3✔
718
                                                                                $filter_attributes = array_filter(
×
719
                                                                                        $filter_attributes,
×
720
                                                                                        static function ( $query ) use ( $taxonomy ) {
×
721
                                                                                                return $query['attribute'] !== $taxonomy;
×
722
                                                                                        }
×
723
                                                                                );
×
724
                                                                        }
725

726
                                                                        $filter_request->set_param( 'attributes', $filter_attributes );
3✔
727
                                                                        $counts = $filters->get_attribute_counts( $filter_request, [ $taxonomy ] );
3✔
728

729
                                                                        $data['attribute_counts'][ $taxonomy ] = [];
3✔
730
                                                                        foreach ( $counts as $key => $value ) {
3✔
731
                                                                                $data['attribute_counts'][ $taxonomy ][] = (object) [
3✔
732
                                                                                        'taxonomy' => $taxonomy,
3✔
733
                                                                                        'termId'   => $key,
3✔
734
                                                                                        'count'    => $value,
3✔
735
                                                                                ];
3✔
736
                                                                        }
737
                                                                }
738
                                                        }
739

740
                                                        if ( ! empty( $taxonomy__and_queries ) ) {
4✔
741
                                                                $counts = $filters->get_attribute_counts( $request, $taxonomy__and_queries );
3✔
742

743
                                                                foreach ( $taxonomy__and_queries as $taxonomy ) {
3✔
744
                                                                        $data['attribute_counts'][ $taxonomy ] = [];
3✔
745
                                                                        foreach ( $counts as $key => $value ) {
3✔
746
                                                                                $data['attribute_counts'][ $taxonomy ][] = (object) [
3✔
747
                                                                                        'taxonomy' => $taxonomy,
3✔
748
                                                                                        'termId'   => $key,
3✔
749
                                                                                        'count'    => $value,
3✔
750
                                                                                ];
3✔
751
                                                                        }
752
                                                                }
753
                                                        }
754
                                                }
755

756
                                                if ( ! empty( $request['calculate_rating_counts'] ) ) {
4✔
757
                                                        /**
758
                                                         * A Rest request object for external filtering
759
                                                         *
760
                                                         * @var \WP_REST_Request $filter_request
761
                                                         */
762
                                                        $filter_request        = clone $request;
4✔
763
                                                        $counts                = $filters->get_rating_counts( $filter_request );
4✔
764
                                                        $data['rating_counts'] = [];
4✔
765

766
                                                        foreach ( $counts as $key => $value ) {
4✔
767
                                                                $data['rating_counts'][] = (object) [
×
768
                                                                        'rating' => $key,
×
769
                                                                        'count'  => $value,
×
770
                                                                ];
×
771
                                                        }
772
                                                }
773

774
                                                return $data;
4✔
775
                                        },
155✔
776
                                ],
155✔
777
                        ]
155✔
778
                );
155✔
779

780
                // Product queries.
781
                $unsupported_type_enabled = woographql_setting( 'enable_unsupported_product_type', 'off' );
155✔
782

783
                $product_type_keys = array_keys( WooGraphQL::get_enabled_product_types() );
155✔
784
                if ( 'on' === $unsupported_type_enabled ) {
155✔
785
                        $product_type_keys[] = 'unsupported';
1✔
786
                }
787

788
                $product_type_keys = apply_filters( 'woographql_register_product_queries', $product_type_keys );
155✔
789

790
                $product_types = WooGraphQL::get_enabled_product_types();
155✔
791
                if ( 'on' === $unsupported_type_enabled ) {
155✔
792
                        $product_types['unsupported'] = WooGraphQL::get_supported_product_type();
1✔
793
                }
794

795
                foreach ( $product_type_keys as $type_key ) {
155✔
796
                        $field_name = "{$type_key}Product";
155✔
797
                        $type_name  = $product_types[ $type_key ] ?? null;
155✔
798

799
                        if ( empty( $type_name ) ) {
155✔
800
                                continue;
×
801
                        }
802

803
                        register_graphql_field(
155✔
804
                                'RootQuery',
155✔
805
                                $field_name,
155✔
806
                                [
155✔
807
                                        'type'              => $type_name,
155✔
808
                                        /* translators: Product type slug */
809
                                        'description'       => sprintf( __( 'A %s product object', 'wp-graphql-woocommerce' ), $type_key ),
155✔
810
                                        'deprecationReason' => 'Use "product" instead.',
155✔
811
                                        'args'              => [
155✔
812
                                                'id'     => [
155✔
813
                                                        'type'        => 'ID',
155✔
814
                                                        'description' => sprintf(
155✔
815
                                                                /* translators: %s: product type */
816
                                                                __( 'The ID for identifying the %s product', 'wp-graphql-woocommerce' ),
155✔
817
                                                                $type_name
155✔
818
                                                        ),
155✔
819
                                                ],
155✔
820
                                                'idType' => [
155✔
821
                                                        'type'        => 'ProductIdTypeEnum',
155✔
822
                                                        'description' => __( 'Type of ID being used identify product', 'wp-graphql-woocommerce' ),
155✔
823
                                                ],
155✔
824
                                        ],
155✔
825
                                        'resolve'           => static function ( $source, array $args, AppContext $context ) use ( $type_key, $unsupported_type_enabled ) {
155✔
826
                                                $id      = isset( $args['id'] ) ? $args['id'] : null;
1✔
827
                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
1✔
828

829
                                                $product_id = null;
1✔
830
                                                switch ( $id_type ) {
831
                                                        case 'sku':
1✔
832
                                                                $product_id = \wc_get_product_id_by_sku( $id );
×
833
                                                                break;
×
834
                                                        case 'slug':
1✔
835
                                                                $post       = get_page_by_path( $id, OBJECT, 'product' );
×
836
                                                                $product_id = ! empty( $post ) ? absint( $post->ID ) : 0;
×
837
                                                                break;
×
838
                                                        case 'database_id':
1✔
839
                                                                $product_id = absint( $id );
×
840
                                                                break;
×
841
                                                        case 'global_id':
1✔
842
                                                        default:
843
                                                                $id_components = Relay::fromGlobalId( $id );
1✔
844
                                                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
845
                                                                        throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
×
846
                                                                }
847
                                                                $product_id = absint( $id_components['id'] );
1✔
848
                                                                break;
1✔
849
                                                }
850

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

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

861
                                                $product = get_post( $product_id );
1✔
862
                                                if ( ! is_object( $product ) || 'product' !== $product->post_type ) {
1✔
863
                                                        /* translators: %1$s: ID type, %2$s: ID value */
864
                                                        throw new UserError( sprintf( __( 'No product exists with the %1$s: %2$s', 'wp-graphql-woocommerce' ), $id_type, $product_id ) );
×
865
                                                }
866

867
                                                return Factory::resolve_crud_object( $product_id, $context );
1✔
868
                                        },
155✔
869
                                ]
155✔
870
                        );
155✔
871
                }//end foreach
872
        }
873
}
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