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

wp-graphql / wp-graphql-woocommerce / 5603640472

pending completion
5603640472

push

github

web-flow
fix: clean up unnecessary variables and ternaries (#766)

21 of 21 new or added lines in 16 files covered. (100.0%)

10249 of 12438 relevant lines covered (82.4%)

54.02 hits per line

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

88.57
/includes/mutation/class-order-update.php
1
<?php
2
/**
3
 * Mutation - updateOrder
4
 *
5
 * Registers mutation for updating an existing order.
6
 *
7
 * @package WPGraphQL\WooCommerce\Mutation
8
 * @since 0.2.0
9
 */
10

11
namespace WPGraphQL\WooCommerce\Mutation;
12

13
use GraphQL\Error\UserError;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use GraphQLRelay\Relay;
16
use WC_Order_Factory;
17
use WPGraphQL\AppContext;
18
use WPGraphQL\WooCommerce\Data\Mutation\Order_Mutation;
19
use WPGraphQL\WooCommerce\Model\Order;
20

21
/**
22
 * Class Order_Update
23
 */
24
class Order_Update {
25

26
        /**
27
         * Registers mutation
28
         *
29
         * @return void
30
         */
31
        public static function register_mutation() {
32
                register_graphql_mutation(
109✔
33
                        'updateOrder',
109✔
34
                        [
109✔
35
                                'inputFields'         => self::get_input_fields(),
109✔
36
                                'outputFields'        => self::get_output_fields(),
109✔
37
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
109✔
38
                        ]
109✔
39
                );
109✔
40
        }
41

42
        /**
43
         * Defines the mutation input field configuration
44
         *
45
         * @return array
46
         */
47
        public static function get_input_fields() {
48
                return array_merge(
109✔
49
                        Order_Create::get_input_fields(),
109✔
50
                        [
109✔
51
                                'id'         => [
109✔
52
                                        'type'        => 'ID',
109✔
53
                                        'description' => __( 'Order global ID', 'wp-graphql-woocommerce' ),
109✔
54
                                ],
109✔
55
                                'orderId'    => [
109✔
56
                                        'type'        => 'Int',
109✔
57
                                        'description' => __( 'Order WP ID', 'wp-graphql-woocommerce' ),
109✔
58
                                ],
109✔
59
                                'customerId' => [
109✔
60
                                        'type'        => 'Int',
109✔
61
                                        'description' => __( 'Order customer ID', 'wp-graphql-woocommerce' ),
109✔
62
                                ],
109✔
63
                        ]
109✔
64
                );
109✔
65
        }
66

67
        /**
68
         * Defines the mutation output field configuration
69
         *
70
         * @return array
71
         */
72
        public static function get_output_fields() {
73
                return [
109✔
74
                        'order' => [
109✔
75
                                'type'    => 'Order',
109✔
76
                                'resolve' => static function( $payload ) {
109✔
77
                                        return new Order( $payload['id'] );
1✔
78
                                },
109✔
79
                        ],
109✔
80
                ];
109✔
81
        }
82

83
        /**
84
         * Defines the mutation data modification closure.
85
         *
86
         * @return callable
87
         */
88
        public static function mutate_and_get_payload() {
89
                return static function( $input, AppContext $context, ResolveInfo $info ) {
109✔
90
                        // Retrieve order ID.
91
                        $order_id = null;
1✔
92
                        if ( ! empty( $input['id'] ) ) {
1✔
93
                                $id_components = Relay::fromGlobalId( $input['id'] );
1✔
94
                                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
1✔
95
                                        throw new UserError( __( 'The "id" provided is invalid', 'wp-graphql-woocommerce' ) );
×
96
                                }
97
                                $order_id = absint( $id_components['id'] );
1✔
98
                        } elseif ( ! empty( $input['orderId'] ) ) {
×
99
                                $order_id = absint( $input['orderId'] );
×
100
                        } else {
101
                                throw new UserError( __( 'No order ID provided.', 'wp-graphql-woocommerce' ) );
×
102
                        }
103

104
                        // Check if authorized to update this order.
105
                        if ( ! Order_Mutation::authorized( $input, $context, $info, 'update', $order_id ) ) {
1✔
106
                                throw new UserError( __( 'User does not have the capabilities necessary to update an order.', 'wp-graphql-woocommerce' ) );
1✔
107
                        }
108

109
                        /**
110
                         * Action called before order is updated.
111
                         *
112
                         * @param int         $order_id  Order ID.
113
                         * @param array       $input     Input data describing order
114
                         * @param \WPGraphQL\AppContext  $context   Request AppContext instance.
115
                         * @param \GraphQL\Type\Definition\ResolveInfo $info      Request ResolveInfo instance.
116
                         */
117
                        do_action( 'graphql_woocommerce_before_order_update', $order_id, $input, $context, $info );
1✔
118

119
                        Order_Mutation::add_order_meta( $order_id, $input, $context, $info );
1✔
120
                        Order_Mutation::add_items( $input, $order_id, $context, $info );
1✔
121

122
                        // Apply coupons.
123
                        if ( ! empty( $input['coupons'] ) ) {
1✔
124
                                Order_Mutation::apply_coupons( $order_id, $input['coupons'] );
1✔
125
                        }
126

127
                        $order = WC_Order_Factory::get_order( $order_id );
1✔
128

129
                        if ( ! is_object( $order ) ) {
1✔
130
                                throw new UserError( __( 'Order not found.', 'wp-graphql-woocommerce' ) );
×
131
                        }
132

133
                        // Make sure gateways are loaded so hooks from gateways fire on save/create.
134
                        \WC()->payment_gateways();
1✔
135

136
                        // Validate customer ID.
137
                        if ( ! empty( $input['customerId'] ) && ! Order_Mutation::validate_customer( $input ) ) {
1✔
138
                                throw new UserError( __( 'New customer ID is invalid.', 'wp-graphql-woocommerce' ) );
×
139
                        }
140

141
                        $order->set_created_via( 'graphql-api' );
1✔
142
                        $order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
1✔
143
                        $order->calculate_totals( true );
1✔
144

145
                        // Set status.
146
                        if ( ! empty( $input['status'] ) ) {
1✔
147
                                $order->update_status( $input['status'] );
×
148
                        }
149

150
                        // Actions for after the order is saved.
151
                        if ( true === $input['isPaid'] ) {
1✔
152
                                $order->payment_complete(
1✔
153
                                        ! empty( $input['transactionId'] )
1✔
154
                                                ? $input['transactionId']
×
155
                                                : ''
1✔
156
                                );
1✔
157
                        }
158

159
                        /**
160
                         * Action called after order is updated.
161
                         *
162
                         * @param \WC_Order    $order   WC_Order instance.
163
                         * @param array       $input   Input data describing order
164
                         * @param \WPGraphQL\AppContext  $context Request AppContext instance.
165
                         * @param \GraphQL\Type\Definition\ResolveInfo $info    Request ResolveInfo instance.
166
                         */
167
                        do_action( 'graphql_woocommerce_after_order_update', $order, $input, $context, $info );
1✔
168

169
                        return [ 'id' => $order->get_id() ];
1✔
170
                };
109✔
171
        }
172
}
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