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,66 @@ 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+ $ count = 0 ;
59+ foreach ( $ items as $ item ) {
60+ $ count += $ item ['quantity ' ];
61+ }
62+
63+ return $ count ;
64+ },
65+ ),
66+ 'productCount ' => array (
67+ 'type ' => 'Int ' ,
68+ 'description ' => __ ( 'Total number of different products in the cart ' , 'wp-graphql-woocommerce ' ),
69+ 'resolve ' => function ( $ source ) {
70+ if ( empty ( $ source ['edges ' ] ) ) {
71+ return 0 ;
72+ }
6773
68- $ items = array_values ( $ source ['edges ' ][0 ]['source ' ]->get_cart () );
69- return count ( $ items );
70- } ,
74+ return count ( array_values ( $ source ['edges ' ][0 ]['source ' ]->get_cart () ) );
75+ },
76+ ) ,
7177 ),
78+ 'resolve ' => function ( $ source , array $ args , AppContext $ context , ResolveInfo $ info ) {
79+ return Factory::resolve_cart_item_connection ( $ source , $ args , $ context , $ info );
80+ },
7281 ),
73- 'resolve ' => function ( $ source , $ args , $ context , $ info ) {
74- return Factory::resolve_cart_item_connection ( $ source , $ args , $ context , $ info );
75- },
82+ $ args
7683 );
77- return array_merge ( $ defaults , $ args );
7884 }
7985
8086 /**
81- * Returns array of where args
87+ * Returns array of where args.
8288 *
8389 * @return array
8490 */
85- public static function get_connection_args () {
91+ public static function get_connection_args (): array {
8692 return array (
8793 'needsShipping ' => array (
8894 'type ' => 'Boolean ' ,
0 commit comments