Merge pull request #238 from kidunot89/feature/customer-downloadable-items

Adds Customer to DownloadableItems connection
This commit is contained in:
Geoffrey K Taylor
2020-02-19 19:52:58 -05:00
committed by GitHub
5 changed files with 112 additions and 3 deletions
+5 -2
View File
@@ -10,9 +10,12 @@ const config = {
"title": "WooGraphQL Documentation",
"githubUrl": "https://github.com/wp-graphql/wp-graphql-woocommerce",
"helpUrl": "https://github.com/wp-graphql/wp-graphql-woocommerce/issues",
"tweetText": "",
"tweetText": "Take a look at the amazing Docs Site for @woographql https://woographql.axistaylor.com",
"links": [
{ "text": "", "link": ""}
{
"text": "AxisTaylor",
"link": "https://axistaylor.com"
}
],
"search": {
"enabled": false,
@@ -22,6 +22,13 @@ class Downloadable_Items {
public static function register_connections() {
// From Order.
register_graphql_connection( self::get_connection_config() );
// From Customer.
register_graphql_connection(
self::get_connection_config(
array( 'fromType' => 'Customer' )
)
);
}
/**
@@ -15,6 +15,7 @@ use GraphQLRelay\Relay;
use GraphQLRelay\Connection\ArrayConnection;
use WPGraphQL\AppContext;
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
use WPGraphQL\WooCommerce\Model\Customer;
/**
* Class Downloadable_Item_Connection_Resolver
@@ -103,7 +104,12 @@ class Downloadable_Item_Connection_Resolver extends AbstractConnectionResolver {
* @return \WP_Query
*/
public function get_query() {
$items = $this->source->downloadable_items;
$items = 0;
if ( is_a( $this->source, Customer::class ) ) {
$items = wc_get_customer_available_downloads( $this->source->ID );
} else {
$items = $this->source->downloadable_items;
}
if ( empty( $items ) ) {
return array();
@@ -205,4 +205,29 @@ class CustomerHelper extends WCG_Helper {
'jwtRefreshToken' => null,
);
}
public function print_downloadables( $id ) {
$items = wc_get_customer_available_downloads( $id );
if ( empty( $items ) ) {
return array();
}
$nodes = array();
foreach ( $items as $item ) {
$nodes[] = array(
'url' => $item['download_url'],
'accessExpires' => $item['access_expires'],
'downloadId' => $item['download_id'],
'downloadsRemaining' => isset( $item['downloads_remaining'] ) && 'integer' === gettype( $item['downloads_remaining'] )
? $item['downloads_remaining']
: null,
'name' => $item['download_name'],
'product' => array( 'productId' => $item['product_id'] ),
'download' => array( 'downloadId' => $item['download_id'] ),
);
}
return $nodes;
}
}
@@ -320,4 +320,72 @@ class DownloadableItemQueriesTest extends \Codeception\TestCase\WPTestCase
$this->assertEquals( $expected, $actual );
}
public function testCustomerToDownloadableItemsQuery() {
$downloadable_product = $this->products->create_simple(
array(
'downloadable' => true,
'downloads' => array( $this->products->create_download() )
)
);
$order_id = $this->orders->create(
array(
'status' => 'completed',
'customer_id' => $this->customer,
),
array(
'line_items' => array(
array(
'product' => $downloadable_product,
'qty' => 1,
),
),
)
);
// Force download permission updated.
wc_downloadable_product_permissions( $order_id, true );
$query = '
query {
customer {
downloadableItems(first: 1) {
nodes {
url
accessExpires
downloadId
downloadsRemaining
name
product {
productId
}
download {
downloadId
}
}
}
}
}
';
/**
* Assertion One
*
* tests query results
*/
wp_set_current_user( $this->customer );
$actual = graphql( array( 'query' => $query ) );
$expected = array(
'data' => array(
'customer' => array(
'downloadableItems' => array( 'nodes' => $this->customers->print_downloadables( $this->customer ) ),
),
),
);
// use --debug flag to view.
codecept_debug( $actual );
$this->assertEquals( $expected, $actual );
}
}