|
14 | 14 | use WPGraphQL\WooCommerce\Data\Mutation\Customer_Mutation; |
15 | 15 | use WPGraphQL\WooCommerce\Model\Customer; |
16 | 16 | use WPGraphQL\Model\User; |
| 17 | +use WPGraphQL\Mutation\UserCreate; |
17 | 18 | use WPGraphQL\Mutation\UserUpdate; |
18 | 19 |
|
19 | 20 | /** |
@@ -41,8 +42,12 @@ public static function register_mutation() { |
41 | 42 | */ |
42 | 43 | public static function get_input_fields() { |
43 | 44 | $input_fields = array_merge( |
44 | | - UserUpdate::get_input_fields(), |
| 45 | + UserCreate::get_input_fields(), |
45 | 46 | array( |
| 47 | + 'id' => array( |
| 48 | + 'type' => 'ID', |
| 49 | + 'description' => __( 'The ID of the user', 'wp-graphql' ), |
| 50 | + ), |
46 | 51 | 'billing' => array( |
47 | 52 | 'type' => 'CustomerAddressInput', |
48 | 53 | 'description' => __( 'Customer billing information', 'wp-graphql-woocommerce' ), |
@@ -84,21 +89,26 @@ public static function get_output_fields() { |
84 | 89 | */ |
85 | 90 | public static function mutate_and_get_payload() { |
86 | 91 | return function( $input, AppContext $context, ResolveInfo $info ) { |
87 | | - // Get closure from "UserRegister::mutate_and_get_payload". |
88 | | - $update_user = UserUpdate::mutate_and_get_payload(); |
| 92 | + $session_only = empty( $input['id'] ); |
| 93 | + $payload = null; |
| 94 | + |
| 95 | + if ( ! $session_only ) { |
| 96 | + // Get closure from "UserRegister::mutate_and_get_payload". |
| 97 | + $update_user = UserUpdate::mutate_and_get_payload(); |
89 | 98 |
|
90 | | - // Update customer with core UserUpdate closure. |
91 | | - $payload = $update_user( $input, $context, $info ); |
| 99 | + // Update customer with core UserUpdate closure. |
| 100 | + $payload = $update_user( $input, $context, $info ); |
92 | 101 |
|
93 | | - if ( empty( $payload ) ) { |
94 | | - throw new UserError( __( 'Failed to update customer.', 'wp-graphql-woocommerce' ) ); |
| 102 | + if ( empty( $payload ) ) { |
| 103 | + throw new UserError( __( 'Failed to update customer.', 'wp-graphql-woocommerce' ) ); |
| 104 | + } |
95 | 105 | } |
96 | 106 |
|
97 | 107 | // Map all of the args from GQL to WC friendly. |
98 | 108 | $customer_args = Customer_Mutation::prepare_customer_props( $input, 'update' ); |
99 | 109 |
|
100 | 110 | // Create customer object. |
101 | | - $customer = new \WC_Customer( $payload['id'] ); |
| 111 | + $customer = ! $session_only ? new \WC_Customer( $payload['id'] ) : \WC()->customer; |
102 | 112 |
|
103 | 113 | // Set billing address. |
104 | 114 | if ( ! empty( $customer_args['billing'] ) ) { |
@@ -128,7 +138,7 @@ public static function mutate_and_get_payload() { |
128 | 138 | $customer->save(); |
129 | 139 |
|
130 | 140 | // Return payload. |
131 | | - return $payload; |
| 141 | + return ! empty( $payload ) ? $payload : array( 'id' => 'session' ); |
132 | 142 | }; |
133 | 143 | } |
134 | 144 | } |
0 commit comments