File tree Expand file tree Collapse file tree 5 files changed +112
-3
lines changed
_support/Helper/crud-helpers Expand file tree Collapse file tree 5 files changed +112
-3
lines changed Original file line number Diff line number Diff line change @@ -10,9 +10,12 @@ const config = {
1010 "title" : "WooGraphQL Documentation" ,
1111 "githubUrl" : "https://github.com/wp-graphql/wp-graphql-woocommerce" ,
1212 "helpUrl" : "https://github.com/wp-graphql/wp-graphql-woocommerce/issues" ,
13- "tweetText" : "" ,
13+ "tweetText" : "Take a look at the amazing Docs Site for @woographql https://woographql.axistaylor.com " ,
1414 "links" : [
15- { "text" : "" , "link" : "" }
15+ {
16+ "text" : "AxisTaylor" ,
17+ "link" : "https://axistaylor.com"
18+ }
1619 ] ,
1720 "search" : {
1821 "enabled" : false ,
Original file line number Diff line number Diff line change @@ -22,6 +22,13 @@ class Downloadable_Items {
2222 public static function register_connections () {
2323 // From Order.
2424 register_graphql_connection ( self ::get_connection_config () );
25+
26+ // From Customer.
27+ register_graphql_connection (
28+ self ::get_connection_config (
29+ array ( 'fromType ' => 'Customer ' )
30+ )
31+ );
2532 }
2633
2734 /**
Original file line number Diff line number Diff line change 1515use GraphQLRelay \Connection \ArrayConnection ;
1616use WPGraphQL \AppContext ;
1717use WPGraphQL \Data \Connection \AbstractConnectionResolver ;
18+ use WPGraphQL \WooCommerce \Model \Customer ;
1819
1920/**
2021 * Class Downloadable_Item_Connection_Resolver
@@ -103,7 +104,12 @@ public function get_query_args() {
103104 * @return \WP_Query
104105 */
105106 public function get_query () {
106- $ items = $ this ->source ->downloadable_items ;
107+ $ items = 0 ;
108+ if ( is_a ( $ this ->source , Customer::class ) ) {
109+ $ items = wc_get_customer_available_downloads ( $ this ->source ->ID );
110+ } else {
111+ $ items = $ this ->source ->downloadable_items ;
112+ }
107113
108114 if ( empty ( $ items ) ) {
109115 return array ();
Original file line number Diff line number Diff line change @@ -205,4 +205,29 @@ public function print_failed_query( $id ) {
205205 'jwtRefreshToken ' => null ,
206206 );
207207 }
208+
209+ public function print_downloadables ( $ id ) {
210+ $ items = wc_get_customer_available_downloads ( $ id );
211+
212+ if ( empty ( $ items ) ) {
213+ return array ();
214+ }
215+
216+ $ nodes = array ();
217+ foreach ( $ items as $ item ) {
218+ $ nodes [] = array (
219+ 'url ' => $ item ['download_url ' ],
220+ 'accessExpires ' => $ item ['access_expires ' ],
221+ 'downloadId ' => $ item ['download_id ' ],
222+ 'downloadsRemaining ' => isset ( $ item ['downloads_remaining ' ] ) && 'integer ' === gettype ( $ item ['downloads_remaining ' ] )
223+ ? $ item ['downloads_remaining ' ]
224+ : null ,
225+ 'name ' => $ item ['download_name ' ],
226+ 'product ' => array ( 'productId ' => $ item ['product_id ' ] ),
227+ 'download ' => array ( 'downloadId ' => $ item ['download_id ' ] ),
228+ );
229+ }
230+
231+ return $ nodes ;
232+ }
208233}
Original file line number Diff line number Diff line change @@ -320,4 +320,72 @@ function( $product_id ) {
320320 $ this ->assertEquals ( $ expected , $ actual );
321321 }
322322
323+ public function testCustomerToDownloadableItemsQuery () {
324+ $ downloadable_product = $ this ->products ->create_simple (
325+ array (
326+ 'downloadable ' => true ,
327+ 'downloads ' => array ( $ this ->products ->create_download () )
328+ )
329+ );
330+ $ order_id = $ this ->orders ->create (
331+ array (
332+ 'status ' => 'completed ' ,
333+ 'customer_id ' => $ this ->customer ,
334+ ),
335+ array (
336+ 'line_items ' => array (
337+ array (
338+ 'product ' => $ downloadable_product ,
339+ 'qty ' => 1 ,
340+ ),
341+ ),
342+ )
343+ );
344+
345+ // Force download permission updated.
346+ wc_downloadable_product_permissions ( $ order_id , true );
347+
348+ $ query = '
349+ query {
350+ customer {
351+ downloadableItems(first: 1) {
352+ nodes {
353+ url
354+ accessExpires
355+ downloadId
356+ downloadsRemaining
357+ name
358+ product {
359+ productId
360+ }
361+ download {
362+ downloadId
363+ }
364+ }
365+ }
366+ }
367+ }
368+ ' ;
369+
370+ /**
371+ * Assertion One
372+ *
373+ * tests query results
374+ */
375+ wp_set_current_user ( $ this ->customer );
376+ $ actual = graphql ( array ( 'query ' => $ query ) );
377+ $ expected = array (
378+ 'data ' => array (
379+ 'customer ' => array (
380+ 'downloadableItems ' => array ( 'nodes ' => $ this ->customers ->print_downloadables ( $ this ->customer ) ),
381+ ),
382+ ),
383+ );
384+
385+ // use --debug flag to view.
386+ codecept_debug ( $ actual );
387+
388+ $ this ->assertEquals ( $ expected , $ actual );
389+ }
390+
323391}
You can’t perform that action at this time.
0 commit comments