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

wp-graphql / wp-graphql-woocommerce / 11714183904

07 Nov 2024 12:23AM UTC coverage: 83.665% (-0.8%) from 84.451%
11714183904

push

github

web-flow
devops: Name officially changed to "WPGraphQL for WooCommerce" (#900)

* devops: Name officially changed to "WPGraphQL for WooCommerce"

* devops: GA workflow environment updated

* devops: GA workflow environment updated

* devops: composer-git-hooks downgraded to "2.8.5"

* devops: Unstable session manager tests skipped

* chore: cleanup applied

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

2 of 8 new or added lines in 5 files covered. (25.0%)

268 existing lines in 20 files now uncovered.

12431 of 14858 relevant lines covered (83.67%)

71.79 hits per line

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

94.53
/includes/type/object/class-cart-type.php
1
<?php
2
/**
3
 * WPObject Type - Cart_Type
4
 *
5
 * Registers Cart WPObject type and queries
6
 *
7
 * @package WPGraphQL\WooCommerce\Type\WPObject
8
 * @since   0.0.3
9
 */
10

11
namespace WPGraphQL\WooCommerce\Type\WPObject;
12

13
use GraphQL\Type\Definition\ResolveInfo;
14
use WPGraphQL\AppContext;
15
use WPGraphQL\WooCommerce\Data\Connection\Cart_Item_Connection_Resolver;
16
use WPGraphQL\WooCommerce\Data\Factory;
17

18
/**
19
 * Class - Cart_Type
20
 */
21
class Cart_Type {
22
        /**
23
         * Register Cart-related types and queries to the WPGraphQL schema
24
         *
25
         * @return void
26
         */
27
        public static function register() {
28
                self::register_cart_fee();
141✔
29
                self::register_cart_tax();
141✔
30
                self::register_applied_coupon();
141✔
31
                self::register_cart();
141✔
32
        }
33

34
        /**
35
         * Returns the "Cart" type fields.
36
         *
37
         * @param array $other_fields Extra fields configs to be added or override the default field definitions.
38
         *
39
         * @return array
40
         */
41
        public static function get_cart_fields( $other_fields = [] ) {
42
                return array_merge(
141✔
43
                        [
141✔
44
                                'subtotal'                 => [
141✔
45
                                        'type'        => 'String',
141✔
46
                                        'description' => __( 'Cart subtotal', 'wp-graphql-woocommerce' ),
141✔
47
                                        'args'        => [
141✔
48
                                                'format' => [
141✔
49
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
50
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
51
                                                ],
141✔
52
                                        ],
141✔
53
                                        'resolve'     => static function ( $source, array $args ) {
141✔
54
                                                $price = ! is_null( $source->get_subtotal() ) ? $source->get_subtotal() : 0;
1✔
55

56
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
57
                                                        return $price;
1✔
58
                                                }
59

60
                                                return wc_graphql_price( $price );
1✔
61
                                        },
141✔
62
                                ],
141✔
63
                                'subtotalTax'              => [
141✔
64
                                        'type'        => 'String',
141✔
65
                                        'description' => __( 'Cart subtotal tax', 'wp-graphql-woocommerce' ),
141✔
66
                                        'args'        => [
141✔
67
                                                'format' => [
141✔
68
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
69
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
70
                                                ],
141✔
71
                                        ],
141✔
72
                                        'resolve'     => static function ( $source, array $args ) {
141✔
73
                                                $price = ! is_null( $source->get_subtotal_tax() ) ? $source->get_subtotal_tax() : 0;
1✔
74

75
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
76
                                                        return $price;
1✔
77
                                                }
78

79
                                                return wc_graphql_price( $price );
1✔
80
                                        },
141✔
81
                                ],
141✔
82
                                'discountTotal'            => [
141✔
83
                                        'type'        => 'String',
141✔
84
                                        'description' => __( 'Cart discount total', 'wp-graphql-woocommerce' ),
141✔
85
                                        'args'        => [
141✔
86
                                                'format' => [
141✔
87
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
88
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
89
                                                ],
141✔
90
                                        ],
141✔
91
                                        'resolve'     => static function ( $source, array $args ) {
141✔
92
                                                $price = ! is_null( $source->get_discount_total() ) ? $source->get_discount_total() : 0;
1✔
93

94
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
95
                                                        return $price;
1✔
96
                                                }
97

98
                                                return wc_graphql_price( $price );
1✔
99
                                        },
141✔
100
                                ],
141✔
101
                                'discountTax'              => [
141✔
102
                                        'type'        => 'String',
141✔
103
                                        'description' => __( 'Cart discount tax', 'wp-graphql-woocommerce' ),
141✔
104
                                        'args'        => [
141✔
105
                                                'format' => [
141✔
106
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
107
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
108
                                                ],
141✔
109
                                        ],
141✔
110
                                        'resolve'     => static function ( $source, array $args ) {
141✔
111
                                                $price = ! is_null( $source->get_discount_tax() ) ? $source->get_discount_tax() : 0;
1✔
112

113
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
114
                                                        return $price;
1✔
115
                                                }
116

117
                                                return wc_graphql_price( $price );
1✔
118
                                        },
141✔
119
                                ],
141✔
120
                                'availableShippingMethods' => [
141✔
121
                                        'type'        => [ 'list_of' => 'ShippingPackage' ],
141✔
122
                                        'description' => __( 'Available shipping methods for this order.', 'wp-graphql-woocommerce' ),
141✔
123
                                        'resolve'     => static function ( $source ) {
141✔
124
                                                $packages = [];
×
125

126
                                                $available_packages = $source->needs_shipping()
×
127
                                                        ? \WC()->shipping()->calculate_shipping( $source->get_shipping_packages() )
×
128
                                                        : [];
×
129

130
                                                foreach ( $available_packages as $index => $package ) {
×
131
                                                        $package['index'] = $index;
×
132
                                                        $packages[]       = $package;
×
133
                                                }
134

135
                                                return $packages;
×
136
                                        },
141✔
137
                                ],
141✔
138
                                'chosenShippingMethods'    => [
141✔
139
                                        'type'        => [ 'list_of' => 'String' ],
141✔
140
                                        'description' => __( 'Shipping method chosen for this order.', 'wp-graphql-woocommerce' ),
141✔
141
                                        'resolve'     => static function ( $source ) {
141✔
142
                                                $chosen_shipping_methods = [];
1✔
143

144
                                                $available_packages = $source->needs_shipping()
1✔
145
                                                        ? \WC()->shipping()->calculate_shipping( $source->get_shipping_packages() )
1✔
UNCOV
146
                                                        : [];
×
147

148
                                                foreach ( $available_packages as $i => $package ) {
1✔
149
                                                        if ( isset( \WC()->session->chosen_shipping_methods[ $i ] ) ) {
1✔
150
                                                                $chosen_shipping_methods[] = \WC()->session->chosen_shipping_methods[ $i ];
1✔
151
                                                        }
152
                                                }
153

154
                                                return $chosen_shipping_methods;
1✔
155
                                        },
141✔
156
                                ],
141✔
157
                                'shippingTotal'            => [
141✔
158
                                        'type'        => 'String',
141✔
159
                                        'description' => __( 'Cart shipping total', 'wp-graphql-woocommerce' ),
141✔
160
                                        'args'        => [
141✔
161
                                                'format' => [
141✔
162
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
163
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
164
                                                ],
141✔
165
                                        ],
141✔
166
                                        'resolve'     => static function ( $source, array $args ) {
141✔
167
                                                $price = ! is_null( $source->get_shipping_total() ) ? $source->get_shipping_total() : 0;
1✔
168

169
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
170
                                                        return $price;
1✔
171
                                                }
172

173
                                                return wc_graphql_price( $price );
1✔
174
                                        },
141✔
175
                                ],
141✔
176
                                'shippingTax'              => [
141✔
177
                                        'type'        => 'String',
141✔
178
                                        'description' => __( 'Cart shipping tax', 'wp-graphql-woocommerce' ),
141✔
179
                                        'args'        => [
141✔
180
                                                'format' => [
141✔
181
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
182
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
183
                                                ],
141✔
184
                                        ],
141✔
185
                                        'resolve'     => static function ( $source, array $args ) {
141✔
186
                                                $price = ! is_null( $source->get_shipping_tax() ) ? $source->get_shipping_tax() : 0;
1✔
187

188
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
189
                                                        return $price;
1✔
190
                                                }
191

192
                                                return wc_graphql_price( $price );
1✔
193
                                        },
141✔
194
                                ],
141✔
195
                                'contentsTotal'            => [
141✔
196
                                        'type'        => 'String',
141✔
197
                                        'description' => __( 'Cart contents total', 'wp-graphql-woocommerce' ),
141✔
198
                                        'args'        => [
141✔
199
                                                'format' => [
141✔
200
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
201
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
202
                                                ],
141✔
203
                                        ],
141✔
204
                                        'resolve'     => static function ( $source, array $args ) {
141✔
205
                                                if ( $source->display_prices_including_tax() ) {
1✔
206
                                                        $cart_subtotal = $source->get_subtotal() + $source->get_subtotal_tax();
×
207
                                                } else {
208
                                                        $cart_subtotal = $source->get_subtotal();
1✔
209
                                                }
210

211
                                                $price = ! is_null( $cart_subtotal )
1✔
212
                                                        ? $cart_subtotal
1✔
UNCOV
213
                                                        : 0;
×
214

215
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
216
                                                        return $price;
1✔
217
                                                }
218

219
                                                return wc_graphql_price( $price );
1✔
220
                                        },
141✔
221
                                ],
141✔
222
                                'contentsTax'              => [
141✔
223
                                        'type'        => 'String',
141✔
224
                                        'description' => __( 'Cart contents tax', 'wp-graphql-woocommerce' ),
141✔
225
                                        'args'        => [
141✔
226
                                                'format' => [
141✔
227
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
228
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
229
                                                ],
141✔
230
                                        ],
141✔
231
                                        'resolve'     => static function ( $source, array $args ) {
141✔
232
                                                $price = ! is_null( $source->get_cart_contents_tax() )
1✔
233
                                                        ? $source->get_cart_contents_tax()
1✔
UNCOV
234
                                                        : 0;
×
235

236
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
237
                                                        return $price;
1✔
238
                                                }
239

240
                                                return wc_graphql_price( $price );
1✔
241
                                        },
141✔
242
                                ],
141✔
243
                                'feeTotal'                 => [
141✔
244
                                        'type'        => 'String',
141✔
245
                                        'description' => __( 'Cart fee total', 'wp-graphql-woocommerce' ),
141✔
246
                                        'args'        => [
141✔
247
                                                'format' => [
141✔
248
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
249
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
250
                                                ],
141✔
251
                                        ],
141✔
252
                                        'resolve'     => static function ( $source, array $args ) {
141✔
253
                                                $price = ! is_null( $source->get_fee_total() ) ? $source->get_fee_total() : 0;
1✔
254

255
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
256
                                                        return $price;
1✔
257
                                                }
258

259
                                                return wc_graphql_price( $price );
1✔
260
                                        },
141✔
261
                                ],
141✔
262
                                'feeTax'                   => [
141✔
263
                                        'type'        => 'String',
141✔
264
                                        'description' => __( 'Cart fee tax', 'wp-graphql-woocommerce' ),
141✔
265
                                        'args'        => [
141✔
266
                                                'format' => [
141✔
267
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
268
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
269
                                                ],
141✔
270
                                        ],
141✔
271
                                        'resolve'     => static function ( $source, array $args ) {
141✔
272
                                                $price = ! is_null( $source->get_fee_tax() ) ? $source->get_fee_tax() : 0;
1✔
273

274
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
275
                                                        return $price;
1✔
276
                                                }
277

278
                                                return wc_graphql_price( $price );
1✔
279
                                        },
141✔
280
                                ],
141✔
281
                                'total'                    => [
141✔
282
                                        'type'        => 'String',
141✔
283
                                        'description' => __( 'Cart total after calculation', 'wp-graphql-woocommerce' ),
141✔
284
                                        'args'        => [
141✔
285
                                                'format' => [
141✔
286
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
287
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
288
                                                ],
141✔
289
                                        ],
141✔
290
                                        'resolve'     => static function ( $source, array $args ) {
141✔
291
                                                $source->calculate_totals();
5✔
292
                                                $price = isset( $source->get_totals()['total'] )
5✔
293
                                                        ? apply_filters( 'graphql_woocommerce_cart_get_total', $source->get_totals()['total'] )
5✔
UNCOV
294
                                                        : 0;
×
295

296
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
5✔
297
                                                        return $price;
1✔
298
                                                }
299

300
                                                return wc_graphql_price( $price );
5✔
301
                                        },
141✔
302
                                ],
141✔
303
                                'totalTax'                 => [
141✔
304
                                        'type'        => 'String',
141✔
305
                                        'description' => __( 'Cart total tax amount', 'wp-graphql-woocommerce' ),
141✔
306
                                        'args'        => [
141✔
307
                                                'format' => [
141✔
308
                                                        'type'        => 'PricingFieldFormatEnum',
141✔
309
                                                        'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
310
                                                ],
141✔
311
                                        ],
141✔
312
                                        'resolve'     => static function ( $source, array $args ) {
141✔
313
                                                $price = ! is_null( $source->get_total_tax() ) ? $source->get_total_tax() : 0;
1✔
314

315
                                                if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
316
                                                        return $price;
1✔
317
                                                }
318

319
                                                return wc_graphql_price( $price );
1✔
320
                                        },
141✔
321
                                ],
141✔
322
                                'totalTaxes'               => [
141✔
323
                                        'type'        => [ 'list_of' => 'CartTax' ],
141✔
324
                                        'description' => __( 'Cart total taxes itemized', 'wp-graphql-woocommerce' ),
141✔
325
                                        'resolve'     => static function ( $source ) {
141✔
326
                                                $taxes = $source->get_tax_totals();
1✔
327
                                                return ! empty( $taxes ) ? array_values( $taxes ) : null;
1✔
328
                                        },
141✔
329
                                ],
141✔
330
                                'isEmpty'                  => [
141✔
331
                                        'type'        => 'Boolean',
141✔
332
                                        'description' => __( 'Is cart empty', 'wp-graphql-woocommerce' ),
141✔
333
                                        'resolve'     => static function ( $source ) {
141✔
334
                                                return ! is_null( $source->is_empty() ) ? $source->is_empty() : null;
1✔
335
                                        },
141✔
336
                                ],
141✔
337
                                'displayPricesIncludeTax'  => [
141✔
338
                                        'type'        => 'Boolean',
141✔
339
                                        'description' => __( 'Do display prices include taxes', 'wp-graphql-woocommerce' ),
141✔
340
                                        'resolve'     => static function ( $source ) {
141✔
341
                                                return ! is_null( $source->display_prices_including_tax() )
1✔
342
                                                        ? $source->display_prices_including_tax()
1✔
343
                                                        : null;
1✔
344
                                        },
141✔
345
                                ],
141✔
346
                                'needsShippingAddress'     => [
141✔
347
                                        'type'        => 'Boolean',
141✔
348
                                        'description' => __( 'Is customer shipping address needed', 'wp-graphql-woocommerce' ),
141✔
349
                                        'resolve'     => static function ( $source ) {
141✔
350
                                                return ! is_null( $source->needs_shipping_address() )
1✔
351
                                                        ? $source->needs_shipping_address()
1✔
352
                                                        : null;
1✔
353
                                        },
141✔
354
                                ],
141✔
355
                                'fees'                     => [
141✔
356
                                        'type'        => [ 'list_of' => 'CartFee' ],
141✔
357
                                        'description' => __( 'Additional fees on the cart.', 'wp-graphql-woocommerce' ),
141✔
358
                                        'resolve'     => static function ( $source ) {
141✔
359
                                                $fees = $source->get_fees();
1✔
360
                                                return ! empty( $fees ) ? array_values( $fees ) : null;
1✔
361
                                        },
141✔
362
                                ],
141✔
363
                                'appliedCoupons'           => [
141✔
364
                                        'type'        => [ 'list_of' => 'AppliedCoupon' ],
141✔
365
                                        'description' => __( 'Coupons applied to the cart', 'wp-graphql-woocommerce' ),
141✔
366
                                        'resolve'     => static function ( $source ) {
141✔
367
                                                $applied_coupons = $source->get_applied_coupons();
3✔
368

369
                                                return ! empty( $applied_coupons ) ? $applied_coupons : null;
3✔
370
                                        },
141✔
371
                                ],
141✔
372
                        ],
141✔
373
                        $other_fields
141✔
374
                );
141✔
375
        }
376

377
        /**
378
         * Returns the "Cart" type connections.
379
         *
380
         * @param array $other_connections Extra connections configs to be added or override the default connection definitions.
381
         * @return array
382
         */
383
        public static function get_cart_connections( $other_connections = [] ) {
384
                return array_merge(
141✔
385
                        [
141✔
386
                                'contents' => [
141✔
387
                                        'toType'           => 'CartItem',
141✔
388
                                        'connectionArgs'   => [
141✔
389
                                                'needsShipping' => [
141✔
390
                                                        'type'        => 'Boolean',
141✔
391
                                                        'description' => __( 'Limit results to cart items that require shipping', 'wp-graphql-woocommerce' ),
141✔
392
                                                ],
141✔
393
                                        ],
141✔
394
                                        'connectionFields' => [
141✔
395
                                                'itemCount'    => [
141✔
396
                                                        'type'        => 'Int',
141✔
397
                                                        'description' => __( 'Total number of items in the cart.', 'wp-graphql-woocommerce' ),
141✔
398
                                                        'resolve'     => static function ( $source ) {
141✔
399
                                                                if ( empty( $source['edges'] ) ) {
1✔
400
                                                                        return 0;
×
401
                                                                }
402

403
                                                                $items = array_values( $source['edges'][0]['source']->get_cart() );
1✔
404
                                                                if ( empty( $items ) ) {
1✔
405
                                                                        return 0;
×
406
                                                                }
407

408
                                                                return array_sum( array_column( $items, 'quantity' ) );
1✔
409
                                                        },
141✔
410
                                                ],
141✔
411
                                                'productCount' => [
141✔
412
                                                        'type'        => 'Int',
141✔
413
                                                        'description' => __( 'Total number of different products in the cart', 'wp-graphql-woocommerce' ),
141✔
414
                                                        'resolve'     => static function ( $source ) {
141✔
415
                                                                if ( empty( $source['edges'] ) ) {
1✔
416
                                                                        return 0;
×
417
                                                                }
418

419
                                                                return count( array_values( $source['edges'][0]['source']->get_cart() ) );
1✔
420
                                                        },
141✔
421
                                                ],
141✔
422
                                        ],
141✔
423
                                        'resolve'          => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
141✔
424
                                                $resolver = new Cart_Item_Connection_Resolver( $source, $args, $context, $info );
11✔
425

426
                                                return $resolver->get_connection();
11✔
427
                                        },
141✔
428
                                ],
141✔
429
                        ],
141✔
430
                        $other_connections
141✔
431
                );
141✔
432
        }
433

434
        /**
435
         * Registers Cart type
436
         *
437
         * @return void
438
         */
439
        public static function register_cart() {
440
                register_graphql_object_type(
141✔
441
                        'Cart',
141✔
442
                        [
141✔
443
                                'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
141✔
444
                                /**
445
                                 * Allows for a decisive filtering of the cart fields.
446
                                 * Note: Only use if deregisteration or renaming the field(s) has failed.
447
                                 *
448
                                 * @param array $fields  Cart field definitions.
449
                                 * @return array
450
                                 */
451
                                'fields'      => apply_filters( 'woographql_cart_field_definitions', self::get_cart_fields() ),
141✔
452
                                /**
453
                                 * Allows for a decisive filtering of the cart connections.
454
                                 * Note: Only use if deregisteration or renaming the connection(s) has failed.
455
                                 *
456
                                 * @param array $connections  Cart connection definitions.
457
                                 * @return array
458
                                 */
459
                                'connections' => apply_filters( 'woographql_cart_connection_definitions', self::get_cart_connections() ),
141✔
460
                        ]
141✔
461
                );
141✔
462
        }
463

464
        /**
465
         * Registers CartFee type
466
         *
467
         * @return void
468
         */
469
        public static function register_cart_fee() {
470
                register_graphql_object_type(
141✔
471
                        'CartFee',
141✔
472
                        [
141✔
473
                                'description' => __( 'An additional fee', 'wp-graphql-woocommerce' ),
141✔
474
                                'fields'      => [
141✔
475
                                        'id'       => [
141✔
476
                                                'type'        => [ 'non_null' => 'ID' ],
141✔
477
                                                'description' => __( 'Fee ID', 'wp-graphql-woocommerce' ),
141✔
478
                                                'resolve'     => static function ( $source ) {
141✔
479
                                                        return ! empty( $source->id ) ? $source->id : null;
3✔
480
                                                },
141✔
481
                                        ],
141✔
482
                                        'name'     => [
141✔
483
                                                'type'        => [ 'non_null' => 'String' ],
141✔
484
                                                'description' => __( 'Fee name', 'wp-graphql-woocommerce' ),
141✔
485
                                                'resolve'     => static function ( $source ) {
141✔
486
                                                        return ! empty( $source->name ) ? $source->name : null;
2✔
487
                                                },
141✔
488
                                        ],
141✔
489
                                        'taxClass' => [
141✔
490
                                                'type'        => 'TaxClassEnum',
141✔
491
                                                'description' => __( 'Fee tax class', 'wp-graphql-woocommerce' ),
141✔
492
                                                'resolve'     => static function ( $source ) {
141✔
493
                                                        return ! empty( $source->tax_class ) ? $source->tax_class : null;
2✔
494
                                                },
141✔
495
                                        ],
141✔
496
                                        'taxable'  => [
141✔
497
                                                'type'        => 'Boolean',
141✔
498
                                                'description' => __( 'Is fee taxable?', 'wp-graphql-woocommerce' ),
141✔
499
                                                'resolve'     => static function ( $source ) {
141✔
500
                                                        return ! is_null( $source->taxable ) ? $source->taxable : null;
2✔
501
                                                },
141✔
502
                                        ],
141✔
503
                                        'amount'   => [
141✔
504
                                                'type'        => 'Float',
141✔
505
                                                'description' => __( 'Fee amount', 'wp-graphql-woocommerce' ),
141✔
506
                                                'resolve'     => static function ( $source ) {
141✔
507
                                                        return ! is_null( $source->amount ) ? $source->amount : 0;
2✔
508
                                                },
141✔
509
                                        ],
141✔
510
                                        'total'    => [
141✔
511
                                                'type'        => 'Float',
141✔
512
                                                'description' => __( 'Fee total', 'wp-graphql-woocommerce' ),
141✔
513
                                                'resolve'     => static function ( $source ) {
141✔
514
                                                        return ! is_null( $source->total ) ? $source->total : 0;
2✔
515
                                                },
141✔
516
                                        ],
141✔
517
                                ],
141✔
518
                        ]
141✔
519
                );
141✔
520
        }
521

522
        /**
523
         * Registers CartTax type
524
         *
525
         * @return void
526
         */
527
        public static function register_cart_tax() {
528
                register_graphql_object_type(
141✔
529
                        'CartTax',
141✔
530
                        [
141✔
531
                                'description' => __( 'An itemized cart tax item', 'wp-graphql-woocommerce' ),
141✔
532
                                'fields'      => [
141✔
533
                                        'id'         => [
141✔
534
                                                'type'        => [ 'non_null' => 'ID' ],
141✔
535
                                                'description' => __( 'Tax Rate ID', 'wp-graphql-woocommerce' ),
141✔
536
                                                'resolve'     => static function ( $source ) {
141✔
537
                                                        return ! empty( $source->tax_rate_id ) ? $source->tax_rate_id : null;
×
538
                                                },
141✔
539
                                        ],
141✔
540
                                        'label'      => [
141✔
541
                                                'type'        => [ 'non_null' => 'String' ],
141✔
542
                                                'description' => __( 'Tax label', 'wp-graphql-woocommerce' ),
141✔
543
                                                'resolve'     => static function ( $source ) {
141✔
544
                                                        return ! empty( $source->label ) ? $source->label : null;
×
545
                                                },
141✔
546
                                        ],
141✔
547
                                        'isCompound' => [
141✔
548
                                                'type'        => 'Boolean',
141✔
549
                                                'description' => __( 'Is tax compound?', 'wp-graphql-woocommerce' ),
141✔
550
                                                'resolve'     => static function ( $source ) {
141✔
551
                                                        return ! empty( $source->is_compound ) ? $source->is_compound : null;
×
552
                                                },
141✔
553
                                        ],
141✔
554
                                        'amount'     => [
141✔
555
                                                'type'        => 'String',
141✔
556
                                                'description' => __( 'Tax amount', 'wp-graphql-woocommerce' ),
141✔
557
                                                'args'        => [
141✔
558
                                                        'format' => [
141✔
559
                                                                'type'        => 'PricingFieldFormatEnum',
141✔
560
                                                                'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
561
                                                        ],
141✔
562
                                                ],
141✔
563
                                                'resolve'     => static function ( $source, array $args ) {
141✔
564
                                                        $amount = ! empty( $source->amount ) ? $source->amount : null;
×
565

566
                                                        if ( ! $amount ) {
×
567
                                                                return null;
×
568
                                                        }
569

570
                                                        if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
×
571
                                                                return $amount;
×
572
                                                        }
573

574
                                                        return wc_graphql_price( $amount );
×
575
                                                },
141✔
576
                                        ],
141✔
577
                                ],
141✔
578
                        ]
141✔
579
                );
141✔
580
        }
581

582
        /**
583
         * Registers AppliedCoupon type
584
         *
585
         * @return void
586
         */
587
        public static function register_applied_coupon() {
588
                register_graphql_object_type(
141✔
589
                        'AppliedCoupon',
141✔
590
                        [
141✔
591
                                'description' => __( 'Coupon applied to the shopping cart.', 'wp-graphql-woocommerce' ),
141✔
592
                                'fields'      => [
141✔
593
                                        'code'           => [
141✔
594
                                                'type'        => [ 'non_null' => 'String' ],
141✔
595
                                                'description' => __( 'Coupon code', 'wp-graphql-woocommerce' ),
141✔
596
                                                'resolve'     => static function ( $source ) {
141✔
597
                                                        return $source;
2✔
598
                                                },
141✔
599
                                        ],
141✔
600
                                        'discountAmount' => [
141✔
601
                                                'type'        => [ 'non_null' => 'String' ],
141✔
602
                                                'args'        => [
141✔
603
                                                        'excludeTax' => [
141✔
604
                                                                'type'        => 'Boolean',
141✔
605
                                                                'description' => __( 'Exclude Taxes (Default "true")', 'wp-graphql-woocommerce' ),
141✔
606
                                                        ],
141✔
607
                                                        'format'     => [
141✔
608
                                                                'type'        => 'PricingFieldFormatEnum',
141✔
609
                                                                'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
610
                                                        ],
141✔
611
                                                ],
141✔
612
                                                'description' => __( 'Discount applied with this coupon', 'wp-graphql-woocommerce' ),
141✔
613
                                                'resolve'     => static function ( $source, array $args ) {
141✔
614
                                                        $ex_tax = ! empty( $args['excludeTax'] ) ? $args['excludeTax'] : true;
1✔
615
                                                        $amount = Factory::resolve_cart()->get_coupon_discount_amount( $source, $ex_tax );
1✔
616

617
                                                        if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
618
                                                                return $amount;
×
619
                                                        }
620

621
                                                        return wc_graphql_price( $amount );
1✔
622
                                                },
141✔
623
                                        ],
141✔
624
                                        'discountTax'    => [
141✔
625
                                                'type'        => [ 'non_null' => 'String' ],
141✔
626
                                                'description' => __( 'Taxes on discount applied with this coupon', 'wp-graphql-woocommerce' ),
141✔
627
                                                'args'        => [
141✔
628
                                                        'format' => [
141✔
629
                                                                'type'        => 'PricingFieldFormatEnum',
141✔
630
                                                                'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
141✔
631
                                                        ],
141✔
632
                                                ],
141✔
633
                                                'resolve'     => static function ( $source, array $args ) {
141✔
634
                                                        $tax = Factory::resolve_cart()->get_coupon_discount_tax_amount( $source );
1✔
635

636
                                                        if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
1✔
637
                                                                return $tax;
×
638
                                                        }
639

640
                                                        return wc_graphql_price( $tax );
1✔
641
                                                },
141✔
642
                                        ],
141✔
643
                                        'description'    => [
141✔
644
                                                'type'        => 'String',
141✔
645
                                                'description' => __( 'Description of applied coupon', 'wp-graphql-woocommerce' ),
141✔
646
                                                'resolve'     => static function ( $source ) {
141✔
647
                                                        $coupon = new \WC_Coupon( $source );
1✔
648
                                                        return $coupon->get_description();
1✔
649
                                                },
141✔
650
                                        ],
141✔
651
                                ],
141✔
652
                        ]
141✔
653
                );
141✔
654
        }
655
}
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