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

wp-graphql / wp-graphql-woocommerce / 11714183904

07 Nov 2024 12:23AM UTC coverage: 83.665% (-0.8%) from 84.451%
11714183904

push

github

web-flow
devops: Name officially changed to "WPGraphQL for WooCommerce" (#900)

* devops: Name officially changed to "WPGraphQL for WooCommerce"

* devops: GA workflow environment updated

* devops: GA workflow environment updated

* devops: composer-git-hooks downgraded to "2.8.5"

* devops: Unstable session manager tests skipped

* chore: cleanup applied

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

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

268 existing lines in 20 files now uncovered.

12431 of 14858 relevant lines covered (83.67%)

71.79 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 WC_Order_Factory;
16
use WPGraphQL\AppContext;
17
use WPGraphQL\Utils\Utils;
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
         * Registers mutation
27
         *
28
         * @return void
29
         */
30
        public static function register_mutation() {
31
                register_graphql_mutation(
141✔
32
                        'updateOrder',
141✔
33
                        [
141✔
34
                                'inputFields'         => self::get_input_fields(),
141✔
35
                                'outputFields'        => self::get_output_fields(),
141✔
36
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
141✔
37
                        ]
141✔
38
                );
141✔
39
        }
40

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

67
        /**
68
         * Defines the mutation output field configuration
69
         *
70
         * @return array
71
         */
72
        public static function get_output_fields() {
73
                return [
141✔
74
                        'order' => [
141✔
75
                                'type'    => 'Order',
141✔
76
                                'resolve' => static function ( $payload ) {
141✔
77
                                        return new Order( $payload['id'] );
1✔
78
                                },
141✔
79
                        ],
141✔
80
                ];
141✔
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 ) {
141✔
90
                        // Retrieve order ID.
91
                        $order_id = null;
1✔
92
                        if ( ! empty( $input['id'] ) ) {
1✔
93
                                $order_id = Utils::get_database_id_from_id( $input['id'] );
1✔
94
                        } elseif ( ! empty( $input['orderId'] ) ) {
×
UNCOV
95
                                $order_id = absint( $input['orderId'] );
×
96
                        } else {
97
                                throw new UserError( __( 'Order ID provided is missing or invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
×
98
                        }
99

100
                        if ( ! $order_id ) {
1✔
UNCOV
101
                                throw new UserError( __( 'Order ID provided is invalid. Please check input and try again.', '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✔
UNCOV
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['customerId'] ) ) {
1✔
UNCOV
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✔
UNCOV
147
                                $order->update_status( $input['status'] );
×
148
                        }
149

150
                        // Actions for after the order is saved.
151
                        if ( isset( $input['isPaid'] ) && true === $input['isPaid'] ) {
1✔
152
                                $order->payment_complete(
1✔
153
                                        ! empty( $input['transactionId'] )
1✔
UNCOV
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
                };
141✔
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