Skip to content

Commit 5de4aa4

Browse files
committed
"*product" added to "RootQuery"
1 parent 52ceafc commit 5de4aa4

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

includes/type/object/class-product-types.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ private static function register_simple_product_type() {
212212
),
213213
)
214214
);
215+
216+
// Register "simpleProduct" query.
217+
self::register_product_query( 'simple' );
215218
}
216219

217220
/**
@@ -231,6 +234,9 @@ private static function register_variable_product_type() {
231234
),
232235
)
233236
);
237+
238+
// Register "variableProduct" query.
239+
self::register_product_query( 'variable' );
234240
}
235241

236242
/**
@@ -258,6 +264,9 @@ private static function register_external_product_type() {
258264
),
259265
)
260266
);
267+
268+
// Register "externalProduct" query.
269+
self::register_product_query( 'external' );
261270
}
262271

263272
/**
@@ -284,5 +293,81 @@ public static function register_group_product_type() {
284293
),
285294
)
286295
);
296+
297+
// Register "groupProduct" query.
298+
self::register_product_query( 'group' );
299+
}
300+
301+
/**
302+
* Register product query
303+
*
304+
* @param string $type Product type.
305+
*/
306+
private static function register_product_query( $type ) {
307+
$field_name = "{$type}Product";
308+
$type_name = ucfirst( $type ) . 'Product';
309+
register_graphql_field(
310+
'RootQuery',
311+
$field_name,
312+
array(
313+
'type' => $type_name,
314+
'description' => __( 'A simple product object', 'wp-graphql-woocommerce' ),
315+
'args' => array(
316+
'id' => array(
317+
'type' => 'ID',
318+
'description' => __( 'Get the product by its global ID', 'wp-graphql-woocommerce' ),
319+
),
320+
'productId' => array(
321+
'type' => 'Int',
322+
'description' => __( 'Get the product by its database ID', 'wp-graphql-woocommerce' ),
323+
),
324+
'slug' => array(
325+
'type' => 'String',
326+
'description' => __( 'Get the product by its slug', 'wp-graphql-woocommerce' ),
327+
),
328+
'sku' => array(
329+
'type' => 'String',
330+
'description' => __( 'Get the product by its sku', 'wp-graphql-woocommerce' ),
331+
),
332+
),
333+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $type ) {
334+
$product_id = 0;
335+
$id_type = '';
336+
if ( ! empty( $args['id'] ) ) {
337+
$id_components = Relay::fromGlobalId( $args['id'] );
338+
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
339+
throw new UserError( __( 'The "id" is invalid', 'wp-graphql-woocommerce' ) );
340+
}
341+
$product_id = absint( $id_components['id'] );
342+
$id_type = 'ID';
343+
} elseif ( ! empty( $args['productId'] ) ) {
344+
$product_id = absint( $args['productId'] );
345+
$id_type = 'product ID';
346+
} elseif ( ! empty( $args['slug'] ) ) {
347+
$post = get_page_by_path( $args['slug'], OBJECT, 'product' );
348+
$product_id = ! empty( $post ) ? absint( $post->ID ) : 0;
349+
$id_type = 'slug';
350+
} elseif ( ! empty( $args['sku'] ) ) {
351+
$product_id = \wc_get_product_id_by_sku( $args['sku'] );
352+
$id_type = 'sku';
353+
}
354+
355+
if ( empty( $product_id ) ) {
356+
/* translators: %1$s: ID type, %2$s: ID value */
357+
throw new UserError( sprintf( __( 'No product ID was found corresponding to the %1$s: %2$s' ), $id_type, $product_id ) );
358+
} elseif ( \WC()->product_factory->get_product_type( $product_id ) !== $type ) {
359+
/* translators: Invalid product type message %1$s: Product ID, %2$s: Product type */
360+
throw new UserError( sprintf( __( 'This product of ID %1$s is not a %2$s product' ), $product_id, $type ) );
361+
} elseif ( get_post( $product_id )->post_type !== 'product' ) {
362+
/* translators: %1$s: ID type, %2$s: ID value */
363+
throw new UserError( sprintf( __( 'No product exists with the %1$s: %2$s' ), $id_type, $product_id ) );
364+
}
365+
366+
$product = Factory::resolve_crud_object( $product_id, $context );
367+
368+
return $product;
369+
},
370+
)
371+
);
287372
}
288373
}

0 commit comments

Comments
 (0)