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

wp-graphql / wp-graphql-woocommerce / 23675172456

28 Mar 2026 02:10AM UTC coverage: 70.983% (-18.4%) from 89.424%
23675172456

Pull #1003

github

web-flow
Merge 05339093d into 6fb7b226f
Pull Request #1003: devops: WC email template tests, COT cursor HPOS fix, checkout account auth

71 of 81 new or added lines in 5 files covered. (87.65%)

3346 existing lines in 124 files now uncovered.

12576 of 17717 relevant lines covered (70.98%)

55.38 hits per line

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

93.33
/includes/mutation/class-order-create.php
1
<?php
2
/**
3
 * Mutation - createOrder
4
 *
5
 * Registers mutation for creating an 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\WooCommerce\Data\Mutation\Order_Mutation;
18
use WPGraphQL\WooCommerce\Model\Order;
19

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

40
        /**
41
         * Defines the mutation input field configuration
42
         *
43
         * @return array
44
         */
45
        public static function get_input_fields() {
46
                return [
110✔
47
                        'parentId'           => [
110✔
48
                                'type'        => 'Int',
110✔
49
                                'description' => __( 'Parent order ID.', 'wp-graphql-woocommerce' ),
110✔
50
                        ],
110✔
51
                        'currency'           => [
110✔
52
                                'type'        => 'CurrencyEnum',
110✔
53
                                'description' => __( 'Currency the order was created with, in ISO format.', 'wp-graphql-woocommerce' ),
110✔
54
                        ],
110✔
55
                        'customerId'         => [
110✔
56
                                'type'        => 'Int',
110✔
57
                                'description' => __( 'Order customer ID', 'wp-graphql-woocommerce' ),
110✔
58
                        ],
110✔
59
                        'customerNote'       => [
110✔
60
                                'type'        => 'String',
110✔
61
                                'description' => __( 'Note left by customer during checkout.', 'wp-graphql-woocommerce' ),
110✔
62
                        ],
110✔
63
                        'coupons'            => [
110✔
64
                                'type'        => [ 'list_of' => 'String' ],
110✔
65
                                'description' => __( 'Coupons codes to be applied to order', 'wp-graphql-woocommerce' ),
110✔
66
                        ],
110✔
67
                        'status'             => [
110✔
68
                                'type'        => 'OrderStatusEnum',
110✔
69
                                'description' => __( 'Order status', 'wp-graphql-woocommerce' ),
110✔
70
                        ],
110✔
71
                        'paymentMethod'      => [
110✔
72
                                'type'        => 'String',
110✔
73
                                'description' => __( 'Payment method ID.', 'wp-graphql-woocommerce' ),
110✔
74
                        ],
110✔
75
                        'paymentMethodTitle' => [
110✔
76
                                'type'        => 'String',
110✔
77
                                'description' => __( 'Payment method title.', 'wp-graphql-woocommerce' ),
110✔
78
                        ],
110✔
79
                        'transactionId'      => [
110✔
80
                                'type'        => 'String',
110✔
81
                                'description' => __( 'Order transaction ID', 'wp-graphql-woocommerce' ),
110✔
82
                        ],
110✔
83
                        'billing'            => [
110✔
84
                                'type'        => 'CustomerAddressInput',
110✔
85
                                'description' => __( 'Order billing address', 'wp-graphql-woocommerce' ),
110✔
86
                        ],
110✔
87
                        'shipping'           => [
110✔
88
                                'type'        => 'CustomerAddressInput',
110✔
89
                                'description' => __( 'Order shipping address', 'wp-graphql-woocommerce' ),
110✔
90
                        ],
110✔
91
                        'lineItems'          => [
110✔
92
                                'type'        => [ 'list_of' => 'LineItemInput' ],
110✔
93
                                'description' => __( 'Order line items', 'wp-graphql-woocommerce' ),
110✔
94
                        ],
110✔
95
                        'shippingLines'      => [
110✔
96
                                'type'        => [ 'list_of' => 'ShippingLineInput' ],
110✔
97
                                'description' => __( 'Order shipping lines', 'wp-graphql-woocommerce' ),
110✔
98
                        ],
110✔
99
                        'feeLines'           => [
110✔
100
                                'type'        => [ 'list_of' => 'FeeLineInput' ],
110✔
101
                                'description' => __( 'Order shipping lines', 'wp-graphql-woocommerce' ),
110✔
102
                        ],
110✔
103
                        'metaData'           => [
110✔
104
                                'type'        => [ 'list_of' => 'MetaDataInput' ],
110✔
105
                                'description' => __( 'Order meta data', 'wp-graphql-woocommerce' ),
110✔
106
                        ],
110✔
107
                        'isPaid'             => [
110✔
108
                                'type'        => 'Boolean',
110✔
109
                                'description' => __( 'Define if the order is paid. It will set the status to processing and reduce stock items.', 'wp-graphql-woocommerce' ),
110✔
110
                        ],
110✔
111
                ];
110✔
112
        }
113

114
        /**
115
         * Defines the mutation output field configuration
116
         *
117
         * @return array
118
         */
119
        public static function get_output_fields() {
120
                return [
110✔
121
                        'order'   => [
110✔
122
                                'type'    => 'Order',
110✔
123
                                'resolve' => static function ( $payload ) {
110✔
124
                                        return new Order( $payload['id'] );
1✔
125
                                },
110✔
126
                        ],
110✔
127
                        'orderId' => [
110✔
128
                                'type'    => 'Int',
110✔
129
                                'resolve' => static function ( $payload ) {
110✔
130
                                        return $payload['id'];
×
131
                                },
110✔
132
                        ],
110✔
133
                ];
110✔
134
        }
135

136
        /**
137
         * Defines the mutation data modification closure.
138
         *
139
         * @return callable
140
         */
141
        public static function mutate_and_get_payload() {
142
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
110✔
143
                        // Check if authorized to create this order.
144
                        if ( ! Order_Mutation::authorized( $input, $context, $info, 'create', null ) ) {
1✔
145
                                throw new UserError( __( 'User does not have the capabilities necessary to create an order.', 'wp-graphql-woocommerce' ) );
1✔
146
                        }
147

148
                        // Create order.
149
                        $order = null;
1✔
150
                        try {
151
                                $order_id = Order_Mutation::create_order( $input, $context, $info );
1✔
152
                                Order_Mutation::add_order_meta( $order_id, $input, $context, $info );
1✔
153
                                Order_Mutation::add_items( $input, $order_id, $context, $info );
1✔
154

155
                                // Apply coupons.
156
                                if ( ! empty( $input['coupons'] ) ) {
1✔
157
                                        Order_Mutation::apply_coupons( $order_id, $input['coupons'] );
1✔
158
                                }
159

160
                                $order = WC_Order_Factory::get_order( $order_id );
1✔
161

162
                                if ( ! is_object( $order ) ) {
1✔
163
                                        throw new UserError( __( 'Order could not be created.', 'wp-graphql-woocommerce' ) );
×
164
                                }
165

166
                                // Make sure gateways are loaded so hooks from gateways fire on save/create.
167
                                WC()->payment_gateways();
1✔
168

169
                                // Validate customer ID, if set.
170
                                if ( ! empty( $input['customerId'] ) && ! Order_Mutation::validate_customer( $input['customerId'] ) ) {
1✔
171
                                        throw new UserError( __( 'Customer ID is invalid.', 'wp-graphql-woocommerce' ) );
×
172
                                }
173

174
                                $order->set_created_via( 'graphql-api' );
1✔
175
                                $order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
1✔
176
                                $order->calculate_totals( true );
1✔
177

178
                                // Set status.
179
                                if ( ! empty( $input['status'] ) ) {
1✔
UNCOV
180
                                        $order->set_status( $input['status'] );
×
181
                                }
182

183
                                // Actions for after the order is saved.
184
                                if ( ! empty( $input['isPaid'] ) ) {
1✔
185
                                        $transaction_id = ! empty( $input['transactionId'] ) ? $input['transactionId'] : '';
1✔
186
                                        $order->payment_complete(
1✔
187
                                                apply_filters( 'graphql_woocommerce_order_pre_validate_transaction_id', $transaction_id, $order, $input )
1✔
188
                                        );
1✔
189
                                }
190

191
                                /**
192
                                 * Action called after order is created.
193
                                 *
194
                                 * @param \WC_Order    $order   WC_Order instance.
195
                                 * @param array       $input   Input data describing order.
196
                                 * @param \WPGraphQL\AppContext  $context Request AppContext instance.
197
                                 * @param \GraphQL\Type\Definition\ResolveInfo $info    Request ResolveInfo instance.
198
                                 */
199
                                do_action( 'graphql_woocommerce_after_order_create', $order, $input, $context, $info );
1✔
200

201
                                return [ 'id' => $order->get_id() ];
1✔
202
                        } catch ( \Throwable $e ) {
×
203
                                // Delete order if it was created.
204
                                if ( is_object( $order ) ) {
×
205
                                        Order_Mutation::purge( $order );
×
206
                                }
207

208
                                // Throw error.
209
                                throw new UserError( $e->getMessage() );
×
210
                        }//end try
211
                };
110✔
212
        }
213
}
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