forked from wp-graphql/wp-graphql-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wp-graphql#49 from kidunot89/feature/more-product-…
…variation-fields More ProductVariation fields
- Loading branch information
Showing
30 changed files
with
693 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* Connection type - VariationAttributes | ||
* | ||
* Registers connections to VariationAttribute | ||
* | ||
* @package WPGraphQL\Extensions\WooCommerce\Connection | ||
* @since 0.0.4 | ||
*/ | ||
|
||
namespace WPGraphQL\Extensions\WooCommerce\Connection; | ||
|
||
use WPGraphQL\Extensions\WooCommerce\Data\Factory; | ||
|
||
/** | ||
* Class Product_Attributes | ||
*/ | ||
class Variation_Attributes { | ||
/** | ||
* Registers the various connections from other Types to VariationAttribute | ||
*/ | ||
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', | ||
) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Given an array of $args, this returns the connection config, merging the provided args | ||
* with the defaults | ||
* | ||
* @access public | ||
* @param array $args - Connection configuration. | ||
* | ||
* @return array | ||
*/ | ||
public static function get_connection_config( $args = array() ) { | ||
$defaults = array( | ||
'fromType' => 'ProductVariation', | ||
'toType' => 'VariationAttribute', | ||
'fromFieldName' => 'attributes', | ||
'connectionArgs' => array(), | ||
'resolve' => function ( $root, $args, $context, $info ) { | ||
return Factory::resolve_variation_attribute_connection( $root, $args, $context, $info ); | ||
}, | ||
); | ||
|
||
return array_merge( $defaults, $args ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/data/connection/class-variation-attribute-connection-resolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* ConnectionResolver - Variation_Attribute_Connection_Resolver | ||
* | ||
* Resolves connections to VariationAttributes | ||
* | ||
* @package WPGraphQL\Extensions\WooCommerce\Data\Connection | ||
* @since 0.0.4 | ||
*/ | ||
|
||
namespace WPGraphQL\Extensions\WooCommerce\Data\Connection; | ||
|
||
use GraphQL\Type\Definition\ResolveInfo; | ||
use GraphQLRelay\Relay; | ||
use WPGraphQL\AppContext; | ||
use WPGraphQL\Extensions\WooCommerce\Model\Product; | ||
|
||
/** | ||
* Class Variation_Attribute_Connection_Resolver | ||
*/ | ||
class Variation_Attribute_Connection_Resolver { | ||
|
||
/** | ||
* Returns data array from WC_Product_Attribute ArrayAccess object. | ||
* | ||
* @param WC_Product_Attribute $attrs - WC_Product_Attribute object. | ||
* @return array | ||
*/ | ||
public function to_data_array( $attrs = array() ) { | ||
$attributes = array(); | ||
if ( array( '0' ) !== $attrs ) { | ||
foreach ( $attrs as $name => $value ) { | ||
$term = \get_term_by( 'slug', $value, $name ); | ||
if ( empty( $term ) ) { | ||
$attributes[] = array( | ||
'id' => 0, | ||
'name' => $name, | ||
'value' => $value, | ||
); | ||
} else { | ||
$attributes[] = array( | ||
'id' => $term->term_id, | ||
'name' => $term->taxonomy, | ||
'value' => $term->name, | ||
); | ||
} | ||
} | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
/** | ||
* Creates connection | ||
* | ||
* @param mixed $source - Connection source Model instance. | ||
* @param array $args - Connection arguments. | ||
* @param AppContext $context - AppContext object. | ||
* @param ResolveInfo $info - ResolveInfo object. | ||
*/ | ||
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) { | ||
if ( is_a( $source, Product::class ) ) { | ||
$attributes = $this->to_data_array( $source->default_attributes ); | ||
} else { | ||
$attributes = $this->to_data_array( $source->attributes ); | ||
} | ||
|
||
$connection = Relay::connectionFromArray( $attributes, $args ); | ||
$nodes = array(); | ||
if ( ! empty( $connection['edges'] ) && is_array( $connection['edges'] ) ) { | ||
foreach ( $connection['edges'] as $edge ) { | ||
$nodes[] = ! empty( $edge['node'] ) ? $edge['node'] : null; | ||
} | ||
} | ||
$connection['nodes'] = ! empty( $nodes ) ? $nodes : null; | ||
return ! empty( $attributes ) ? $connection : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.