Skip to content

Commit c526a5b

Browse files
authored
feat: ProductWithAttributes and ProductVariation interfaces added (#803)
* feat: ProductWithAttributes and ProductVariation interfaces added * chore: Unused files removed
1 parent 481c12c commit c526a5b

17 files changed

+568
-412
lines changed

includes/class-core-schema-filters.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public static function resolve_product_type( $value ) {
401401
if ( isset( $possible_types[ $product_type ] ) ) {
402402
return $type_registry->get_type( $possible_types[ $product_type ] );
403403
} elseif ( str_ends_with( $product_type, 'variation' ) ) {
404-
return $type_registry->get_type( 'ProductVariation' );
404+
return self::resolve_product_variation_type( $value );
405405
} elseif ( 'on' === woographql_setting( 'enable_unsupported_product_type', 'off' ) ) {
406406
$unsupported_type = WooGraphQL::get_supported_product_type();
407407
return $type_registry->get_type( $unsupported_type );
@@ -415,4 +415,30 @@ public static function resolve_product_type( $value ) {
415415
)
416416
);
417417
}
418+
419+
/**
420+
* Resolves GraphQL type for provided product variation model.
421+
*
422+
* @param \WPGraphQL\WooCommerce\Model\Product $value Product model.
423+
*
424+
* @throws \GraphQL\Error\UserError Invalid product type requested.
425+
*
426+
* @return mixed
427+
*/
428+
public static function resolve_product_variation_type( $value ) {
429+
$type_registry = \WPGraphQL::get_type_registry();
430+
$possible_types = WooGraphQL::get_enabled_product_variation_types();
431+
$product_type = $value->get_type();
432+
if ( isset( $possible_types[ $product_type ] ) ) {
433+
return $type_registry->get_type( $possible_types[ $product_type ] );
434+
}
435+
436+
throw new UserError(
437+
sprintf(
438+
/* translators: %s: Product type */
439+
__( 'The "%s" product variation type is not supported by the core WPGraphQL WooCommerce (WooGraphQL) schema.', 'wp-graphql-woocommerce' ),
440+
$value->type
441+
)
442+
);
443+
}
418444
}

includes/class-type-registry.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,19 @@ public function init() {
6565
* Interfaces.
6666
*/
6767
Type\WPInterface\Product::register_interface();
68+
Type\WPInterface\Product_Variation::register_interface();
6869
Type\WPInterface\Attribute::register_interface();
6970
Type\WPInterface\Product_Attribute::register_interface();
7071
Type\WPInterface\Cart_Error::register_interface();
7172
Type\WPInterface\Payment_Token::register_interface();
7273
Type\WPInterface\Product_Union::register_interface();
7374
Type\WPInterface\Cart_Item::register_interface();
74-
Type\WPInterface\Downloadable_Products::register_interface();
75-
Type\WPInterface\Inventoried_Products::register_interface();
76-
Type\WPInterface\Products_With_Dimensions::register_interface();
77-
Type\WPInterface\Products_With_Pricing::register_interface();
78-
Type\WPInterface\Products_With_Variations::register_interface();
75+
Type\WPInterface\Downloadable_Product::register_interface();
76+
Type\WPInterface\Inventoried_Product::register_interface();
77+
Type\WPInterface\Product_With_Dimensions::register_interface();
78+
Type\WPInterface\Product_With_Pricing::register_interface();
79+
Type\WPInterface\Product_With_Variations::register_interface();
80+
Type\WPInterface\Product_With_Attributes::register_interface();
7981

8082
/**
8183
* Objects.
@@ -85,7 +87,6 @@ public function init() {
8587
Type\WPObject\Coupon_Type::register();
8688
Type\WPObject\Product_Types::register();
8789
Type\WPObject\Product_Attribute_Types::register();
88-
Type\WPObject\Product_Variation_Type::register();
8990
Type\WPObject\Order_Item_Type::register();
9091
Type\WPObject\Order_Type::register();
9192
Type\WPObject\Refund_Type::register();
@@ -132,7 +133,6 @@ public function init() {
132133
Connection\Products::register_connections();
133134
Connection\Orders::register_connections();
134135
Connection\Product_Attributes::register_connections();
135-
Connection\Variation_Attributes::register_connections();
136136
Connection\Customers::register_connections();
137137
Connection\Tax_Rates::register_connections();
138138
Connection\Shipping_Methods::register_connections();

includes/class-wp-graphql-woocommerce.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static function get_enabled_product_types() {
8888
* @return array
8989
*/
9090
public static function get_enabled_product_variation_types() {
91-
return apply_filters( 'graphql_woocommerce_product_variation_types', [ 'variation' => 'ProductVariation' ] );
91+
return apply_filters( 'graphql_woocommerce_product_variation_types', [ 'variation' => 'SimpleProductVariation' ] );
9292
}
9393

9494
/**
@@ -242,14 +242,16 @@ private function includes() {
242242
require $include_directory_path . 'type/interface/class-cart-error.php';
243243
require $include_directory_path . 'type/interface/class-product-attribute.php';
244244
require $include_directory_path . 'type/interface/class-product.php';
245+
require $include_directory_path . 'type/interface/class-product-variation.php';
245246
require $include_directory_path . 'type/interface/class-payment-token.php';
246247
require $include_directory_path . 'type/interface/class-product-union.php';
247248
require $include_directory_path . 'type/interface/class-cart-item.php';
248-
require $include_directory_path . 'type/interface/class-downloadable-products.php';
249-
require $include_directory_path . 'type/interface/class-inventoried-products.php';
250-
require $include_directory_path . 'type/interface/class-products-with-dimensions.php';
251-
require $include_directory_path . 'type/interface/class-products-with-pricing.php';
252-
require $include_directory_path . 'type/interface/class-products-with-variations.php';
249+
require $include_directory_path . 'type/interface/class-downloadable-product.php';
250+
require $include_directory_path . 'type/interface/class-inventoried-product.php';
251+
require $include_directory_path . 'type/interface/class-product-with-dimensions.php';
252+
require $include_directory_path . 'type/interface/class-product-with-pricing.php';
253+
require $include_directory_path . 'type/interface/class-product-with-variations.php';
254+
require $include_directory_path . 'type/interface/class-product-with-attributes.php';
253255

254256
// Include object type class files.
255257
require $include_directory_path . 'type/object/class-cart-error-types.php';
@@ -266,7 +268,6 @@ private function includes() {
266268
require $include_directory_path . 'type/object/class-product-category-type.php';
267269
require $include_directory_path . 'type/object/class-product-download-type.php';
268270
require $include_directory_path . 'type/object/class-product-types.php';
269-
require $include_directory_path . 'type/object/class-product-variation-type.php';
270271
require $include_directory_path . 'type/object/class-refund-type.php';
271272
require $include_directory_path . 'type/object/class-root-query.php';
272273
require $include_directory_path . 'type/object/class-shipping-method-type.php';
@@ -334,7 +335,6 @@ private function includes() {
334335
require $include_directory_path . 'connection/class-products.php';
335336
require $include_directory_path . 'connection/class-shipping-methods.php';
336337
require $include_directory_path . 'connection/class-tax-rates.php';
337-
require $include_directory_path . 'connection/class-variation-attributes.php';
338338
require $include_directory_path . 'connection/class-wc-terms.php';
339339

340340
// Include admin files.

includes/connection/class-products.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,12 @@ public static function set_connection_config( $config ) {
258258
public static function get_connection_config( $args = [] ): array {
259259
return array_merge(
260260
[
261-
'fromType' => 'RootQuery',
262-
'toType' => 'ProductUnion',
263-
'fromFieldName' => 'products',
264-
'connectionArgs' => self::get_connection_args(),
265-
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
261+
'fromType' => 'RootQuery',
262+
'toType' => 'ProductUnion',
263+
'fromFieldName' => 'products',
264+
'connectionArgs' => self::get_connection_args(),
265+
'connectionFields' => self::get_connection_fields(),
266+
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
266267
$resolver = new Product_Connection_Resolver( $source, $args, $context, $info );
267268

268269
return $resolver->get_connection();
@@ -272,6 +273,20 @@ public static function get_connection_config( $args = [] ): array {
272273
);
273274
}
274275

276+
/**
277+
* Returns array of edge fields.
278+
*
279+
* @return array
280+
*/
281+
public static function get_connection_fields(): array {
282+
return [
283+
'found' => [
284+
'type' => 'Number',
285+
'description' => __( 'Total products founds', 'wp-graphql-woocommerce' ),
286+
],
287+
];
288+
}
289+
275290
/**
276291
* Returns array of where args.
277292
*

includes/connection/class-variation-attributes.php

Lines changed: 0 additions & 68 deletions
This file was deleted.

includes/model/class-product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ protected function init() {
382382
) {
383383
$fields += [
384384
'manageStock' => function () {
385-
return $this->wc_data->get_manage_stock();
385+
return ! empty( $this->wc_data->get_manage_stock() ) ? $this->wc_data->get_manage_stock() : null;
386386
},
387387
'stockQuantity' => function () {
388388
return ! empty( $this->wc_data->get_stock_quantity() ) ? $this->wc_data->get_stock_quantity() : null;

includes/type/interface/class-downloadable-products.php renamed to includes/type/interface/class-downloadable-product.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Defines the fields for downloadable products.
3+
* Defines the "DownloadableProduct" interface.
44
*
55
* @package WPGraphQL\WooCommerce\Type\WPInterface
66
* @since TBD
@@ -11,20 +11,20 @@
1111
use WPGraphQL\WooCommerce\Core_Schema_Filters as Core;
1212

1313
/**
14-
* Class Downloadable_Products
14+
* Class Downloadable_Product
1515
*/
16-
class Downloadable_Products {
16+
class Downloadable_Product {
1717
/**
18-
* Registers the "DownloadableProducts" type
18+
* Registers the "DownloadableProduct" type
1919
*
2020
* @return void
2121
* @throws \Exception
2222
*/
2323
public static function register_interface(): void {
2424
register_graphql_interface_type(
25-
'DownloadableProducts',
25+
'DownloadableProduct',
2626
[
27-
'description' => __( 'Downloadable products.', 'wp-graphql-woocommerce' ),
27+
'description' => __( 'A downloadable product.', 'wp-graphql-woocommerce' ),
2828
'interfaces' => [ 'Node' ],
2929
'fields' => self::get_fields(),
3030
'resolveType' => [ Core::class, 'resolve_product_type' ],
@@ -33,7 +33,7 @@ public static function register_interface(): void {
3333
}
3434

3535
/**
36-
* Defines "DownloadableProducts" fields.
36+
* Defines fields of "DownloadableProduct".
3737
*
3838
* @return array
3939
*/

includes/type/interface/class-inventoried-products.php renamed to includes/type/interface/class-inventoried-product.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Defines the fields for manage product inventories.
3+
* Defines the "InventoriedProduct" interface.
44
*
55
* @package WPGraphQL\WooCommerce\Type\WPInterface
66
* @since TBD
@@ -11,20 +11,20 @@
1111
use WPGraphQL\WooCommerce\Core_Schema_Filters as Core;
1212

1313
/**
14-
* Class Inventoried_Products
14+
* Class Inventoried_Product
1515
*/
16-
class Inventoried_Products {
16+
class Inventoried_Product {
1717
/**
18-
* Registers the "InventoriedProducts" type
18+
* Registers the "InventoriedProduct" type
1919
*
2020
* @return void
2121
* @throws \Exception
2222
*/
2323
public static function register_interface(): void {
2424
register_graphql_interface_type(
25-
'InventoriedProducts',
25+
'InventoriedProduct',
2626
[
27-
'description' => __( 'Products with stock information.', 'wp-graphql-woocommerce' ),
27+
'description' => __( 'A product with stock information.', 'wp-graphql-woocommerce' ),
2828
'interfaces' => [ 'Node' ],
2929
'fields' => self::get_fields(),
3030
'resolveType' => [ Core::class, 'resolve_product_type' ],
@@ -33,7 +33,7 @@ public static function register_interface(): void {
3333
}
3434

3535
/**
36-
* Defines "InventoriedProducts" fields.
36+
* Defines fields of "InventoriedProduct".
3737
*
3838
* @return array
3939
*/
@@ -48,7 +48,7 @@ public static function get_fields() {
4848
'description' => __( 'Product or variation ID', 'wp-graphql-woocommerce' ),
4949
],
5050
'manageStock' => [
51-
'type' => 'Boolean',
51+
'type' => 'ManageStockEnum',
5252
'description' => __( 'If product manage stock', 'wp-graphql-woocommerce' ),
5353
],
5454
'stockQuantity' => [

0 commit comments

Comments
 (0)