1010
1111namespace WPGraphQL \WooCommerce \Connection ;
1212
13+ use GraphQL \Type \Definition \ResolveInfo ;
14+ use WPGraphQL \AppContext ;
1315use WPGraphQL \WooCommerce \Data \Factory ;
1416
1517/**
1618 * Class - Cart_Items
1719 */
1820class Cart_Items {
21+
1922 /**
2023 * Registers the various connections from other Types to CartItem
2124 */
@@ -26,63 +29,61 @@ public static function register_connections() {
2629
2730 /**
2831 * Given an array of $args, this returns the connection config, merging the provided args
29- * with the defaults
32+ * with the defaults.
3033 *
31- * @access public
3234 * @param array $args - Connection configuration.
33- *
3435 * @return array
3536 */
36- public static function get_connection_config ( $ args = array () ) {
37- $ defaults = array (
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- }
37+ public static function get_connection_config ( $ args = array () ): array {
38+ return array_merge (
39+ array (
40+ 'fromType ' => 'Cart ' ,
41+ 'toType ' => 'CartItem ' ,
42+ 'fromFieldName ' => 'contents ' ,
43+ 'connectionArgs ' => self ::get_connection_args (),
44+ 'connectionFields ' => array (
45+ 'itemCount ' => array (
46+ 'type ' => 'Int ' ,
47+ 'description ' => __ ( 'Total number of items in the cart. ' , 'wp-graphql-woocommerce ' ),
48+ 'resolve ' => function ( $ source ) {
49+ if ( empty ( $ source ['edges ' ] ) ) {
50+ return 0 ;
51+ }
5052
51- $ items = array_values ( $ source ['edges ' ][0 ]['source ' ]->get_cart () );
52- $ count = 0 ;
53- foreach ( $ items as $ item ) {
54- $ count += $ item ['quantity ' ];
55- }
53+ $ items = array_values ( $ source ['edges ' ][0 ]['source ' ]->get_cart () );
54+ if ( empty ( $ items ) ) {
55+ return 0 ;
56+ }
5657
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- }
58+ return array_sum ( array_column ( $ items , ' quantity ' ) ) ;
59+ },
60+ ),
61+ 'productCount ' => array (
62+ 'type ' => 'Int ' ,
63+ 'description ' => __ ( 'Total number of different products in the cart ' , 'wp-graphql-woocommerce ' ),
64+ 'resolve ' => function ( $ source ) {
65+ if ( empty ( $ source ['edges ' ] ) ) {
66+ return 0 ;
67+ }
6768
68- $ items = array_values ( $ source ['edges ' ][0 ]['source ' ]->get_cart () );
69- return count ( $ items );
70- } ,
69+ return count ( array_values ( $ source ['edges ' ][0 ]['source ' ]->get_cart () ) );
70+ },
71+ ) ,
7172 ),
73+ 'resolve ' => function ( $ source , array $ args , AppContext $ context , ResolveInfo $ info ) {
74+ return Factory::resolve_cart_item_connection ( $ source , $ args , $ context , $ info );
75+ },
7276 ),
73- 'resolve ' => function ( $ source , $ args , $ context , $ info ) {
74- return Factory::resolve_cart_item_connection ( $ source , $ args , $ context , $ info );
75- },
77+ $ args
7678 );
77- return array_merge ( $ defaults , $ args );
7879 }
7980
8081 /**
81- * Returns array of where args
82+ * Returns array of where args.
8283 *
8384 * @return array
8485 */
85- public static function get_connection_args () {
86+ public static function get_connection_args (): array {
8687 return array (
8788 'needsShipping ' => array (
8889 'type ' => 'Boolean ' ,
0 commit comments