Skip to content

Commit 9fef297

Browse files
authored
Merge pull request #159 from kidunot89/feature/product-interface
Product(Object) to Product(Interface)
2 parents a9033bb + 5de4aa4 commit 9fef297

32 files changed

+1446
-1019
lines changed

includes/class-type-registry.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
5252
\WPGraphQL\Extensions\WooCommerce\Type\WPInputObject\Product_Taxonomy_Filter_Relation_Input::register();
5353
\WPGraphQL\Extensions\WooCommerce\Type\WPInputObject\Orderby_Inputs::register();
5454

55+
// Interfaces.
56+
\WPGraphQL\Extensions\WooCommerce\Type\WPInterface\Product::register_interface( $type_registry );
57+
5558
// Objects.
5659
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Coupon_Type::register();
57-
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Product_Type::register();
60+
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Product_Types::register();
5861
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Product_Variation_Type::register();
5962
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Order_Type::register();
6063
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Order_Item_Type::register();

includes/class-wp-graphql-woocommerce.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function instance() {
3838
/**
3939
* Fire off init action
4040
*
41-
* @param WPGraphQLWooCommerce $instance The instance of the WPGraphQLWooCommerce class
41+
* @param WP_GraphQL_WooCommerce $instance The instance of the WPGraphQLWooCommerce class
4242
*/
4343
do_action( 'graphql_woocommerce_init', self::$instance );
4444

@@ -66,6 +66,21 @@ public static function get_post_types() {
6666
);
6767
}
6868

69+
/**
70+
* Returns WooCommerce product types to be exposed to the GraphQL schema.
71+
*/
72+
public static function get_enabled_product_types() {
73+
return apply_filters(
74+
'graphql_enabled_wc_product_types',
75+
array(
76+
'simple' => 'SimpleProduct',
77+
'variable' => 'VariableProduct',
78+
'external' => 'ExternalProduct',
79+
'grouped' => 'GroupProduct',
80+
)
81+
);
82+
}
83+
6984
/**
7085
* Returns WooCommerce product attribute taxonomies to be registered as
7186
* "TermObject" types in the schema.
@@ -156,7 +171,7 @@ private function setup() {
156171
\WPGraphQL\Extensions\WooCommerce\JWT_Auth_Schema_Filters::add_filters();
157172

158173
$registry = new \WPGraphQL\Extensions\WooCommerce\Type_Registry();
159-
add_action( 'graphql_register_types', array( $registry, 'init' ) );
174+
add_action( 'graphql_register_types', array( $registry, 'init' ), 10, 1 );
160175
}
161176
}
162177
endif;

includes/connection/class-posts.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ class Posts extends PostObjects {
1919
* Registers the various connections from other WooCommerce Types to other WordPress post-types
2020
*/
2121
public static function register_connections() {
22-
/**
23-
* From Product to MediaItem
24-
*/
2522
register_graphql_connection(
2623
self::get_connection_config(
2724
get_post_type_object( 'attachment' ),
@@ -32,5 +29,21 @@ public static function register_connections() {
3229
)
3330
)
3431
);
32+
/**
33+
* From product types to MediaItem
34+
*/
35+
$product_types = array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() );
36+
foreach ( $product_types as $product_type ) {
37+
register_graphql_connection(
38+
self::get_connection_config(
39+
get_post_type_object( 'attachment' ),
40+
array(
41+
'fromType' => $product_type,
42+
'toType' => 'MediaItem',
43+
'fromFieldName' => 'galleryImages',
44+
)
45+
)
46+
);
47+
}
3548
}
3649
}

includes/connection/class-product-attributes.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ class Product_Attributes {
2020
* Registers the various connections from other Types to ProductAttribute
2121
*/
2222
public static function register_connections() {
23-
// From Product.
24-
register_graphql_connection( self::get_connection_config() );
23+
// From product types.
24+
$product_types = array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() );
25+
foreach ( $product_types as $product_type ) {
26+
register_graphql_connection(
27+
self::get_connection_config(
28+
array( 'fromType' => $product_type )
29+
)
30+
);
31+
}
2532
}
2633

2734
/**
@@ -35,7 +42,7 @@ public static function register_connections() {
3542
*/
3643
public static function get_connection_config( $args = array() ) {
3744
$defaults = array(
38-
'fromType' => 'Product',
45+
'fromType' => 'SimpleProduct',
3946
'toType' => 'ProductAttribute',
4047
'fromFieldName' => 'attributes',
4148
'connectionArgs' => array(),

includes/connection/class-products.php

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,9 @@ class Products {
2121
public static function register_connections() {
2222
// From RootQuery.
2323
register_graphql_connection( self::get_connection_config() );
24+
2425
// From Coupon.
25-
register_graphql_connection(
26-
self::get_connection_config(
27-
array(
28-
'fromType' => 'Coupon',
29-
'fromFieldName' => 'products',
30-
)
31-
)
32-
);
26+
register_graphql_connection( self::get_connection_config( array( 'fromType' => 'Coupon' ) ) );
3327
register_graphql_connection(
3428
self::get_connection_config(
3529
array(
@@ -38,7 +32,8 @@ public static function register_connections() {
3832
)
3933
)
4034
);
41-
// From Product.
35+
36+
// Connections from all product types to related and upsell.
4237
register_graphql_connection(
4338
self::get_connection_config(
4439
array(
@@ -55,63 +50,71 @@ public static function register_connections() {
5550
)
5651
)
5752
);
53+
54+
$product_types = array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() );
55+
foreach ( $product_types as $product_type ) {
56+
register_graphql_connection(
57+
self::get_connection_config(
58+
array(
59+
'fromType' => $product_type,
60+
'fromFieldName' => 'related',
61+
)
62+
)
63+
);
64+
register_graphql_connection(
65+
self::get_connection_config(
66+
array(
67+
'fromType' => $product_type,
68+
'fromFieldName' => 'upsell',
69+
)
70+
)
71+
);
72+
}
73+
74+
// Group product children connection.
75+
register_graphql_connection( self::get_connection_config( array( 'fromType' => 'GroupProduct' ) ) );
76+
77+
// Product cross-sell connections.
5878
register_graphql_connection(
5979
self::get_connection_config(
6080
array(
61-
'fromType' => 'Product',
81+
'fromType' => 'SimpleProduct',
6282
'fromFieldName' => 'crossSell',
6383
)
6484
)
6585
);
6686
register_graphql_connection(
6787
self::get_connection_config(
6888
array(
69-
'fromType' => 'Product',
70-
'fromFieldName' => 'grouped',
89+
'fromType' => 'VariableProduct',
90+
'fromFieldName' => 'crossSell',
7191
)
7292
)
7393
);
7494

75-
// From Product to ProductVariation.
95+
// From VariableProduct to ProductVariation.
7696
register_graphql_connection(
7797
self::get_connection_config(
7898
array(
79-
'fromType' => 'Product',
99+
'fromType' => 'VariableProduct',
80100
'toType' => 'ProductVariation',
81101
'fromFieldName' => 'variations',
82102
)
83103
)
84104
);
85105

86106
// From ProductCategory.
87-
register_graphql_connection(
88-
self::get_connection_config(
89-
array(
90-
'fromType' => 'ProductCategory',
91-
'fromFieldName' => 'products',
92-
)
93-
)
94-
);
107+
register_graphql_connection( self::get_connection_config( array( 'fromType' => 'ProductCategory' ) ) );
95108

96109
// From ProductTag.
97-
register_graphql_connection(
98-
self::get_connection_config(
99-
array(
100-
'fromType' => 'ProductTag',
101-
'fromFieldName' => 'products',
102-
)
103-
)
104-
);
110+
register_graphql_connection( self::get_connection_config( array( 'fromType' => 'ProductTag' ) ) );
105111

106112
// From WooCommerce product attributes.
107113
$attributes = \WP_GraphQL_WooCommerce::get_product_attribute_taxonomies();
108114
foreach ( $attributes as $attribute ) {
109115
register_graphql_connection(
110116
self::get_connection_config(
111-
array(
112-
'fromType' => ucfirst( graphql_format_field_name( $attribute ) ),
113-
'fromFieldName' => 'products',
114-
)
117+
array( 'fromType' => ucfirst( graphql_format_field_name( $attribute ) ) )
115118
)
116119
);
117120
register_graphql_connection(

includes/connection/class-variation-attributes.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ class Variation_Attributes {
2222
public static function register_connections() {
2323
// From ProductVariation.
2424
register_graphql_connection( self::get_connection_config() );
25-
// From Product.
26-
register_graphql_connection(
27-
self::get_connection_config(
28-
array(
29-
'fromType' => 'Product',
30-
'fromFieldName' => 'defaultAttributes',
25+
26+
// From product types.
27+
$product_types = array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() );
28+
foreach ( $product_types as $product_type ) {
29+
register_graphql_connection(
30+
self::get_connection_config(
31+
array(
32+
'fromType' => $product_type,
33+
'fromFieldName' => 'defaultAttributes',
34+
)
3135
)
32-
)
33-
);
36+
);
37+
}
3438
}
3539

3640
/**

includes/connection/class-wc-terms.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,30 @@ public static function register_connections() {
6363
)
6464
)
6565
);
66+
67+
// From Product child types.
68+
$product_types = array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() );
69+
foreach ( $product_types as $product_type ) {
70+
register_graphql_connection(
71+
self::get_connection_config(
72+
get_taxonomy( 'product_cat' ),
73+
array(
74+
'fromType' => $product_type,
75+
'toType' => 'ProductCategory',
76+
'fromFieldName' => 'categories',
77+
)
78+
)
79+
);
80+
register_graphql_connection(
81+
self::get_connection_config(
82+
get_taxonomy( 'product_tag' ),
83+
array(
84+
'fromType' => $product_type,
85+
'toType' => 'ProductTag',
86+
'fromFieldName' => 'tags',
87+
)
88+
)
89+
);
90+
}
6691
}
6792
}

includes/data/class-factory.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,17 @@ public static function resolve_node_type( $type, $node ) {
247247
case is_a( $node, Order::class ):
248248
$type = 'Order';
249249
break;
250-
case is_a( $node, Product::class ):
251-
$type = 'Product';
250+
case is_a( $node, Product::class ) && 'simple' === $node->type:
251+
$type = 'SimpleProduct';
252+
break;
253+
case is_a( $node, Product::class ) && 'variable' === $node->type:
254+
$type = 'VariableProduct';
255+
break;
256+
case is_a( $node, Product::class ) && 'external' === $node->type:
257+
$type = 'ExternalProduct';
258+
break;
259+
case is_a( $node, Product::class ) && 'grouped' === $node->type:
260+
$type = 'GroupProduct';
252261
break;
253262
case is_a( $node, Product_Variation::class ):
254263
$type = 'ProductVariation';

includes/data/connection/class-product-connection-resolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function get_query_args() {
200200
$query_args['post__in'] = isset( $query_args['post__in'] )
201201
? array_intersect( $this->source->cross_sell_ids, $query_args['post__in'] )
202202
: $this->source->cross_sell_ids;
203-
} elseif ( 'grouped' === $this->info->fieldName ) {
203+
} elseif ( 'products' === $this->info->fieldName ) {
204204
$query_args['post__in'] = isset( $query_args['post__in'] )
205205
? array_intersect( $this->source->grouped_ids, $query_args['post__in'] )
206206
: $this->source->grouped_ids;

0 commit comments

Comments
 (0)