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

wp-graphql / wp-graphql-woocommerce / 5945400372

23 Aug 2023 12:18AM UTC coverage: 82.817% (-0.05%) from 82.865%
5945400372

push

github

web-flow
fix: WP User core field support fixed in `updateCustomer` mutation (#789)

* devops: CustomerMutationsTest refactored heavily

* fix: `updateCustomer` mutation patched

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

10290 of 12425 relevant lines covered (82.82%)

54.85 hits per line

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

86.36
/includes/data/mutation/class-customer-mutation.php
1
<?php
2
/**
3
 * Defines functions for processing customer mutations.
4
 *
5
 * @package WPGraphQL\WooCommerce\Data\Mutation
6
 * @since 0.1.0
7
 */
8

9
namespace WPGraphQL\WooCommerce\Data\Mutation;
10

11
/**
12
 * Class Customer_Mutation
13
 */
14
class Customer_Mutation {
15
        /**
16
         * Maps the GraphQL input to a format that the used by WooCommerce's WC_Customer class
17
         *
18
         * @param array  $input    Data coming from the GraphQL mutation query input.
19
         * @param string $mutation Mutation being performed.
20
         *
21
         * @access public
22
         * @return array
23
         */
24
        public static function prepare_customer_props( $input, $mutation ) {
25
                $customer_args = [];
9✔
26

27
                if ( ! empty( $input['billing'] ) ) {
9✔
28
                        $customer_args['billing'] = self::address_input_mapping( $input['billing'], 'billing' );
6✔
29
                }
30

31
                if ( ! empty( $input['shipping'] ) ) {
9✔
32
                        $customer_args['shipping'] = self::address_input_mapping( $input['shipping'], 'shipping' );
3✔
33
                }
34

35
                /**
36
                 * Filters the mappings for input to arguments
37
                 *
38
                 * @var array  $customer_args The arguments to ultimately be passed to the WC_Customer::set_props function
39
                 * @var array  $input         Input data from the GraphQL mutation
40
                 * @var string $mutation      What customer mutation is being performed for context
41
                 */
42
                $customer_args = apply_filters( 'woocommerce_new_customer_data', $customer_args, $input, $mutation ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
9✔
43

44
                return $customer_args;
9✔
45
        }
46

47
        /**
48
         * Formats CustomerAddressInput into a address object to be used by WC_Customer object
49
         *
50
         * @param array  $input Customer address input.
51
         * @param string $type  Address type.
52
         *
53
         * @return array;
54
         */
55
        public static function address_input_mapping( $input, $type = 'billing' ) {
56
                // Map GQL input to address props array.
57
                $key_mapping = [
10✔
58
                        'firstName' => 'first_name',
10✔
59
                        'lastName'  => 'last_name',
10✔
60
                        'address1'  => 'address_1',
10✔
61
                        'address2'  => 'address_2',
10✔
62
                ];
10✔
63

64
                $skip = apply_filters( 'graphql_woocommerce_customer_address_input_mapping_skipped', [ 'overwrite' ] );
10✔
65

66
                $type    = 'empty_' . $type;
10✔
67
                $address = ! empty( $input['overwrite'] ) && true === $input['overwrite']
10✔
68
                        ? self::{$type}()
×
69
                        : [];
10✔
70
                foreach ( $input as $input_field => $value ) {
10✔
71
                        if ( in_array( $input_field, array_keys( $key_mapping ), true ) ) {
10✔
72
                                $address[ $key_mapping[ $input_field ] ] = $value;
10✔
73
                        } elseif ( in_array( $input_field, $skip, true ) ) {
10✔
74
                                continue;
×
75
                        } else {
76
                                $address[ $input_field ] = $value;
10✔
77
                        }
78
                }
79

80
                return $address;
10✔
81
        }
82

83
        /**
84
         * Returns default customer shipping address data
85
         *
86
         * @return array
87
         */
88
        public static function empty_shipping() {
89
                return [
2✔
90
                        'first_name' => '',
2✔
91
                        'last_name'  => '',
2✔
92
                        'company'    => '',
2✔
93
                        'address_1'  => '',
2✔
94
                        'address_2'  => '',
2✔
95
                        'city'       => '',
2✔
96
                        'state'      => '',
2✔
97
                        'postcode'   => '',
2✔
98
                        'country'    => '',
2✔
99
                        'phone'      => '',
2✔
100
                ];
2✔
101
        }
102

103
        /**
104
         * Returns default customer billing address data
105
         *
106
         * @return array
107
         */
108
        public static function empty_billing() {
109
                return array_merge(
×
110
                        self::empty_shipping(),
×
111
                        [ 'email' => '' ]
×
112
                );
×
113
        }
114

115
        /**
116
         * Processes Customer meta data input.
117
         *
118
         * @param \WC_Customer $customer  Customer object.
119
         * @param array        $inputs    Incoming meta data.
120
         *
121
         * @return void
122
         */
123
        public static function input_meta_data_mapping( $customer, $inputs ) {
124
                if ( is_array( $inputs ) ) {
1✔
125
                        foreach ( $inputs as $meta ) {
1✔
126
                                $customer->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
1✔
127
                        }
128
                }
129
        }
130
}
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