Skip to content

Commit fb6578d

Browse files
authored
Merge pull request #383 from wp-graphql/release/v0.7.0
Release v0.7.0
2 parents 2cd50a1 + b3c649d commit fb6578d

File tree

8 files changed

+249
-87
lines changed

8 files changed

+249
-87
lines changed

includes/data/connection/class-variation-attribute-connection-resolver.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class Variation_Attribute_Connection_Resolver {
2323
/**
2424
* Returns data array from WC_Product_Attribute ArrayAccess object.
2525
*
26-
* @param WC_Product_Attribute $attrs - WC_Product_Attribute object.
27-
* @param string $parent_id - ProductVariation Relay ID.
26+
* @param array $attrs WC_Product_Attribute object.
27+
* @param string $parent_id ProductVariation Relay ID.
2828
*
2929
* @return array
3030
*/
31-
public function to_data_array( $attrs = array(), $parent_id = 0 ) {
31+
public static function to_data_array( $attrs = array(), $parent_id = 0 ) {
3232
$attributes = array();
3333
if ( array( '0' ) !== $attrs ) {
3434
foreach ( $attrs as $name => $value ) {
@@ -66,9 +66,9 @@ public function to_data_array( $attrs = array(), $parent_id = 0 ) {
6666
*/
6767
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) {
6868
if ( is_a( $source, Product::class ) ) {
69-
$attributes = $this->to_data_array( $source->default_attributes, $source->ID );
69+
$attributes = self::to_data_array( $source->default_attributes, $source->ID );
7070
} else {
71-
$attributes = $this->to_data_array( $source->attributes, $source->ID );
71+
$attributes = self::to_data_array( $source->attributes, $source->ID );
7272
}
7373

7474
$connection = Relay::connectionFromArray( $attributes, $args );

includes/type/object/class-cart-type.php

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use GraphQL\Error\UserError;
1414
use GraphQL\Type\Definition\ResolveInfo;
1515
use WPGraphQL\AppContext;
16+
use WPGraphQL\WooCommerce\Data\Connection\Variation_Attribute_Connection_Resolver;
17+
use WPGraphQL\WooCommerce\Data\Connection\Product_Connection_Resolver;
1618
use WPGraphQL\WooCommerce\Data\Factory;
1719

1820
/**
@@ -27,6 +29,7 @@ public static function register() {
2729
self::register_cart_fee();
2830
self::register_cart_tax();
2931
self::register_cart_item();
32+
self::register_cart_item_connections();
3033
self::register_cart();
3134
}
3235

@@ -173,7 +176,7 @@ public static function register_cart() {
173176
return \wc_graphql_price( $price );
174177
},
175178
),
176-
'totalTaxes' => array(
179+
'totalTaxes' => array(
177180
'type' => array( 'list_of' => 'CartTax' ),
178181
'description' => __( 'Cart total taxes itemized', 'wp-graphql-woocommerce' ),
179182
'resolve' => function( $source ) {
@@ -235,24 +238,6 @@ public static function register_cart_item() {
235238
return ! empty( $source['key'] ) ? $source['key'] : null;
236239
},
237240
),
238-
'product' => array(
239-
'type' => 'Product',
240-
'description' => __( 'A product in the cart', 'wp-graphql-woocommerce' ),
241-
'resolve' => function( $source, array $args, AppContext $context ) {
242-
return ! empty( $source['product_id'] )
243-
? Factory::resolve_crud_object( $source['product_id'], $context )
244-
: null;
245-
},
246-
),
247-
'variation' => array(
248-
'type' => 'ProductVariation',
249-
'description' => __( 'Selected variation of the product', 'wp-graphql-woocommerce' ),
250-
'resolve' => function( $source, array $args, AppContext $context ) {
251-
return ! empty( $source['variation_id'] )
252-
? Factory::resolve_crud_object( $source['variation_id'], $context )
253-
: null;
254-
},
255-
),
256241
'quantity' => array(
257242
'type' => 'Int',
258243
'description' => __( 'Quantity of the product', 'wp-graphql-woocommerce' ),
@@ -297,6 +282,68 @@ public static function register_cart_item() {
297282
);
298283
}
299284

285+
/**
286+
* Registers one-to-one connections for CartItem.
287+
*/
288+
public static function register_cart_item_connections() {
289+
register_graphql_connection(
290+
array(
291+
'fromType' => 'CartItem',
292+
'toType' => 'Product',
293+
'fromFieldName' => 'product',
294+
'oneToOne' => true,
295+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
296+
$id = $source['product_id'];
297+
$resolver = new Product_Connection_Resolver( $source, $args, $context, $info );
298+
299+
return $resolver->one_to_one()
300+
->set_query_arg( 'p', $id )
301+
->get_connection();
302+
},
303+
)
304+
);
305+
306+
register_graphql_connection(
307+
array(
308+
'fromType' => 'CartItem',
309+
'toType' => 'ProductVariation',
310+
'fromFieldName' => 'variation',
311+
'oneToOne' => true,
312+
'edgeFields' => array(
313+
'attributes' => array(
314+
'type' => array( 'list_of' => 'VariationAttribute' ),
315+
'description' => __( 'Attributes of the variation.', 'wp-graphql-woocommerce' ),
316+
'resolve' => function( $source ) {
317+
$attributes = array();
318+
319+
$variation = $source['node'];
320+
$cart_item_data = $source['source'];
321+
$cart_variation_data = $cart_item_data['variation'];
322+
foreach( $variation->attributes as $name => $default_value ) {
323+
if ( isset( $cart_variation_data["attribute_{$name}"] ) ) {
324+
$attributes[ $name ] = $cart_variation_data["attribute_{$name}"];
325+
} else {
326+
$attributes[ $name ] = $default_value;
327+
}
328+
}
329+
330+
return Variation_Attribute_Connection_Resolver::to_data_array( $attributes, $variation->ID );
331+
},
332+
),
333+
),
334+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
335+
$id = $source['variation_id'];
336+
$resolver = new Product_Connection_Resolver( $source, $args, $context, $info );
337+
338+
return $resolver->one_to_one()
339+
->set_query_arg( 'post_type', 'product_variation' )
340+
->set_query_arg( 'p', $id )
341+
->get_connection();
342+
},
343+
)
344+
);
345+
}
346+
300347
/**
301348
* Registers CartFee type
302349
*/

includes/type/object/class-variation-attribute-type.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ public static function register() {
2626
'fields' => array(
2727
'id' => array(
2828
'type' => array( 'non_null' => 'ID' ),
29-
'description' => __( 'The Id of the order. Equivalent to WP_Post->ID', 'wp-graphql-woocommerce' ),
29+
'description' => __( 'The Global ID of the attribute.', 'wp-graphql-woocommerce' ),
3030
'resolve' => function ( $source ) {
3131
return isset( $source['id'] ) ? $source['id'] : null;
3232
},
3333
),
3434
'attributeId' => array(
3535
'type' => 'Int',
36-
'description' => __( 'The Id of the order. Equivalent to WP_Post->ID', 'wp-graphql-woocommerce' ),
36+
'description' => __( 'The Database ID of the attribute.', 'wp-graphql-woocommerce' ),
3737
'resolve' => function ( $source ) {
3838
return isset( $source['attributeId'] ) ? $source['attributeId'] : null;
3939
},

tests/_support/Helper/GraphQLE2E.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,14 @@ public function addToCart( $input, $request_headers = array() ) {
7373
cartItem {
7474
key
7575
product {
76-
id
76+
node {
77+
id
78+
}
7779
}
7880
variation {
79-
id
81+
node {
82+
id
83+
}
8084
}
8185
quantity
8286
subtotal
@@ -148,10 +152,14 @@ public function removeItemsFromCart( $input, $request_headers = array() ) {
148152
cartItems {
149153
key
150154
product {
151-
id
155+
node {
156+
id
157+
}
152158
}
153159
variation {
154-
id
160+
node {
161+
id
162+
}
155163
}
156164
quantity
157165
subtotal
@@ -185,10 +193,14 @@ public function restoreCartItems( $input, $request_headers = array() ) {
185193
cartItems {
186194
key
187195
product {
188-
id
196+
node {
197+
id
198+
}
189199
}
190200
variation {
191-
id
201+
node {
202+
id
203+
}
192204
}
193205
quantity
194206
subtotal
@@ -224,10 +236,14 @@ public function emptyCart( $input, $request_headers = array() ) {
224236
nodes {
225237
key
226238
product {
227-
id
239+
node {
240+
id
241+
}
228242
}
229243
variation {
230-
id
244+
node {
245+
id
246+
}
231247
}
232248
quantity
233249
subtotal
@@ -301,7 +317,9 @@ public function applyCoupon( $input, $request_headers = array() ) {
301317
nodes {
302318
key
303319
product {
304-
id
320+
node {
321+
id
322+
}
305323
}
306324
quantity
307325
subtotal

tests/_support/Helper/crud-helpers/cart.php

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,68 @@ function( $tax_data ) {
7070
public function print_item_query( $key ) {
7171
$cart = WC()->cart;
7272
$item = $cart->get_cart_item( $key );
73+
74+
$variation = $item['variation_id'];
75+
$variation_data = $item['variation'];
76+
77+
// Stub any variation attributes for later use.
78+
$attributes = array();
79+
if ( ! empty( $variation ) ) {
80+
$variation = \wc_get_product( $variation );
81+
foreach ( $variation->get_attributes() as $name => $default_value ) {
82+
if ( isset( $variation_data["attribute_{$name}"] ) ) {
83+
$value = $variation_data["attribute_{$name}"];
84+
} else {
85+
$value = $default_value;
86+
}
87+
88+
$attribute = array(
89+
'id' => base64_encode( $variation->get_id() . '||' . $name . '||' . $value ),
90+
'label' => ucwords( str_replace( '_', ' ', \wc_attribute_taxonomy_slug( $name ) ) ),
91+
);
92+
93+
$term = \get_term_by( 'slug', $value, $name );
94+
if ( empty( $term ) ) {
95+
$attribute = array_merge(
96+
$attribute,
97+
array(
98+
'attributeId' => 0,
99+
'name' => $name,
100+
'value' => $value,
101+
)
102+
);
103+
} else {
104+
$attribute = array_merge(
105+
$attribute,
106+
array(
107+
'attributeId' => $term->term_id,
108+
'name' => $term->taxonomy,
109+
'value' => $term->name,
110+
)
111+
);
112+
}
113+
114+
$attributes[] = $attribute;
115+
}
116+
}
117+
73118
return array(
74119
'key' => $item['key'],
75120
'product' => array(
76-
'id' => Relay::toGlobalId( 'product', $item['product_id'] ),
77-
'databaseId' => $item['product_id'],
78-
),
79-
'variation' => array(
80-
'id' => Relay::toGlobalId( 'product_variation', $item['variation_id'] ),
81-
'databaseId' => $item['variation_id']
121+
'node' => array(
122+
'id' => Relay::toGlobalId( 'product', $item['product_id'] ),
123+
'databaseId' => $item['product_id'],
124+
),
82125
),
126+
'variation' => ! empty( $variation )
127+
? array(
128+
'attributes' => $attributes,
129+
'node' => array(
130+
'id' => Relay::toGlobalId( 'product_variation', $item['variation_id'] ),
131+
'databaseId' => $item['variation_id'],
132+
),
133+
)
134+
: null,
83135
'quantity' => $item['quantity'],
84136
'subtotal' => \wc_graphql_price( $item['line_subtotal'] ),
85137
'subtotalTax' => \wc_graphql_price( $item['line_subtotal_tax'] ),

0 commit comments

Comments
 (0)