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

wp-graphql / wp-graphql-woocommerce / 5945402416

23 Aug 2023 12:19AM UTC coverage: 82.818% (-0.05%) from 82.865%
5945402416

push

github

web-flow
fix: default the customer to the current user (#787)

5 of 5 new or added lines in 1 file covered. (100.0%)

10291 of 12426 relevant lines covered (82.82%)

54.86 hits per line

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

95.71
/includes/mutation/class-customer-update.php
1
<?php
2
/**
3
 * Registers "updateCustomer" mutation
4
 *
5
 * @package WPGraphQL\WooCommerce\Mutation
6
 * @since 0.1.0
7
 */
8

9
namespace WPGraphQL\WooCommerce\Mutation;
10

11
use GraphQL\Error\UserError;
12
use GraphQL\Type\Definition\ResolveInfo;
13
use WC_Customer;
14
use WPGraphQL\AppContext;
15
use WPGraphQL\Mutation\UserCreate;
16
use WPGraphQL\Mutation\UserUpdate;
17
use WPGraphQL\WooCommerce\Data\Mutation\Customer_Mutation;
18
use WPGraphQL\WooCommerce\Model\Customer;
19

20
/**
21
 * Class - Customer_Update
22
 */
23
class Customer_Update {
24
        /**
25
         * Registers mutation
26
         *
27
         * @return void
28
         */
29
        public static function register_mutation() {
30
                register_graphql_mutation(
111✔
31
                        'updateCustomer',
111✔
32
                        [
111✔
33
                                'inputFields'         => self::get_input_fields(),
111✔
34
                                'outputFields'        => self::get_output_fields(),
111✔
35
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
111✔
36
                        ]
111✔
37
                );
111✔
38
        }
39

40
        /**
41
         * Defines the mutation input field configuration
42
         *
43
         * @return array
44
         */
45
        public static function get_input_fields() {
46
                return array_merge(
111✔
47
                        UserCreate::get_input_fields(),
111✔
48
                        [
111✔
49
                                'id'                    => [
111✔
50
                                        'type'        => 'ID',
111✔
51
                                        'description' => __( 'The ID of the user', 'wp-graphql-woocommerce' ),
111✔
52
                                ],
111✔
53
                                'billing'               => [
111✔
54
                                        'type'        => 'CustomerAddressInput',
111✔
55
                                        'description' => __( 'Customer billing information', 'wp-graphql-woocommerce' ),
111✔
56
                                ],
111✔
57
                                'shipping'              => [
111✔
58
                                        'type'        => 'CustomerAddressInput',
111✔
59
                                        'description' => __( 'Customer shipping address', 'wp-graphql-woocommerce' ),
111✔
60
                                ],
111✔
61
                                'shippingSameAsBilling' => [
111✔
62
                                        'type'        => 'Boolean',
111✔
63
                                        'description' => __( 'Customer shipping is identical to billing address', 'wp-graphql-woocommerce' ),
111✔
64
                                ],
111✔
65
                                'metaData'              => [
111✔
66
                                        'description' => __( 'Meta data.', 'wp-graphql-woocommerce' ),
111✔
67
                                        'type'        => [ 'list_of' => 'MetaDataInput' ],
111✔
68
                                ],
111✔
69
                        ]
111✔
70
                );
111✔
71
        }
72

73
        /**
74
         * Defines the mutation output field configuration
75
         *
76
         * @return array
77
         */
78
        public static function get_output_fields() {
79
                return [
111✔
80
                        'customer' => [
111✔
81
                                'type'    => 'Customer',
111✔
82
                                'resolve' => static function ( $payload ) {
111✔
83
                                        return new Customer( $payload['id'] );
4✔
84
                                },
111✔
85
                        ],
111✔
86
                ];
111✔
87
        }
88

89
        /**
90
         * Defines the mutation data modification closure.
91
         *
92
         * @return callable
93
         */
94
        public static function mutate_and_get_payload() {
95
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
111✔
96
                        $session_only = empty( $input['id'] ) && ! is_user_logged_in();
4✔
97
                        $payload      = null;
4✔
98

99
                        if ( ! $session_only ) {
4✔
100
                                // Get closure from "UserRegister::mutate_and_get_payload".
101
                                $update_user = UserUpdate::mutate_and_get_payload();
4✔
102

103
                                if ( ! isset( $input['id'] ) ) {
4✔
104
                                        $input['id'] = get_current_user_id();
1✔
105
                                }
106

107
                                // Update customer with core UserUpdate closure.
108
                                $payload = $update_user( $input, $context, $info );
4✔
109

110
                                if ( empty( $payload ) ) {
4✔
111
                                        throw new UserError( __( 'Failed to update customer.', 'wp-graphql-woocommerce' ) );
×
112
                                }
113
                        }
114

115
                        // Map all of the args from GQL to WC friendly.
116
                        $customer_args = Customer_Mutation::prepare_customer_props( $input, 'update' );
4✔
117

118
                        // Create customer object.
119
                        $customer = ! $session_only ? new WC_Customer( $payload['id'] ) : \WC()->customer;
4✔
120

121
                        // Copy billing address as shipping address.
122
                        if ( isset( $input['shippingSameAsBilling'] ) && $input['shippingSameAsBilling'] ) {
4✔
123
                                $customer_args['shipping'] = array_merge(
1✔
124
                                        Customer_Mutation::empty_shipping(),
1✔
125
                                        array_intersect_key( $customer->get_billing( 'edit' ), Customer_Mutation::empty_shipping() )
1✔
126
                                );
1✔
127
                        }
128

129
                        // Update customer fields.
130
                        foreach ( $customer_args as $prop => $value ) {
4✔
131

132
                                // If field group like 'shipping' or 'billing'.
133
                                if ( ! empty( $value ) && \is_array( $value ) ) {
3✔
134

135
                                        // Check if group field has set function and assigns new value.
136
                                        foreach ( $value as $field => $field_value ) {
3✔
137
                                                if ( is_callable( [ $customer, "set_{$prop}_{$field}" ] ) ) {
3✔
138
                                                        $customer->{"set_{$prop}_{$field}"}( $field_value );
3✔
139
                                                }
140
                                        }
141

142
                                        // If field has set function and assigns new value.
143
                                } elseif ( is_callable( [ $customer, "set_{$prop}" ] ) ) {
×
144
                                        $customer->{"set_{$prop}"}( $value );
×
145
                                }
146
                        }
147

148
                        // Set meta data.
149
                        if ( ! empty( $input['metaData'] ) ) {
4✔
150
                                Customer_Mutation::input_meta_data_mapping( $customer, $input['metaData'] );
1✔
151
                        }
152

153
                        // Save customer and get customer ID.
154
                        $customer->save();
4✔
155

156
                        // Return payload.
157
                        return ! empty( $payload ) ? $payload : [ 'id' => 'session' ];
4✔
158
                };
111✔
159
        }
160
}
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