Skip to content

Commit bef3537

Browse files
authored
Merge pull request #215 from kidunot89/feature/cart-item-connection-upgrade
Pagination implemented on cart connection
2 parents 0d60a4c + 9ba819c commit bef3537

File tree

6 files changed

+310
-45
lines changed

6 files changed

+310
-45
lines changed

includes/connection/class-cart-items.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,42 @@ public static function register_connections() {
3535
*/
3636
public static function get_connection_config( $args = array() ) {
3737
$defaults = array(
38-
'fromType' => 'Cart',
39-
'toType' => 'CartItem',
40-
'fromFieldName' => 'contents',
41-
'connectionArgs' => self::get_connection_args(),
42-
'resolve' => function ( $source, $args, $context, $info ) {
38+
'fromType' => 'Cart',
39+
'toType' => 'CartItem',
40+
'fromFieldName' => 'contents',
41+
'connectionArgs' => self::get_connection_args(),
42+
'connectionFields' => array(
43+
'itemCount' => array(
44+
'type' => 'Int',
45+
'description' => __( 'Total number of items in the cart.', 'wp-graphql-woocommerce' ),
46+
'resolve' => function( $source ) {
47+
if ( empty( $source['edges'] ) ) {
48+
return 0;
49+
}
50+
51+
$items = array_values( $source['edges'][0]['source']->get_cart() );
52+
$count = 0;
53+
foreach ( $items as $item ) {
54+
$count += $item['quantity'];
55+
}
56+
57+
return $count;
58+
},
59+
),
60+
'productCount' => array(
61+
'type' => 'Int',
62+
'description' => __( 'Total number of different products in the cart', 'wp-graphql-woocommerce' ),
63+
'resolve' => function( $source ) {
64+
if ( empty( $source['edges'] ) ) {
65+
return 0;
66+
}
67+
68+
$items = array_values( $source['edges'][0]['source']->get_cart() );
69+
return count( $items );
70+
},
71+
),
72+
),
73+
'resolve' => function ( $source, $args, $context, $info ) {
4374
return Factory::resolve_cart_item_connection( $source, $args, $context, $info );
4475
},
4576
);
@@ -55,7 +86,7 @@ public static function get_connection_args() {
5586
return array(
5687
'needShipping' => array(
5788
'type' => 'Boolean',
58-
'description' => __( 'Limit results to cart item that require shipping', 'wp-graphql-woocommerce' ),
89+
'description' => __( 'Limit results to cart items that require shipping', 'wp-graphql-woocommerce' ),
5990
),
6091
);
6192
}

includes/data/class-factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,8 @@ public static function resolve_shipping_method_connection( $source, array $args,
487487
* @access public
488488
*/
489489
public static function resolve_cart_item_connection( $source, array $args, AppContext $context, ResolveInfo $info ) {
490-
$resolver = new Cart_Item_Connection_Resolver();
491-
return $resolver->resolve( $source, $args, $context, $info );
490+
$resolver = new Cart_Item_Connection_Resolver( $source, $args, $context, $info );
491+
return $resolver->get_connection();
492492
}
493493

494494
/**

includes/data/connection/class-cart-item-connection-resolver.php

Lines changed: 91 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,114 @@
1212

1313
use GraphQL\Type\Definition\ResolveInfo;
1414
use GraphQLRelay\Relay;
15+
use GraphQLRelay\Connection\ArrayConnection;
1516
use WPGraphQL\AppContext;
17+
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
1618

1719
/**
1820
* Class Cart_Item_Connection_Resolver
1921
*/
20-
class Cart_Item_Connection_Resolver {
22+
class Cart_Item_Connection_Resolver extends AbstractConnectionResolver {
2123
/**
22-
* Returns an array of cart items filtered based upon query arguments
24+
* Confirms if cart items should be retrieved.
2325
*
24-
* @param array $items - Cart items.
25-
* @param array $args - Query arguments.
26-
*
27-
* @return array
26+
* @return bool
2827
*/
29-
public function filter( $items, $args = array() ) {
30-
$filter_items = array_values( $items );
28+
public function should_execute() {
29+
return true;
30+
}
3131

32-
usort(
33-
$filter_items,
34-
function( $item_a, $item_b ) {
35-
return strcmp( $item_a['key'], $item_b['key'] );
32+
/**
33+
* Creates query arguments array
34+
*/
35+
public function get_query_args() {
36+
$query_args = array();
37+
if ( ! empty( $this->args['where'] ) ) {
38+
$where_args = $this->args['where'];
39+
if ( ! empty( $where_args['needShipping'] ) ) {
40+
$query_args['filters'] = array();
41+
$query_args['filters'][] = function( $cart_item ) {
42+
$product = \WC()->product_factory->get_product( $cart_item['product_id'] );
43+
if ( $product ) {
44+
return $product->needs_shipping();
45+
}
46+
};
3647
}
37-
);
48+
}
3849

39-
return $filter_items;
50+
return $query_args;
4051
}
4152

4253
/**
43-
* Creates connection
54+
* Executes query
4455
*
45-
* @param mixed $source - Connection source Model instance.
46-
* @param array $args - Connection arguments.
47-
* @param AppContext $context - AppContext object.
48-
* @param ResolveInfo $info - ResolveInfo object.
56+
* @return \WP_Query
4957
*/
50-
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) {
51-
$items = $this->filter( $source->get_cart(), $args );
52-
53-
$connection = Relay::connectionFromArray( $items, $args );
54-
$nodes = array();
55-
if ( ! empty( $connection['edges'] ) && is_array( $connection['edges'] ) ) {
56-
foreach ( $connection['edges'] as $edge ) {
57-
$nodes[] = ! empty( $edge['node'] ) ? $edge['node'] : null;
58+
public function get_query() {
59+
$cart_items = array_values( $this->source->get_cart() );
60+
61+
if ( ! empty( $this->query_args['filters'] ) ) {
62+
if ( is_array( $this->query_args['filters'] ) ) {
63+
foreach ( $this->query_args['filters'] as $filter ) {
64+
$cart_items = array_filter( $cart_items, $filter );
65+
}
66+
} else {
67+
$cart_items = array_filter( $cart_items, $this->query_args['filters'] );
5868
}
5969
}
60-
$connection['nodes'] = ! empty( $nodes ) ? $nodes : array();
61-
return $connection;
70+
71+
$cursor_key = $this->get_offset();
72+
$cursor_offset = array_search( $cursor_key, \array_column( $cart_items, 'key' ), true );
73+
74+
if ( ! empty( $this->args['after'] ) ) {
75+
$cart_items = array_splice( $cart_items, $cursor_offset + 1 );
76+
} elseif ( $cursor_offset ) {
77+
$cart_items = array_splice( $cart_items, 0, $cursor_offset );
78+
}
79+
80+
return array_values( $cart_items );
81+
}
82+
83+
/**
84+
* This returns the offset to be used in the $query_args based on the $args passed to the
85+
* GraphQL query.
86+
*
87+
* @return int|mixed
88+
*/
89+
public function get_offset() {
90+
$offset = null;
91+
92+
// Get the offset.
93+
if ( ! empty( $this->args['after'] ) ) {
94+
$offset = $this->args['after'];
95+
} elseif ( ! empty( $this->args['before'] ) ) {
96+
$offset = $this->args['before'];
97+
}
98+
99+
/**
100+
* Return the higher of the two values
101+
*/
102+
return $offset;
103+
}
104+
105+
/**
106+
* Create cursor for cart item node.
107+
*
108+
* @param array $node Cart item.
109+
* @param string $key Cart item key.
110+
*
111+
* @return string
112+
*/
113+
protected function get_cursor_for_node( $node, $key = null ) {
114+
return $node['key'];
115+
}
116+
117+
/**
118+
* Return an array of items from the query
119+
*
120+
* @return array
121+
*/
122+
public function get_items() {
123+
return ! empty( $this->query ) ? $this->query : array();
62124
}
63125
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function ( $query ) {
100100
}
101101

102102
/**
103-
* Confirms the uses has the privileges to query Products
103+
* Confirms the user has the privileges to query the products
104104
*
105105
* @return bool
106106
*/

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,11 @@ public function print_fee_query( $id ) {
6868

6969
public function print_nodes( $processors = array(), $_ = null ) {
7070
$cart = WC()->cart;
71-
$ids = array_keys( $cart->get_cart_contents() );
71+
$ids = array_keys( $cart->get_cart() );
7272
$default_processors = array(
7373
'mapper' => function( $key ) {
7474
return array( 'key' => $key );
7575
},
76-
'sorter' => function( $key_a, $key_b ) {
77-
return strcmp( $key_a, $key_b );
78-
},
7976
'filter' => function( $key ) {
8077
return true;
8178
}
@@ -84,9 +81,6 @@ public function print_nodes( $processors = array(), $_ = null ) {
8481
$processors = array_merge( $default_processors, $processors );
8582

8683
$results = array_filter( $ids, $processors['filter'] );
87-
if( ! empty( $results ) ) {
88-
usort( $results, $processors['sorter'] );
89-
}
9084

9185
return array_values( array_map( $processors['mapper'], $results ) );
9286
}

0 commit comments

Comments
 (0)