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

wp-graphql / wp-graphql-woocommerce / 5603554109

pending completion
5603554109

push

github

web-flow
chore: use fully-qualified class names for PHPDoc types (#767)

* fix!: update deps to required versions

* chore: use fqcn for PHPDoc types

* chore: cleanup use statements

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

10258 of 12448 relevant lines covered (82.41%)

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

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

114
                return $input_fields;
109✔
115
        }
116

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

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

151
                        // Create order.
152
                        $order = null;
4✔
153
                        try {
154
                                $order_id = Order_Mutation::create_order( $input, $context, $info );
4✔
155
                                Order_Mutation::add_order_meta( $order_id, $input, $context, $info );
4✔
156
                                Order_Mutation::add_items( $input, $order_id, $context, $info );
4✔
157

158
                                // Apply coupons.
159
                                if ( ! empty( $input['coupons'] ) ) {
4✔
160
                                        Order_Mutation::apply_coupons( $order_id, $input['coupons'] );
4✔
161
                                }
162

163
                                $order = WC_Order_Factory::get_order( $order_id );
4✔
164

165
                                if ( ! is_object( $order ) ) {
4✔
166
                                        throw new UserError( __( 'Order could not be created.', 'wp-graphql-woocommerce' ) );
×
167
                                }
168

169
                                // Make sure gateways are loaded so hooks from gateways fire on save/create.
170
                                WC()->payment_gateways();
4✔
171

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

177
                                $order->set_created_via( 'graphql-api' );
4✔
178
                                $order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
4✔
179
                                $order->calculate_totals( true );
4✔
180

181
                                // Set status.
182
                                if ( ! empty( $input['status'] ) ) {
4✔
183
                                        $order->set_status( $input['status'] );
×
184
                                }
185

186
                                // Actions for after the order is saved.
187
                                if ( true === $input['isPaid'] ) {
4✔
188
                                        $order->payment_complete(
1✔
189
                                                ! empty( $input['transactionId'] ) ? $input['transactionId'] : ''
1✔
190
                                        );
1✔
191
                                }
192

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

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

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