Skip to content

Commit

Permalink
Merge pull request #102 from kidunot89/feature/product-category-image
Browse files Browse the repository at this point in the history
Adds product category image to schema.
  • Loading branch information
kidunot89 authored Jul 10, 2019
2 parents eccbcba + 0171ff5 commit 6f253a8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 21 deletions.
26 changes: 6 additions & 20 deletions includes/class-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace WPGraphQL\Extensions\WooCommerce;

use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Backorders;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Catalog_Visibility;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Countries;
Expand Down Expand Up @@ -47,6 +49,7 @@
use WPGraphQL\Extensions\WooCommerce\Type\WPObject\Shipping_Method_Type;
use WPGraphQL\Extensions\WooCommerce\Type\WPObject\Cart_Type;
use WPGraphQL\Extensions\WooCommerce\Type\WPObject\Variation_Attribute_Type;
use WPGraphQL\Extensions\WooCommerce\Type\WPObject\Product_Category_Type;
use WPGraphQL\Extensions\WooCommerce\Connection\Posts;
use WPGraphQL\Extensions\WooCommerce\Connection\WC_Terms;
use WPGraphQL\Extensions\WooCommerce\Connection\Coupons;
Expand Down Expand Up @@ -141,6 +144,9 @@ public static function graphql_register_types() {
Cart_Type::register();
Variation_Attribute_Type::register();

// Object fields.
Product_Category_Type::register_fields();

// Connections.
Posts::register_connections();
WC_Terms::register_connections();
Expand Down Expand Up @@ -171,25 +177,5 @@ public static function graphql_register_types() {
Order_Update::register_mutation();
Order_Delete::register_mutation();
Checkout::register_mutation();

if ( class_exists( '\WPGraphQL\JWT_Authentication\ManageTokens' ) ) {
$fields = array();
foreach ( \WPGraphQL\JWT_Authentication\ManageTokens::add_user_fields() as $field_name => $field ) {
$root_resolver = $field['resolve'];
$fields[ $field_name ] = array_merge(
$field,
array(
'resolve' => function( $source ) use ( $root_resolver ) {
$user = get_user_by( 'id', $source->ID );
return $root_resolver( $user );
},
)
);
}
register_graphql_fields(
'Customer',
$fields
);
}
}
}
20 changes: 20 additions & 0 deletions includes/type/object/class-customer-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,25 @@ public static function register() {
},
)
);

if ( class_exists( '\WPGraphQL\JWT_Authentication\ManageTokens' ) ) {
$fields = array();
foreach ( \WPGraphQL\JWT_Authentication\ManageTokens::add_user_fields() as $field_name => $field ) {
$root_resolver = $field['resolve'];
$fields[ $field_name ] = array_merge(
$field,
array(
'resolve' => function( $source ) use ( $root_resolver ) {
$user = get_user_by( 'id', $source->ID );
return $root_resolver( $user );
},
)
);
}
register_graphql_fields(
'Customer',
$fields
);
}
}
}
41 changes: 41 additions & 0 deletions includes/type/object/class-product-category-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* WPObjectType - ProductCategory
*
* Registers fields for the ProductCategory type
*
* @package \WPGraphQL\Extensions\WooCommerce\Type\WPObject
* @since 0.2.0
*/

namespace WPGraphQL\Extensions\WooCommerce\Type\WPObject;

use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;
use WPGraphQL\Type\WPObjectType;
use WPGraphQL\Extensions\WooCommerce\Data\Factory;

/**
* Class - Product_Category_Type
*/
class Product_Category_Type {
/**
* Registers fields to ProductCategory
*/
public static function register_fields() {
register_graphql_field(
'ProductCategory',
'image',
array(
'type' => 'MediaItem',
'description' => __( 'Product category image', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context ) {
$thumbnail_id = get_term_meta( $source->term_id, 'thumbnail_id', true );
return ! empty( $thumbnail_id )
? DataSource::resolve_post_object( $thumbnail_id, $context )
: null;
},
)
);
}
}
10 changes: 9 additions & 1 deletion tests/wpunit/ProductQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ public function setUp() {
'post_content' => 'product image',
)
);
$category_id = $this->helper->create_product_category( $this->product_cat );
$this->product = $this->helper->create_simple(
array(
'tag_ids' => array( $this->helper->create_product_tag( $this->product_tag ) ),
'category_ids' => array( $this->helper->create_product_category( $this->product_cat ) ),
'category_ids' => array( $category_id ),
'image_id' => $this->image_id,
'gallery_image_ids' => array( $this->image_id ),
'downloads' => array( ProductHelper::create_download() ),
'slug' => 'product-slug',
'sku' => 'product-sku',
)
);
update_term_meta( $category_id, 'thumbnail_id', $this->image_id );
}

public function tearDown() {
Expand Down Expand Up @@ -526,6 +528,9 @@ public function testTermToProductConnection() {
productCategories( where: { hideEmpty: true } ) {
nodes {
name
image {
id
}
products {
nodes {
id
Expand Down Expand Up @@ -557,6 +562,9 @@ public function testTermToProductConnection() {
'nodes' => array(
array(
'name' => $this->product_cat,
'image' => array(
'id' => Relay::toGlobalId( 'attachment', $this->image_id ),
),
'products' => array(
'nodes' => array(
array (
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Order_Item_Type' => $baseDir . '/includes/type/object/class-order-item-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Order_Type' => $baseDir . '/includes/type/object/class-order-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Attribute_Type' => $baseDir . '/includes/type/object/class-product-attribute-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Category_Type' => $baseDir . '/includes/type/object/class-product-category-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Download_Type' => $baseDir . '/includes/type/object/class-product-download-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Type' => $baseDir . '/includes/type/object/class-product-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Variation_Type' => $baseDir . '/includes/type/object/class-product-variation-type.php',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class ComposerStaticInitee0d17af17b841ed3a93c4a0e5cc5e5f
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Order_Item_Type' => __DIR__ . '/../..' . '/includes/type/object/class-order-item-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Order_Type' => __DIR__ . '/../..' . '/includes/type/object/class-order-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Attribute_Type' => __DIR__ . '/../..' . '/includes/type/object/class-product-attribute-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Category_Type' => __DIR__ . '/../..' . '/includes/type/object/class-product-category-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Download_Type' => __DIR__ . '/../..' . '/includes/type/object/class-product-download-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Type' => __DIR__ . '/../..' . '/includes/type/object/class-product-type.php',
'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Variation_Type' => __DIR__ . '/../..' . '/includes/type/object/class-product-variation-type.php',
Expand Down

0 comments on commit 6f253a8

Please sign in to comment.