Skip to content

Commit

Permalink
Merge pull request #383 from wp-graphql/release/v0.7.0
Browse files Browse the repository at this point in the history
Release v0.7.0
  • Loading branch information
kidunot89 authored Nov 24, 2020
2 parents 2cd50a1 + b3c649d commit fb6578d
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class Variation_Attribute_Connection_Resolver {
/**
* Returns data array from WC_Product_Attribute ArrayAccess object.
*
* @param WC_Product_Attribute $attrs - WC_Product_Attribute object.
* @param string $parent_id - ProductVariation Relay ID.
* @param array $attrs WC_Product_Attribute object.
* @param string $parent_id ProductVariation Relay ID.
*
* @return array
*/
public function to_data_array( $attrs = array(), $parent_id = 0 ) {
public static function to_data_array( $attrs = array(), $parent_id = 0 ) {
$attributes = array();
if ( array( '0' ) !== $attrs ) {
foreach ( $attrs as $name => $value ) {
Expand Down Expand Up @@ -66,9 +66,9 @@ public function to_data_array( $attrs = array(), $parent_id = 0 ) {
*/
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) {
if ( is_a( $source, Product::class ) ) {
$attributes = $this->to_data_array( $source->default_attributes, $source->ID );
$attributes = self::to_data_array( $source->default_attributes, $source->ID );
} else {
$attributes = $this->to_data_array( $source->attributes, $source->ID );
$attributes = self::to_data_array( $source->attributes, $source->ID );
}

$connection = Relay::connectionFromArray( $attributes, $args );
Expand Down
85 changes: 66 additions & 19 deletions includes/type/object/class-cart-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Data\Connection\Variation_Attribute_Connection_Resolver;
use WPGraphQL\WooCommerce\Data\Connection\Product_Connection_Resolver;
use WPGraphQL\WooCommerce\Data\Factory;

/**
Expand All @@ -27,6 +29,7 @@ public static function register() {
self::register_cart_fee();
self::register_cart_tax();
self::register_cart_item();
self::register_cart_item_connections();
self::register_cart();
}

Expand Down Expand Up @@ -173,7 +176,7 @@ public static function register_cart() {
return \wc_graphql_price( $price );
},
),
'totalTaxes' => array(
'totalTaxes' => array(
'type' => array( 'list_of' => 'CartTax' ),
'description' => __( 'Cart total taxes itemized', 'wp-graphql-woocommerce' ),
'resolve' => function( $source ) {
Expand Down Expand Up @@ -235,24 +238,6 @@ public static function register_cart_item() {
return ! empty( $source['key'] ) ? $source['key'] : null;
},
),
'product' => array(
'type' => 'Product',
'description' => __( 'A product in the cart', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context ) {
return ! empty( $source['product_id'] )
? Factory::resolve_crud_object( $source['product_id'], $context )
: null;
},
),
'variation' => array(
'type' => 'ProductVariation',
'description' => __( 'Selected variation of the product', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context ) {
return ! empty( $source['variation_id'] )
? Factory::resolve_crud_object( $source['variation_id'], $context )
: null;
},
),
'quantity' => array(
'type' => 'Int',
'description' => __( 'Quantity of the product', 'wp-graphql-woocommerce' ),
Expand Down Expand Up @@ -297,6 +282,68 @@ public static function register_cart_item() {
);
}

/**
* Registers one-to-one connections for CartItem.
*/
public static function register_cart_item_connections() {
register_graphql_connection(
array(
'fromType' => 'CartItem',
'toType' => 'Product',
'fromFieldName' => 'product',
'oneToOne' => true,
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$id = $source['product_id'];
$resolver = new Product_Connection_Resolver( $source, $args, $context, $info );

return $resolver->one_to_one()
->set_query_arg( 'p', $id )
->get_connection();
},
)
);

register_graphql_connection(
array(
'fromType' => 'CartItem',
'toType' => 'ProductVariation',
'fromFieldName' => 'variation',
'oneToOne' => true,
'edgeFields' => array(
'attributes' => array(
'type' => array( 'list_of' => 'VariationAttribute' ),
'description' => __( 'Attributes of the variation.', 'wp-graphql-woocommerce' ),
'resolve' => function( $source ) {
$attributes = array();

$variation = $source['node'];
$cart_item_data = $source['source'];
$cart_variation_data = $cart_item_data['variation'];
foreach( $variation->attributes as $name => $default_value ) {
if ( isset( $cart_variation_data["attribute_{$name}"] ) ) {
$attributes[ $name ] = $cart_variation_data["attribute_{$name}"];
} else {
$attributes[ $name ] = $default_value;
}
}

return Variation_Attribute_Connection_Resolver::to_data_array( $attributes, $variation->ID );
},
),
),
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$id = $source['variation_id'];
$resolver = new Product_Connection_Resolver( $source, $args, $context, $info );

return $resolver->one_to_one()
->set_query_arg( 'post_type', 'product_variation' )
->set_query_arg( 'p', $id )
->get_connection();
},
)
);
}

/**
* Registers CartFee type
*/
Expand Down
4 changes: 2 additions & 2 deletions includes/type/object/class-variation-attribute-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public static function register() {
'fields' => array(
'id' => array(
'type' => array( 'non_null' => 'ID' ),
'description' => __( 'The Id of the order. Equivalent to WP_Post->ID', 'wp-graphql-woocommerce' ),
'description' => __( 'The Global ID of the attribute.', 'wp-graphql-woocommerce' ),
'resolve' => function ( $source ) {
return isset( $source['id'] ) ? $source['id'] : null;
},
),
'attributeId' => array(
'type' => 'Int',
'description' => __( 'The Id of the order. Equivalent to WP_Post->ID', 'wp-graphql-woocommerce' ),
'description' => __( 'The Database ID of the attribute.', 'wp-graphql-woocommerce' ),
'resolve' => function ( $source ) {
return isset( $source['attributeId'] ) ? $source['attributeId'] : null;
},
Expand Down
36 changes: 27 additions & 9 deletions tests/_support/Helper/GraphQLE2E.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ public function addToCart( $input, $request_headers = array() ) {
cartItem {
key
product {
id
node {
id
}
}
variation {
id
node {
id
}
}
quantity
subtotal
Expand Down Expand Up @@ -148,10 +152,14 @@ public function removeItemsFromCart( $input, $request_headers = array() ) {
cartItems {
key
product {
id
node {
id
}
}
variation {
id
node {
id
}
}
quantity
subtotal
Expand Down Expand Up @@ -185,10 +193,14 @@ public function restoreCartItems( $input, $request_headers = array() ) {
cartItems {
key
product {
id
node {
id
}
}
variation {
id
node {
id
}
}
quantity
subtotal
Expand Down Expand Up @@ -224,10 +236,14 @@ public function emptyCart( $input, $request_headers = array() ) {
nodes {
key
product {
id
node {
id
}
}
variation {
id
node {
id
}
}
quantity
subtotal
Expand Down Expand Up @@ -301,7 +317,9 @@ public function applyCoupon( $input, $request_headers = array() ) {
nodes {
key
product {
id
node {
id
}
}
quantity
subtotal
Expand Down
64 changes: 58 additions & 6 deletions tests/_support/Helper/crud-helpers/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,68 @@ function( $tax_data ) {
public function print_item_query( $key ) {
$cart = WC()->cart;
$item = $cart->get_cart_item( $key );

$variation = $item['variation_id'];
$variation_data = $item['variation'];

// Stub any variation attributes for later use.
$attributes = array();
if ( ! empty( $variation ) ) {
$variation = \wc_get_product( $variation );
foreach ( $variation->get_attributes() as $name => $default_value ) {
if ( isset( $variation_data["attribute_{$name}"] ) ) {
$value = $variation_data["attribute_{$name}"];
} else {
$value = $default_value;
}

$attribute = array(
'id' => base64_encode( $variation->get_id() . '||' . $name . '||' . $value ),
'label' => ucwords( str_replace( '_', ' ', \wc_attribute_taxonomy_slug( $name ) ) ),
);

$term = \get_term_by( 'slug', $value, $name );
if ( empty( $term ) ) {
$attribute = array_merge(
$attribute,
array(
'attributeId' => 0,
'name' => $name,
'value' => $value,
)
);
} else {
$attribute = array_merge(
$attribute,
array(
'attributeId' => $term->term_id,
'name' => $term->taxonomy,
'value' => $term->name,
)
);
}

$attributes[] = $attribute;
}
}

return array(
'key' => $item['key'],
'product' => array(
'id' => Relay::toGlobalId( 'product', $item['product_id'] ),
'databaseId' => $item['product_id'],
),
'variation' => array(
'id' => Relay::toGlobalId( 'product_variation', $item['variation_id'] ),
'databaseId' => $item['variation_id']
'node' => array(
'id' => Relay::toGlobalId( 'product', $item['product_id'] ),
'databaseId' => $item['product_id'],
),
),
'variation' => ! empty( $variation )
? array(
'attributes' => $attributes,
'node' => array(
'id' => Relay::toGlobalId( 'product_variation', $item['variation_id'] ),
'databaseId' => $item['variation_id'],
),
)
: null,
'quantity' => $item['quantity'],
'subtotal' => \wc_graphql_price( $item['line_subtotal'] ),
'subtotalTax' => \wc_graphql_price( $item['line_subtotal_tax'] ),
Expand Down
Loading

0 comments on commit fb6578d

Please sign in to comment.