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

wp-graphql / wp-graphql-woocommerce / 23354837106

20 Mar 2026 05:31PM UTC coverage: 83.637% (+0.4%) from 83.244%
23354837106

push

github

web-flow
feat: Product and product attribute mutations (#851)

* devops: Product and product attribute mutation tests generated

* feat: Product Mutations implemented and tested

* chore: Linter and PHPStan compliance met

* fix: resolve test failures in product and variation mutation tests

- Removed duplicate requires for payment-method classes (rebase artifact)
- Wrapped bare `attributes` queries in `... on SimpleProduct` inline
  fragments (attributes field is on ProductWithAttributes interface,
  not the base Product type)
- Fixed attribute label expectations to match factory output (lowercase)
- Fixed relay ID prefix from 'product'/'product_variation' to 'post'
  to match WPGraphQL's actual encoding
- Cache WP_Post before force-deletion and re-cache after so the Product
  model's type resolver can still determine the GraphQL type in the
  response
- Added wp_cache_flush() before asserting product deletion to avoid
  false positives from the re-cached post

* chore: fix PHPCS lint errors in mutation files

* fix: resolve CI test failures for attribute and variation mutations

- Fixed ProductVariation type registration in schema filters so the
  variation mutation output field resolves correctly
- Renamed attribute slug from 'pattern' to 'fabric' in
  ProductAttributeMutationsTest to avoid collision with the pattern
  attribute created by ProductAttributeQueriesTest
- Fixed PHPStan errors for redundant is_wp_error checks
- Updated IntrospectionQueryTest

1251 of 1437 new or added lines in 23 files covered. (87.06%)

13995 of 16733 relevant lines covered (83.64%)

91.46 hits per line

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

92.35
/includes/mutation/class-product-create.php
1
<?php
2
/**
3
 * Mutation - createProduct
4
 *
5
 * Registers mutation for creating a product.
6
 *
7
 * @package WPGraphQL\WooCommerce\Mutation
8
 * @since TBD
9
 */
10

11
namespace WPGraphQL\WooCommerce\Mutation;
12

13
use GraphQL\Error\UserError;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use WPGraphQL\AppContext;
16
use WPGraphQL\WooCommerce\Data\Mutation\Product_Mutation;
17
use WPGraphQL\WooCommerce\Model\Product;
18

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

39
        /**
40
         * Defines the mutation input field configuration
41
         *
42
         * @return array
43
         */
44
        public static function get_input_fields() {
45
                return [
182✔
46
                        'name'              => [
182✔
47
                                'type'        => [ 'non_null' => 'String' ],
182✔
48
                                'description' => __( 'Name of the product.', 'wp-graphql-woocommerce' ),
182✔
49
                        ],
182✔
50
                        'slug'              => [
182✔
51
                                'type'        => 'String',
182✔
52
                                'description' => __( 'Product slug.', 'wp-graphql-woocommerce' ),
182✔
53
                        ],
182✔
54
                        'type'              => [
182✔
55
                                'type'        => 'ProductTypesEnum',
182✔
56
                                'description' => __( 'Type of the product.', 'wp-graphql-woocommerce' ),
182✔
57
                        ],
182✔
58
                        'status'            => [
182✔
59
                                'type'        => 'PostStatusEnum',
182✔
60
                                'description' => __( 'Status of the product.', 'wp-graphql-woocommerce' ),
182✔
61
                        ],
182✔
62
                        'featured'          => [
182✔
63
                                'type'        => 'Boolean',
182✔
64
                                'description' => __( 'Featured product.', 'wp-graphql-woocommerce' ),
182✔
65
                        ],
182✔
66
                        'catalogVisibility' => [
182✔
67
                                'type'        => 'CatalogVisibilityEnum',
182✔
68
                                'description' => __( 'Catalog visibility.', 'wp-graphql-woocommerce' ),
182✔
69
                        ],
182✔
70
                        'description'       => [
182✔
71
                                'type'        => 'String',
182✔
72
                                'description' => __( 'Product description.', 'wp-graphql-woocommerce' ),
182✔
73
                        ],
182✔
74
                        'shortDescription'  => [
182✔
75
                                'type'        => 'String',
182✔
76
                                'description' => __( 'Product short description.', 'wp-graphql-woocommerce' ),
182✔
77
                        ],
182✔
78
                        'sku'               => [
182✔
79
                                'type'        => 'String',
182✔
80
                                'description' => __( 'Product SKU.', 'wp-graphql-woocommerce' ),
182✔
81
                        ],
182✔
82
                        'regularPrice'      => [
182✔
83
                                'type'        => 'Float',
182✔
84
                                'description' => __( 'Product regular price.', 'wp-graphql-woocommerce' ),
182✔
85
                        ],
182✔
86
                        'salePrice'         => [
182✔
87
                                'type'        => 'Float',
182✔
88
                                'description' => __( 'Product sale price.', 'wp-graphql-woocommerce' ),
182✔
89
                        ],
182✔
90
                        'dateOnSaleFrom'    => [
182✔
91
                                'type'        => 'String',
182✔
92
                                'description' => __( 'Product sale start date.', 'wp-graphql-woocommerce' ),
182✔
93
                        ],
182✔
94
                        'dateOnSaleTo'      => [
182✔
95
                                'type'        => 'String',
182✔
96
                                'description' => __( 'Product sale end date.', 'wp-graphql-woocommerce' ),
182✔
97
                        ],
182✔
98
                        'virtual'           => [
182✔
99
                                'type'        => 'Boolean',
182✔
100
                                'description' => __( 'Product virtual.', 'wp-graphql-woocommerce' ),
182✔
101
                        ],
182✔
102
                        'downloadable'      => [
182✔
103
                                'type'        => 'Boolean',
182✔
104
                                'description' => __( 'Product downloadable.', 'wp-graphql-woocommerce' ),
182✔
105
                        ],
182✔
106
                        'downloads'         => [
182✔
107
                                'type'        => [ 'list_of' => 'ProductDownloadInput' ],
182✔
108
                                'description' => __( 'Product downloads.', 'wp-graphql-woocommerce' ),
182✔
109
                        ],
182✔
110
                        'downloadLimit'     => [
182✔
111
                                'type'        => 'Int',
182✔
112
                                'description' => __( 'Product download limit.', 'wp-graphql-woocommerce' ),
182✔
113
                        ],
182✔
114
                        'downloadExpiry'    => [
182✔
115
                                'type'        => 'Int',
182✔
116
                                'description' => __( 'Number of days until download access expires.', 'wp-graphql-woocommerce' ),
182✔
117
                        ],
182✔
118
                        'externalUrl'       => [
182✔
119
                                'type'        => 'String',
182✔
120
                                'description' => __( 'Product external URL. (External products only)', 'wp-graphql-woocommerce' ),
182✔
121
                        ],
182✔
122
                        'buttonText'        => [
182✔
123
                                'type'        => 'String',
182✔
124
                                'description' => __( 'Product button text. (External products only)', 'wp-graphql-woocommerce' ),
182✔
125
                        ],
182✔
126
                        'taxStatus'         => [
182✔
127
                                'type'        => 'TaxStatusEnum',
182✔
128
                                'description' => __( 'Tax status.', 'wp-graphql-woocommerce' ),
182✔
129
                        ],
182✔
130
                        'taxClass'          => [
182✔
131
                                'type'        => 'TaxClassEnum',
182✔
132
                                'description' => __( 'Tax class.', 'wp-graphql-woocommerce' ),
182✔
133
                        ],
182✔
134
                        'manageStock'       => [
182✔
135
                                'type'        => 'Boolean',
182✔
136
                                'description' => __( 'Manage stock.', 'wp-graphql-woocommerce' ),
182✔
137
                        ],
182✔
138
                        'stockQuantity'     => [
182✔
139
                                'type'        => 'Int',
182✔
140
                                'description' => __( 'Stock quantity.', 'wp-graphql-woocommerce' ),
182✔
141
                        ],
182✔
142
                        'stockStatus'       => [
182✔
143
                                'type'        => 'StockStatusEnum',
182✔
144
                                'description' => __( 'Stock status.', 'wp-graphql-woocommerce' ),
182✔
145
                        ],
182✔
146
                        'backorders'        => [
182✔
147
                                'type'        => 'BackordersEnum',
182✔
148
                                'description' => __( 'Backorders.', 'wp-graphql-woocommerce' ),
182✔
149
                        ],
182✔
150
                        'soldIndividually'  => [
182✔
151
                                'type'        => 'Boolean',
182✔
152
                                'description' => __( 'Sold individually.', 'wp-graphql-woocommerce' ),
182✔
153
                        ],
182✔
154
                        'weight'            => [
182✔
155
                                'type'        => 'String',
182✔
156
                                'description' => __( 'Product weight.', 'wp-graphql-woocommerce' ),
182✔
157
                        ],
182✔
158
                        'dimensions'        => [
182✔
159
                                'type'        => 'ProductDimensionsInput',
182✔
160
                                'description' => __( 'Product dimensions.', 'wp-graphql-woocommerce' ),
182✔
161
                        ],
182✔
162
                        'shippingClass'     => [
182✔
163
                                'type'        => 'String',
182✔
164
                                'description' => __( 'Shipping class.', 'wp-graphql-woocommerce' ),
182✔
165
                        ],
182✔
166
                        'reviewsAllowed'    => [
182✔
167
                                'type'        => 'Boolean',
182✔
168
                                'description' => __( 'Allow reviews. Default is true', 'wp-graphql-woocommerce' ),
182✔
169
                        ],
182✔
170
                        'upsellIds'         => [
182✔
171
                                'type'        => [ 'list_of' => 'Int' ],
182✔
172
                                'description' => __( 'Upsell product IDs.', 'wp-graphql-woocommerce' ),
182✔
173
                        ],
182✔
174
                        'crossSellIds'      => [
182✔
175
                                'type'        => [ 'list_of' => 'Int' ],
182✔
176
                                'description' => __( 'Cross-sell product IDs.', 'wp-graphql-woocommerce' ),
182✔
177
                        ],
182✔
178
                        'parentId'          => [
182✔
179
                                'type'        => 'Int',
182✔
180
                                'description' => __( 'Parent product ID.', 'wp-graphql-woocommerce' ),
182✔
181
                        ],
182✔
182
                        'purchaseNote'      => [
182✔
183
                                'type'        => 'String',
182✔
184
                                'description' => __( 'Purchase note.', 'wp-graphql-woocommerce' ),
182✔
185
                        ],
182✔
186
                        'categories'        => [
182✔
187
                                'type'        => [ 'list_of' => 'Int' ],
182✔
188
                                'description' => __( 'Product categories.', 'wp-graphql-woocommerce' ),
182✔
189
                        ],
182✔
190
                        'tags'              => [
182✔
191
                                'type'        => [ 'list_of' => 'Int' ],
182✔
192
                                'description' => __( 'Product tags.', 'wp-graphql-woocommerce' ),
182✔
193
                        ],
182✔
194
                        'images'            => [
182✔
195
                                'type'        => [ 'list_of' => 'ProductImageInput' ],
182✔
196
                                'description' => __( 'Product images.', 'wp-graphql-woocommerce' ),
182✔
197
                        ],
182✔
198
                        'attributes'        => [
182✔
199
                                'type'        => [ 'list_of' => 'ProductAttributesInput' ],
182✔
200
                                'description' => __( 'Product attributes.', 'wp-graphql-woocommerce' ),
182✔
201
                        ],
182✔
202
                        'defaultAttributes' => [
182✔
203
                                'type'        => [ 'list_of' => 'ProductAttributeInput' ],
182✔
204
                                'description' => __( 'Product default attributes.', 'wp-graphql-woocommerce' ),
182✔
205
                        ],
182✔
206
                        'groupedProducts'   => [
182✔
207
                                'type'        => [ 'list_of' => 'Int' ],
182✔
208
                                'description' => __( 'Grouped product IDs.', 'wp-graphql-woocommerce' ),
182✔
209
                        ],
182✔
210
                        'menuOrder'         => [
182✔
211
                                'type'        => 'Int',
182✔
212
                                'description' => __( 'Menu order.', 'wp-graphql-woocommerce' ),
182✔
213
                        ],
182✔
214
                        'metaData'          => [
182✔
215
                                'type'        => [ 'list_of' => 'MetaDataInput' ],
182✔
216
                                'description' => __( 'Meta data.', 'wp-graphql-woocommerce' ),
182✔
217
                        ],
182✔
218
                ];
182✔
219
        }
220

221
        /**
222
         * Defines the mutation output field configuration
223
         *
224
         * @return array
225
         */
226
        public static function get_output_fields() {
227
                return [
182✔
228
                        'product'   => [
182✔
229
                                'type'    => 'Product',
182✔
230
                                'resolve' => static function ( $payload ) {
182✔
231
                                        return new Product( $payload['id'] );
9✔
232
                                },
182✔
233
                        ],
182✔
234
                        'productId' => [
182✔
235
                                'type'    => 'Int',
182✔
236
                                'resolve' => static function ( $payload ) {
182✔
NEW
237
                                        return $payload['id'];
×
238
                                },
182✔
239
                        ],
182✔
240
                ];
182✔
241
        }
242

243
        /**
244
         * Defines the mutation data modification closure.
245
         *
246
         * @param array                                $input    Mutation input.
247
         * @param \WPGraphQL\AppContext                $context  AppContext instance.
248
         * @param \GraphQL\Type\Definition\ResolveInfo $info     ResolveInfo instance. Can be
249
         * use to get info about the current node in the GraphQL tree.
250
         *
251
         * @throws \GraphQL\Error\UserError Invalid ID provided | Lack of capabilities.
252
         *
253
         * @return array
254
         */
255
        public static function mutate_and_get_payload( $input, AppContext $context, ResolveInfo $info ) {
256
                $product_id = ! empty( $input['id'] ) ? $input['id'] : 0;
10✔
257
                $type       = ! empty( $input['type'] ) ? $input['type'] : 'simple';
10✔
258

259
                if ( 0 !== $product_id ) {
10✔
260
                        /**
261
                         * @var \WC_Product $product
262
                         */
263
                        $product = \wc_get_product( $product_id );
1✔
264
                        if ( $product && ! wc_rest_check_post_permissions( 'product', 'edit', $product->get_id() ) ) {
1✔
265
                                throw new UserError( __( 'You do not have permission to edit this product', 'wp-graphql-woocommerce' ) );
1✔
266
                        }
267
                } else {
268
                        $classname = \WC_Product_Factory::get_classname_from_product_type( $type );
9✔
269
                        if ( ! $classname || ! class_exists( $classname ) ) {
9✔
NEW
270
                                $classname = '\WC_Product_Simple';
×
271
                        }
272

273
                        /**
274
                         * @var \WC_Product $product
275
                         */
276
                        $product = new $classname( $product_id );
9✔
277

278
                        /**
279
                         * @var \WP_Post_Type $post_type_object
280
                         */
281
                        $post_type_object = get_post_type_object( 'product' );
9✔
282
                        if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) {
9✔
283
                                throw new UserError( __( 'You do not have permission to create products', 'wp-graphql-woocommerce' ) );
1✔
284
                        }
285
                }
286

287
                if ( ! empty( $input['name'] ) ) {
10✔
288
                        $product->set_name( wp_filter_post_kses( $input['name'] ) );
10✔
289
                }
290

291
                if ( ! empty( $input['description'] ) ) {
10✔
NEW
292
                        $product->set_description( wp_filter_post_kses( $input['description'] ) );
×
293
                }
294

295
                if ( ! empty( $input['shortDescription'] ) ) {
10✔
NEW
296
                        $product->set_short_description( wp_filter_post_kses( $input['shortDescription'] ) );
×
297
                }
298

299
                if ( ! empty( $input['status'] ) ) {
10✔
NEW
300
                        $product->set_status( get_post_status_object( $input['status'] ) ? $input['status'] : 'draft' );
×
301
                }
302

303
                if ( ! empty( $input['slug'] ) ) {
10✔
NEW
304
                        $product->set_slug( $input['slug'] );
×
305
                }
306

307
                if ( ! empty( $input['menuOrder'] ) ) {
10✔
NEW
308
                        $product->set_menu_order( $input['menuOrder'] );
×
309
                }
310

311
                if ( isset( $input['reviewsAllowed'] ) ) {
10✔
312
                        $product->set_reviews_allowed( $input['reviewsAllowed'] );
1✔
313
                }
314

315
                if ( isset( $input['virtual'] ) ) {
10✔
NEW
316
                        $product->set_virtual( $input['virtual'] );
×
317
                }
318

319
                if ( ! empty( $input['taxStatus'] ) ) {
10✔
320
                        $product->set_tax_status( $input['taxStatus'] );
1✔
321
                }
322

323
                if ( ! empty( $input['taxClass'] ) ) {
10✔
NEW
324
                        $product->set_tax_class( $input['taxClass'] );
×
325
                }
326

327
                if ( ! empty( $input['catalogVisibility'] ) ) {
10✔
NEW
328
                        $product->set_catalog_visibility( $input['catalogVisibility'] );
×
329
                }
330

331
                if ( ! empty( $input['purchaseNote'] ) ) {
10✔
332
                        $product->set_purchase_note( wp_filter_post_kses( $input['purchaseNote'] ) );
1✔
333
                }
334

335
                if ( isset( $input['featured'] ) ) {
10✔
NEW
336
                        $product->set_featured( $input['featured'] );
×
337
                }
338

339
                $product = Product_Mutation::save_product_shipping_data( $product, $input );
10✔
340

341
                if ( ! empty( $input['sku'] ) ) {
10✔
342
                        /**
343
                         * @var string $sku
344
                         */
NEW
345
                        $sku = wc_clean( $input['sku'] );
×
NEW
346
                        $product->set_sku( $sku );
×
347
                }
348

349
                if ( ! empty( $input['attributes'] ) ) {
10✔
350
                        $attributes = [];
2✔
351

352
                        foreach ( $input['attributes'] as $attribute ) {
2✔
353
                                $attribute_object = Product_Mutation::prepare_attribute( $attribute );
2✔
354
                                if ( $attribute_object ) {
2✔
355
                                        $attributes[] = $attribute_object;
2✔
356
                                }
357
                        }
358

359
                        $product->set_attributes( $attributes );
2✔
360
                }
361

362
                if ( in_array( $type, [ 'variable', 'grouped' ], true ) ) {
10✔
363
                        $product->set_regular_price( '' );
2✔
364
                        $product->set_sale_price( '' );
2✔
365
                        $product->set_date_on_sale_to( '' );
2✔
366
                        $product->set_date_on_sale_from( '' );
2✔
367
                        $product->set_price( '' );
2✔
368
                } else {
369
                        if ( ! empty( $input['regularPrice'] ) ) {
8✔
370
                                $product->set_regular_price( $input['regularPrice'] );
7✔
371
                        }
372

373
                        if ( ! empty( $input['salePrice'] ) ) {
8✔
374
                                $product->set_sale_price( $input['salePrice'] );
2✔
375
                        }
376

377
                        if ( ! empty( $input['dateOnSaleFrom'] ) ) {
8✔
378
                                $product->set_date_on_sale_from( $input['dateOnSaleFrom'] );
1✔
379
                        }
380

381
                        if ( ! empty( $input['dateOnSaleTo'] ) ) {
8✔
382
                                $product->set_date_on_sale_to( $input['dateOnSaleTo'] );
1✔
383
                        }
384
                }
385

386
                if ( ! empty( $input['parentId'] ) ) {
10✔
NEW
387
                        $product->set_parent_id( $input['parentId'] );
×
388
                }
389

390
                if ( isset( $input['soldIndividually'] ) ) {
10✔
391
                        $product->set_sold_individually( $input['soldIndividually'] );
1✔
392
                }
393

394
                if ( isset( $input['stockStatus'] ) ) {
10✔
395
                        /**
396
                         * @var string $stock_status
397
                         */
NEW
398
                        $stock_status = wc_clean( $input['stockStatus'] );
×
399
                } else {
400
                        $stock_status = $product->get_stock_status();
10✔
401
                }
402

403
                if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
10✔
404
                        if ( isset( $input['manageStock'] ) ) {
10✔
405
                                /**
406
                                 * @var bool $manage_stock
407
                                 */
408
                                $manage_stock = $input['manageStock'];
1✔
409
                                $product->set_manage_stock( $manage_stock );
1✔
410
                        }
411

412
                        if ( isset( $input['backorders'] ) ) {
10✔
NEW
413
                                $product->set_backorders( $input['backorders'] );
×
414
                        }
415

416
                        if ( $product->is_type( 'grouped' ) ) {
10✔
417
                                $product->set_manage_stock( false );
1✔
418
                                $product->set_backorders( 'no' );
1✔
419
                                $product->set_stock_quantity( null );
1✔
420
                                $product->set_stock_status( $stock_status );
1✔
421
                        } elseif ( $product->is_type( 'external' ) ) {
9✔
422
                                $product->set_manage_stock( false );
1✔
423
                                $product->set_backorders( 'no' );
1✔
424
                                $product->set_stock_quantity( null );
1✔
425
                                $product->set_stock_status( 'instock' );
1✔
426
                        } elseif ( $product->get_manage_stock() ) {
8✔
427
                                // Stock status is always determined by children so sync later.
428
                                if ( ! $product->is_type( 'variable' ) ) {
2✔
429
                                        $product->set_stock_status( $stock_status );
2✔
430
                                }
431

432
                                // Stock quantity.
433
                                if ( isset( $input['stockQuantity'] ) ) {
2✔
434
                                        $product->set_stock_quantity( wc_stock_amount( $input['stockQuantity'] ) );
2✔
435
                                }
436
                        } else {
437
                                // Don't manage stock.
438
                                $product->set_manage_stock( false );
6✔
439
                                $product->set_stock_quantity( null );
6✔
440
                                $product->set_stock_status( $stock_status );
6✔
441
                        }
NEW
442
                } elseif ( $product->is_type( 'variable' ) ) {
×
NEW
443
                        $product->set_stock_status( $stock_status );
×
444
                }
445

446
                if ( ! empty( $input['upsellIds'] ) ) {
10✔
447
                        $product->set_upsell_ids( $input['upsellIds'] );
1✔
448
                }
449

450
                if ( ! empty( $input['crossSellIds'] ) ) {
10✔
451
                        $product->set_cross_sell_ids( $input['crossSellIds'] );
1✔
452
                }
453

454
                if ( ! empty( $input['categories'] ) ) {
10✔
455
                        $product = Product_Mutation::save_taxonomy_terms( $product, $input['categories'] );
1✔
456
                }
457

458
                if ( ! empty( $input['tags'] ) ) {
10✔
459
                        $product = Product_Mutation::save_taxonomy_terms( $product, $input['tags'], 'tag' );
1✔
460
                }
461

462
                if ( isset( $input['downloadable'] ) ) {
10✔
NEW
463
                        $product->set_downloadable( $input['downloadable'] );
×
464
                }
465

466
                if ( $product->get_downloadable() ) {
10✔
NEW
467
                        if ( ! empty( $input['downloads'] ) ) {
×
NEW
468
                                $product = Product_Mutation::save_downloadable_files( $product, $input['downloads'] );
×
469
                        }
470

NEW
471
                        if ( isset( $input['downloadLimit'] ) ) {
×
NEW
472
                                $product->set_download_limit( $input['downloadLimit'] );
×
473
                        }
474

NEW
475
                        if ( isset( $input['downloadExpiry'] ) ) {
×
NEW
476
                                $product->set_download_expiry( $input['downloadExpiry'] );
×
477
                        }
478
                }
479

480
                if ( $product->is_type( 'external' ) ) {
10✔
481
                        if ( ! empty( $input['externalUrl'] ) ) {
1✔
482
                                /**
483
                                 * @var \WC_Product_External $product
484
                                 */
485
                                $product->set_product_url( $input['externalUrl'] );
1✔
486
                        }
487

488
                        if ( ! empty( $input['buttonText'] ) ) {
1✔
489
                                /**
490
                                 * @var \WC_Product_External $product
491
                                 */
492
                                $product->set_button_text( $input['buttonText'] );
1✔
493
                        }
494
                }
495

496
                if ( $product->is_type( 'variable' ) ) {
10✔
497
                        $product = Product_Mutation::save_default_attributes( $product, $input );
1✔
498
                }
499

500
                if ( $product->is_type( 'grouped' ) && isset( $input['groupedProducts'] ) ) {
10✔
501
                        /**
502
                                 * @var \WC_Product_Grouped $product
503
                                 */
504
                        $product->set_children( $input['groupedProducts'] );
1✔
505
                }
506

507
                if ( ! empty( $input['images'] ) ) {
10✔
508
                        $product = Product_Mutation::set_product_images( $product, $input['images'] );
1✔
509
                }
510

511
                if ( ! empty( $input['metaData'] ) ) {
10✔
512
                        foreach ( $input['metaData'] as $meta ) {
1✔
513
                                $product->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
1✔
514
                        }
515
                }
516

517
                /**
518
                 * Filters an object before it is inserted via the GraphQL API.
519
                 *
520
                 * The dynamic portion of the hook name, `$this->post_type`,
521
                 * refers to the object type slug.
522
                 *
523
                 * @param \WC_Product $product   Object object.
524
                 * @param array       $input     GraphQL input object.
525
                 * @param bool        $creating  If is creating a new object.
526
                 */
527
                $product = apply_filters( 'graphql_woocommerce_pre_insert_product_object', $product, $input, true );
10✔
528

529
                $product_id = $product->save();
10✔
530

531
                return [ 'id' => $product_id ];
10✔
532
        }
533
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc