Skip to content

Commit 680f3f2

Browse files
committed
feat: New fees input fields add to cart query and checkout mutation
1 parent 24dede2 commit 680f3f2

File tree

13 files changed

+225
-30
lines changed

13 files changed

+225
-30
lines changed

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/class-type-registry.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function init() {
5757
Type\WPInputObject\Customer_Address_Input::register();
5858
Type\WPInputObject\Product_Attribute_Input::register();
5959
Type\WPInputObject\Tax_Rate_Connection_Orderby_Input::register();
60+
Type\WPInputObject\Fee_Input::register();
6061
Type\WPInputObject\Fee_Line_Input::register();
6162
Type\WPInputObject\Line_Item_Input::register();
6263
Type\WPInputObject\Meta_Data_Input::register();

includes/class-wp-graphql-woocommerce.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ private function includes() {
300300
require $include_directory_path . 'type/input/class-cart-item-quantity-input.php';
301301
require $include_directory_path . 'type/input/class-create-account-input.php';
302302
require $include_directory_path . 'type/input/class-customer-address-input.php';
303+
require $include_directory_path . 'type/input/class-fee-input.php';
303304
require $include_directory_path . 'type/input/class-fee-line-input.php';
304305
require $include_directory_path . 'type/input/class-line-item-input.php';
305306
require $include_directory_path . 'type/input/class-meta-data-input.php';

includes/connection/class-orders.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -285,28 +285,31 @@ public static function get_connection_args( $access = 'public' ): array {
285285

286286
case 'public':
287287
default:
288-
return [
289-
'statuses' => [
290-
'type' => [ 'list_of' => 'OrderStatusEnum' ],
291-
'description' => __( 'Limit result set to orders assigned a specific status.', 'wp-graphql-woocommerce' ),
292-
],
293-
'productId' => [
294-
'type' => 'Int',
295-
'description' => __( 'Limit result set to orders assigned a specific product.', 'wp-graphql-woocommerce' ),
296-
],
297-
'orderby' => [
298-
'type' => [ 'list_of' => 'OrdersOrderbyInput' ],
299-
'description' => __( 'What paramater to use to order the objects by.', 'wp-graphql-woocommerce' ),
300-
],
301-
'search' => [
302-
'type' => 'String',
303-
'description' => __( 'Limit results to those matching a string.', 'wp-graphql-woocommerce' ),
304-
],
305-
'dateQuery' => [
306-
'type' => 'DateQueryInput',
307-
'description' => __( 'Filter the connection based on dates.', 'wp-graphql-woocommerce' ),
308-
],
309-
];
288+
return array_merge(
289+
get_wc_cpt_connection_args(),
290+
[
291+
'statuses' => [
292+
'type' => [ 'list_of' => 'OrderStatusEnum' ],
293+
'description' => __( 'Limit result set to orders assigned a specific status.', 'wp-graphql-woocommerce' ),
294+
],
295+
'productId' => [
296+
'type' => 'Int',
297+
'description' => __( 'Limit result set to orders assigned a specific product.', 'wp-graphql-woocommerce' ),
298+
],
299+
'orderby' => [
300+
'type' => [ 'list_of' => 'OrdersOrderbyInput' ],
301+
'description' => __( 'What paramater to use to order the objects by.', 'wp-graphql-woocommerce' ),
302+
],
303+
'search' => [
304+
'type' => 'String',
305+
'description' => __( 'Limit results to those matching a string.', 'wp-graphql-woocommerce' ),
306+
],
307+
'dateQuery' => [
308+
'type' => 'DateQueryInput',
309+
'description' => __( 'Filter the connection based on dates.', 'wp-graphql-woocommerce' ),
310+
],
311+
]
312+
);
310313
}//end switch
311314
}
312315

includes/data/mutation/class-checkout-mutation.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static function prepare_checkout_args( $input, $context, $info ) {
7373
'ship_to_different_address' => ! empty( $input['shipToDifferentAddress'] ) && ! wc_ship_to_billing_address_only(),
7474
];
7575

76-
$skipped = [];
76+
$skipped = ['fees'];
7777
foreach ( self::get_checkout_fields() as $fieldset_key => $fieldset ) {
7878
if ( self::maybe_skip_fieldset( $fieldset_key, $data ) ) {
7979
$skipped[] = $fieldset_key;
@@ -96,6 +96,31 @@ public static function prepare_checkout_args( $input, $context, $info ) {
9696
}
9797
}//end foreach
9898

99+
if ( ! empty( $input['fees'] ) ) {
100+
$fees = $input['fees'];
101+
add_action(
102+
'woocommerce_cart_calculate_fees',
103+
function () use ( $fees ) {
104+
foreach( $fees as $fee_input ) {
105+
if ( empty( $fee_input['name'] ) || empty( $fee_input['amount'] ) ) {
106+
// TODO: Log invalid fee input.
107+
continue;
108+
}
109+
110+
$fee_args = [
111+
$fee_input['name'],
112+
$fee_input['amount'],
113+
isset( $fee_input['taxable'] ) ? $fee_input['taxable'] : false,
114+
isset( $fee_input['taxClass'] ) ? $fee_input['taxClass'] : '',
115+
];
116+
117+
\WC()->cart->add_fee( ...$fee_args );
118+
}
119+
}
120+
);
121+
122+
}
123+
99124
if ( in_array( 'shipping', $skipped, true ) && ( \WC()->cart->needs_shipping_address() || \wc_ship_to_billing_address_only() ) ) {
100125
foreach ( self::get_checkout_fields( 'shipping' ) as $field => $input_key ) {
101126
$data[ "shipping_{$field}" ] = isset( $data[ "billing_{$field}" ] ) ? $data[ "billing_{$field}" ] : '';

includes/mutation/class-cart-add-fee.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public static function mutate_and_get_payload() {
8888
return static function ( $input, AppContext $context, ResolveInfo $info ) {
8989
Cart_Mutation::check_session_token();
9090

91-
if ( ! current_user_can( 'edit_shop_orders' ) ) {
92-
throw new UserError( __( 'You do not have the appropriate capabilities to perform this action.', 'wp-graphql-woocommerce' ) );
93-
}
91+
// if ( ! current_user_can( 'edit_shop_orders' ) ) {
92+
// throw new UserError( __( 'You do not have the appropriate capabilities to perform this action.', 'wp-graphql-woocommerce' ) );
93+
// }
9494

9595
if ( empty( $input['name'] ) ) {
9696
throw new UserError( __( 'No name provided for fee.', 'wp-graphql-woocommerce' ) );

includes/mutation/class-checkout.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public static function get_input_fields() {
8585
'type' => 'String',
8686
'description' => __( 'Order customer note', 'wp-graphql-woocommerce' ),
8787
],
88+
'fees' => [
89+
'type' => [ 'list_of' => 'FeeInput' ],
90+
'description' => __( 'Fees to add to the order.', 'wp-graphql-woocommerce' ),
91+
]
8892
];
8993
}
9094

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* WPInputObjectType - FeeInput
4+
*
5+
* @package WPGraphQL\WooCommerce\Type\WPInputObject
6+
* @since TBD
7+
*/
8+
9+
namespace WPGraphQL\WooCommerce\Type\WPInputObject;
10+
11+
/**
12+
* Class Fee_Input
13+
*/
14+
class Fee_Input {
15+
/**
16+
* Registers type
17+
*
18+
* @return void
19+
*/
20+
public static function register() {
21+
register_graphql_input_type(
22+
'FeeInput',
23+
[
24+
'description' => __( 'Fee line data.', 'wp-graphql-woocommerce' ),
25+
'fields' => [
26+
'name' => [
27+
'type' => [ 'non_null' => 'String' ],
28+
'description' => __( 'Unique name for the fee.', 'wp-graphql-woocommerce' ),
29+
],
30+
'amount' => [
31+
'type' => 'Float',
32+
'description' => __( 'Fee amount', 'wp-graphql-woocommerce' ),
33+
],
34+
'taxable' => [
35+
'type' => 'Boolean',
36+
'description' => __( 'Is the fee taxable?', 'wp-graphql-woocommerce' ),
37+
],
38+
'taxClass' => [
39+
'type' => 'TaxClassEnum',
40+
'description' => __( 'The tax class for the fee if taxable.', 'wp-graphql-woocommerce' ),
41+
],
42+
],
43+
]
44+
);
45+
}
46+
}

includes/type/object/class-cart-type.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public static function get_cart_fields( $other_fields = [] ) {
243243
'feeTotal' => [
244244
'type' => 'String',
245245
'description' => __( 'Cart fee total', 'wp-graphql-woocommerce' ),
246+
'deprecationReason' => __( 'Always null', 'wp-graphql-woocommerce' ),
246247
'args' => [
247248
'format' => [
248249
'type' => 'PricingFieldFormatEnum',
@@ -262,6 +263,7 @@ public static function get_cart_fields( $other_fields = [] ) {
262263
'feeTax' => [
263264
'type' => 'String',
264265
'description' => __( 'Cart fee tax', 'wp-graphql-woocommerce' ),
266+
'deprecationReason' => __( 'Always null', 'wp-graphql-woocommerce' ),
265267
'args' => [
266268
'format' => [
267269
'type' => 'PricingFieldFormatEnum',
@@ -353,9 +355,10 @@ public static function get_cart_fields( $other_fields = [] ) {
353355
},
354356
],
355357
'fees' => [
356-
'type' => [ 'list_of' => 'CartFee' ],
357-
'description' => __( 'Additional fees on the cart.', 'wp-graphql-woocommerce' ),
358-
'resolve' => static function ( $source ) {
358+
'type' => [ 'list_of' => 'CartFee' ],
359+
'description' => __( 'Additional fees on the cart.', 'wp-graphql-woocommerce' ),
360+
'deprecationReason' => __( 'Always null', 'wp-graphql-woocommerce' ),
361+
'resolve' => static function ( $source ) {
359362
$fees = $source->get_fees();
360363
return ! empty( $fees ) ? array_values( $fees ) : null;
361364
},

includes/type/object/class-root-query.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public static function register_fields() {
3636
'type' => 'Boolean',
3737
'description' => __( 'Should cart totals be recalculated.', 'wp-graphql-woocommerce' ),
3838
],
39+
'fees' => [
40+
'type' => [ 'list_of' => 'FeeInput' ],
41+
'description' => __( 'Fees to add to the cart.', 'wp-graphql-woocommerce' ),
42+
],
3943
],
4044
'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
4145
'resolve' => static function ( $_, $args ) {
@@ -45,6 +49,31 @@ public static function register_fields() {
4549
}
4650

4751
$cart = Factory::resolve_cart();
52+
53+
if ( ! empty( $args['fees'] ) ) {
54+
$fees = $args['fees'];
55+
add_action(
56+
'woocommerce_cart_calculate_fees',
57+
function () use ( $fees ) {
58+
foreach( $fees as $fee_input ) {
59+
if ( empty( $fee_input['name'] ) || empty( $fee_input['amount'] ) ) {
60+
// TODO: Log invalid fee input.
61+
continue;
62+
}
63+
64+
$fee_args = [
65+
$fee_input['name'],
66+
$fee_input['amount'],
67+
isset( $fee_input['taxable'] ) ? $fee_input['taxable'] : false,
68+
isset( $fee_input['taxClass'] ) ? $fee_input['taxClass'] : '',
69+
];
70+
71+
\WC()->cart->add_fee( ...$fee_args );
72+
}
73+
}
74+
);
75+
}
76+
4877
if ( ! empty( $args['recalculateTotals'] ) ) {
4978
$cart->calculate_totals();
5079
}

0 commit comments

Comments
 (0)