Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product(Object) to Product(Interface) #159

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion includes/class-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
\WPGraphQL\Extensions\WooCommerce\Type\WPInputObject\Product_Taxonomy_Filter_Relation_Input::register();
\WPGraphQL\Extensions\WooCommerce\Type\WPInputObject\Orderby_Inputs::register();

// Interfaces.
\WPGraphQL\Extensions\WooCommerce\Type\WPInterface\Product::register_interface( $type_registry );

// Objects.
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Coupon_Type::register();
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Product_Type::register();
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Product_Types::register();
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Product_Variation_Type::register();
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Order_Type::register();
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Order_Item_Type::register();
Expand Down
19 changes: 17 additions & 2 deletions includes/class-wp-graphql-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function instance() {
/**
* Fire off init action
*
* @param WPGraphQLWooCommerce $instance The instance of the WPGraphQLWooCommerce class
* @param WP_GraphQL_WooCommerce $instance The instance of the WPGraphQLWooCommerce class
*/
do_action( 'graphql_woocommerce_init', self::$instance );

Expand Down Expand Up @@ -66,6 +66,21 @@ public static function get_post_types() {
);
}

/**
* Returns WooCommerce product types to be exposed to the GraphQL schema.
*/
public static function get_enabled_product_types() {
return apply_filters(
'graphql_enabled_wc_product_types',
array(
'simple' => 'SimpleProduct',
'variable' => 'VariableProduct',
'external' => 'ExternalProduct',
'grouped' => 'GroupProduct',
)
);
}

/**
* Returns WooCommerce product attribute taxonomies to be registered as
* "TermObject" types in the schema.
Expand Down Expand Up @@ -156,7 +171,7 @@ private function setup() {
\WPGraphQL\Extensions\WooCommerce\JWT_Auth_Schema_Filters::add_filters();

$registry = new \WPGraphQL\Extensions\WooCommerce\Type_Registry();
add_action( 'graphql_register_types', array( $registry, 'init' ) );
add_action( 'graphql_register_types', array( $registry, 'init' ), 10, 1 );
}
}
endif;
19 changes: 16 additions & 3 deletions includes/connection/class-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class Posts extends PostObjects {
* Registers the various connections from other WooCommerce Types to other WordPress post-types
*/
public static function register_connections() {
/**
* From Product to MediaItem
*/
register_graphql_connection(
self::get_connection_config(
get_post_type_object( 'attachment' ),
Expand All @@ -32,5 +29,21 @@ public static function register_connections() {
)
)
);
/**
* From product types to MediaItem
*/
$product_types = array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() );
foreach ( $product_types as $product_type ) {
register_graphql_connection(
self::get_connection_config(
get_post_type_object( 'attachment' ),
array(
'fromType' => $product_type,
'toType' => 'MediaItem',
'fromFieldName' => 'galleryImages',
)
)
);
}
}
}
13 changes: 10 additions & 3 deletions includes/connection/class-product-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ class Product_Attributes {
* Registers the various connections from other Types to ProductAttribute
*/
public static function register_connections() {
// From Product.
register_graphql_connection( self::get_connection_config() );
// From product types.
$product_types = array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() );
foreach ( $product_types as $product_type ) {
register_graphql_connection(
self::get_connection_config(
array( 'fromType' => $product_type )
)
);
}
}

/**
Expand All @@ -35,7 +42,7 @@ public static function register_connections() {
*/
public static function get_connection_config( $args = array() ) {
$defaults = array(
'fromType' => 'Product',
'fromType' => 'SimpleProduct',
'toType' => 'ProductAttribute',
'fromFieldName' => 'attributes',
'connectionArgs' => array(),
Expand Down
71 changes: 37 additions & 34 deletions includes/connection/class-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ class Products {
public static function register_connections() {
// From RootQuery.
register_graphql_connection( self::get_connection_config() );

// From Coupon.
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Coupon',
'fromFieldName' => 'products',
)
)
);
register_graphql_connection( self::get_connection_config( array( 'fromType' => 'Coupon' ) ) );
register_graphql_connection(
self::get_connection_config(
array(
Expand All @@ -38,7 +32,8 @@ public static function register_connections() {
)
)
);
// From Product.

// Connections from all product types to related and upsell.
register_graphql_connection(
self::get_connection_config(
array(
Expand All @@ -55,63 +50,71 @@ public static function register_connections() {
)
)
);

$product_types = array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() );
foreach ( $product_types as $product_type ) {
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => $product_type,
'fromFieldName' => 'related',
)
)
);
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => $product_type,
'fromFieldName' => 'upsell',
)
)
);
}

// Group product children connection.
register_graphql_connection( self::get_connection_config( array( 'fromType' => 'GroupProduct' ) ) );

// Product cross-sell connections.
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Product',
'fromType' => 'SimpleProduct',
'fromFieldName' => 'crossSell',
)
)
);
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Product',
'fromFieldName' => 'grouped',
'fromType' => 'VariableProduct',
'fromFieldName' => 'crossSell',
)
)
);

// From Product to ProductVariation.
// From VariableProduct to ProductVariation.
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Product',
'fromType' => 'VariableProduct',
'toType' => 'ProductVariation',
'fromFieldName' => 'variations',
)
)
);

// From ProductCategory.
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'ProductCategory',
'fromFieldName' => 'products',
)
)
);
register_graphql_connection( self::get_connection_config( array( 'fromType' => 'ProductCategory' ) ) );

// From ProductTag.
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'ProductTag',
'fromFieldName' => 'products',
)
)
);
register_graphql_connection( self::get_connection_config( array( 'fromType' => 'ProductTag' ) ) );

// From WooCommerce product attributes.
$attributes = \WP_GraphQL_WooCommerce::get_product_attribute_taxonomies();
foreach ( $attributes as $attribute ) {
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => ucfirst( graphql_format_field_name( $attribute ) ),
'fromFieldName' => 'products',
)
array( 'fromType' => ucfirst( graphql_format_field_name( $attribute ) ) )
)
);
register_graphql_connection(
Expand Down
20 changes: 12 additions & 8 deletions includes/connection/class-variation-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ class Variation_Attributes {
public static function register_connections() {
// From ProductVariation.
register_graphql_connection( self::get_connection_config() );
// From Product.
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Product',
'fromFieldName' => 'defaultAttributes',

// From product types.
$product_types = array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() );
foreach ( $product_types as $product_type ) {
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => $product_type,
'fromFieldName' => 'defaultAttributes',
)
)
)
);
);
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions includes/connection/class-wc-terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,30 @@ public static function register_connections() {
)
)
);

// From Product child types.
$product_types = array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() );
foreach ( $product_types as $product_type ) {
register_graphql_connection(
self::get_connection_config(
get_taxonomy( 'product_cat' ),
array(
'fromType' => $product_type,
'toType' => 'ProductCategory',
'fromFieldName' => 'categories',
)
)
);
register_graphql_connection(
self::get_connection_config(
get_taxonomy( 'product_tag' ),
array(
'fromType' => $product_type,
'toType' => 'ProductTag',
'fromFieldName' => 'tags',
)
)
);
}
}
}
13 changes: 11 additions & 2 deletions includes/data/class-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,17 @@ public static function resolve_node_type( $type, $node ) {
case is_a( $node, Order::class ):
$type = 'Order';
break;
case is_a( $node, Product::class ):
$type = 'Product';
case is_a( $node, Product::class ) && 'simple' === $node->type:
$type = 'SimpleProduct';
break;
case is_a( $node, Product::class ) && 'variable' === $node->type:
$type = 'VariableProduct';
break;
case is_a( $node, Product::class ) && 'external' === $node->type:
$type = 'ExternalProduct';
break;
case is_a( $node, Product::class ) && 'grouped' === $node->type:
$type = 'GroupProduct';
break;
case is_a( $node, Product_Variation::class ):
$type = 'ProductVariation';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function get_query_args() {
$query_args['post__in'] = isset( $query_args['post__in'] )
? array_intersect( $this->source->cross_sell_ids, $query_args['post__in'] )
: $this->source->cross_sell_ids;
} elseif ( 'grouped' === $this->info->fieldName ) {
} elseif ( 'products' === $this->info->fieldName ) {
$query_args['post__in'] = isset( $query_args['post__in'] )
? array_intersect( $this->source->grouped_ids, $query_args['post__in'] )
: $this->source->grouped_ids;
Expand Down
Loading