2019-05-10 20:56:13 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Registers "updateCustomer" mutation
|
|
|
|
|
*
|
2019-10-25 19:13:36 -04:00
|
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
2019-05-10 20:56:13 -04:00
|
|
|
* @since 0.1.0
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-25 19:13:36 -04:00
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
2019-05-10 20:56:13 -04:00
|
|
|
|
|
|
|
|
use GraphQL\Error\UserError;
|
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
2023-07-19 23:01:50 +03:00
|
|
|
use WC_Customer;
|
2019-05-10 20:56:13 -04:00
|
|
|
use WPGraphQL\AppContext;
|
2019-12-05 19:56:50 -05:00
|
|
|
use WPGraphQL\Mutation\UserCreate;
|
2019-07-11 18:08:02 -04:00
|
|
|
use WPGraphQL\Mutation\UserUpdate;
|
2023-07-19 23:01:50 +03:00
|
|
|
use WPGraphQL\WooCommerce\Data\Mutation\Customer_Mutation;
|
|
|
|
|
use WPGraphQL\WooCommerce\Model\Customer;
|
2019-05-10 20:56:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class - Customer_Update
|
|
|
|
|
*/
|
|
|
|
|
class Customer_Update {
|
|
|
|
|
/**
|
|
|
|
|
* Registers mutation
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @return void
|
2019-05-10 20:56:13 -04:00
|
|
|
*/
|
|
|
|
|
public static function register_mutation() {
|
|
|
|
|
register_graphql_mutation(
|
|
|
|
|
'updateCustomer',
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2019-05-10 20:56:13 -04:00
|
|
|
'inputFields' => self::get_input_fields(),
|
|
|
|
|
'outputFields' => self::get_output_fields(),
|
|
|
|
|
'mutateAndGetPayload' => self::mutate_and_get_payload(),
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2019-05-10 20:56:13 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation input field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_input_fields() {
|
2020-02-13 22:04:11 -06:00
|
|
|
return array_merge(
|
2019-12-05 19:56:50 -05:00
|
|
|
UserCreate::get_input_fields(),
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
|
|
|
|
'id' => [
|
2019-12-05 19:56:50 -05:00
|
|
|
'type' => 'ID',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'The ID of the user', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'billing' => [
|
2019-05-10 20:56:13 -04:00
|
|
|
'type' => 'CustomerAddressInput',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Customer billing information', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'shipping' => [
|
2019-05-10 20:56:13 -04:00
|
|
|
'type' => 'CustomerAddressInput',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Customer shipping address', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'shippingSameAsBilling' => [
|
2019-05-10 20:56:13 -04:00
|
|
|
'type' => 'Boolean',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Customer shipping is identical to billing address', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'metaData' => [
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Meta data.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
'type' => [ 'list_of' => 'MetaDataInput' ],
|
|
|
|
|
],
|
2025-02-27 10:22:17 -05:00
|
|
|
'isSession' => [
|
|
|
|
|
'type' => 'Boolean',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Whether to save changes on the session or in the database', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2025-02-27 10:22:17 -05:00
|
|
|
],
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2019-05-10 20:56:13 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation output field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_output_fields() {
|
2022-08-26 14:53:36 -04:00
|
|
|
return [
|
|
|
|
|
'customer' => [
|
2019-05-10 21:08:01 -04:00
|
|
|
'type' => 'Customer',
|
2023-07-19 22:56:42 +03:00
|
|
|
'resolve' => static function ( $payload ) {
|
2019-05-10 21:08:01 -04:00
|
|
|
return new Customer( $payload['id'] );
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
];
|
2019-05-10 20:56:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation data modification closure.
|
|
|
|
|
*
|
|
|
|
|
* @return callable
|
|
|
|
|
*/
|
|
|
|
|
public static function mutate_and_get_payload() {
|
2023-07-20 00:11:25 +03:00
|
|
|
return static function ( $input, AppContext $context, ResolveInfo $info ) {
|
2025-02-27 10:22:17 -05:00
|
|
|
$is_session = isset( $input['isSession'] ) ? $input['isSession'] : false;
|
2023-08-22 20:18:47 -04:00
|
|
|
$session_only = empty( $input['id'] ) && ! is_user_logged_in();
|
2019-12-05 19:56:50 -05:00
|
|
|
$payload = null;
|
2019-05-10 20:56:13 -04:00
|
|
|
|
2019-12-05 19:56:50 -05:00
|
|
|
if ( ! $session_only ) {
|
|
|
|
|
// Get closure from "UserRegister::mutate_and_get_payload".
|
|
|
|
|
$update_user = UserUpdate::mutate_and_get_payload();
|
2019-05-10 20:56:13 -04:00
|
|
|
|
2023-08-22 20:18:47 -04:00
|
|
|
if ( ! isset( $input['id'] ) ) {
|
|
|
|
|
$input['id'] = get_current_user_id();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 19:56:50 -05:00
|
|
|
// Update customer with core UserUpdate closure.
|
|
|
|
|
$payload = $update_user( $input, $context, $info );
|
|
|
|
|
|
|
|
|
|
if ( empty( $payload ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Failed to update customer.', 'graphql-for-ecommerce' ) );
|
2019-12-05 19:56:50 -05:00
|
|
|
}
|
2019-05-10 20:56:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Map all of the args from GQL to WC friendly.
|
|
|
|
|
$customer_args = Customer_Mutation::prepare_customer_props( $input, 'update' );
|
|
|
|
|
|
|
|
|
|
// Create customer object.
|
2025-02-27 10:22:17 -05:00
|
|
|
$customer = ! $session_only ? new WC_Customer( $payload['id'], $is_session ) : \WC()->customer;
|
2019-05-10 20:56:13 -04:00
|
|
|
|
|
|
|
|
// Copy billing address as shipping address.
|
2023-06-13 23:17:02 +03:00
|
|
|
if ( isset( $input['shippingSameAsBilling'] ) && $input['shippingSameAsBilling'] ) {
|
2019-05-10 20:56:13 -04:00
|
|
|
$customer_args['shipping'] = array_merge(
|
|
|
|
|
Customer_Mutation::empty_shipping(),
|
|
|
|
|
array_intersect_key( $customer->get_billing( 'edit' ), Customer_Mutation::empty_shipping() )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 20:06:21 -04:00
|
|
|
// Update customer fields.
|
|
|
|
|
foreach ( $customer_args as $prop => $value ) {
|
|
|
|
|
|
|
|
|
|
// If field group like 'shipping' or 'billing'.
|
|
|
|
|
if ( ! empty( $value ) && \is_array( $value ) ) {
|
|
|
|
|
|
|
|
|
|
// Check if group field has set function and assigns new value.
|
|
|
|
|
foreach ( $value as $field => $field_value ) {
|
2022-08-26 14:53:36 -04:00
|
|
|
if ( is_callable( [ $customer, "set_{$prop}_{$field}" ] ) ) {
|
2020-04-30 20:06:21 -04:00
|
|
|
$customer->{"set_{$prop}_{$field}"}( $field_value );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 22:41:43 -05:00
|
|
|
// If field has set function and assigns new value.
|
2022-08-26 14:53:36 -04:00
|
|
|
} elseif ( is_callable( [ $customer, "set_{$prop}" ] ) ) {
|
2020-04-30 20:06:21 -04:00
|
|
|
$customer->{"set_{$prop}"}( $value );
|
2019-05-10 20:56:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 14:02:06 -05:00
|
|
|
// Set meta data.
|
2020-12-09 19:58:40 +01:00
|
|
|
if ( ! empty( $input['metaData'] ) ) {
|
2021-01-27 14:02:06 -05:00
|
|
|
Customer_Mutation::input_meta_data_mapping( $customer, $input['metaData'] );
|
2020-12-09 19:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-10 20:56:13 -04:00
|
|
|
// Save customer and get customer ID.
|
|
|
|
|
$customer->save();
|
|
|
|
|
|
2024-08-07 12:39:45 -04:00
|
|
|
if ( $session_only ) {
|
|
|
|
|
do_action( 'woographql_update_session', true );
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 20:56:13 -04:00
|
|
|
// Return payload.
|
2022-08-26 14:53:36 -04:00
|
|
|
return ! empty( $payload ) ? $payload : [ 'id' => 'session' ];
|
2019-05-10 20:56:13 -04:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|