Skip to content

Commit 0603f83

Browse files
authored
Merge pull request #238 from kidunot89/feature/customer-downloadable-items
Adds Customer to DownloadableItems connection
2 parents 8b7fc3e + c636794 commit 0603f83

File tree

5 files changed

+112
-3
lines changed

5 files changed

+112
-3
lines changed

docs/config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff 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,

includes/connection/class-downloadable-items.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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
/**

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use GraphQLRelay\Connection\ArrayConnection;
1616
use WPGraphQL\AppContext;
1717
use 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();

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

tests/wpunit/DownloadableItemQueriesTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)