Skip to content

Commit

Permalink
Two new fields added to the "ProductCategory" type.
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Jul 7, 2020
1 parent 7a3fa63 commit 92ef2c8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
1 change: 1 addition & 0 deletions includes/class-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
\WPGraphQL\WooCommerce\Type\WPEnum\Order_Status::register();
\WPGraphQL\WooCommerce\Type\WPEnum\Product_Types::register();
\WPGraphQL\WooCommerce\Type\WPEnum\Product_Attribute_Types::register();
\WPGraphQL\WooCommerce\Type\WPEnum\Product_Category_Display::register();
\WPGraphQL\WooCommerce\Type\WPEnum\Stock_Status::register();
\WPGraphQL\WooCommerce\Type\WPEnum\Tax_Class::register();
\WPGraphQL\WooCommerce\Type\WPEnum\Tax_Status::register();
Expand Down
44 changes: 44 additions & 0 deletions includes/type/enum/class-product-category-display.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* WPEnum Type - ProductCategoryDisplay
*
* @package WPGraphQL\WooCommerce\Type\WPEnum
* @since 0.6.0
*/

namespace WPGraphQL\WooCommerce\Type\WPEnum;

/**
* Class Product_Category_Display
*/
class Product_Category_Display {
/**
* Registers type
*/
public static function register() {
register_graphql_enum_type(
'ProductCategoryDisplay',
array(
'description' => __( 'Product category display type enumeration', 'wp-graphql-woocommerce' ),
'values' => array(
'DEFAULT' => array(
'value' => 'default',
'description' => __( 'Display default content connected to this category.', 'wp-graphql-woocommerce' ),
),
'PRODUCTS' => array(
'value' => 'products',
'description' => __( 'Display products associated with this category.', 'wp-graphql-woocommerce' ),
),
'SUBCATEGORIES' => array(
'value' => 'subcategories',
'description' => __( 'Display subcategories of this category.', 'wp-graphql-woocommerce' ),
),
'BOTH' => array(
'value' => 'both',
'description' => __( 'Display both products and subcategories of this category.', 'wp-graphql-woocommerce' ),
),
),
)
);
}
}
37 changes: 27 additions & 10 deletions includes/type/object/class-product-category-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,35 @@ class Product_Category_Type {
* Registers fields to ProductCategory.
*/
public static function register_fields() {
register_graphql_field(
register_graphql_fields(
'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;
},
'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;
},
),
'display' => array(
'type' => 'ProductCategoryDisplay',
'description' => __( 'Product category display type', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context ) {
$display = get_term_meta( $source->term_id, 'display_type', true );
return ! empty( $display ) ? $display : 'default';
},
),
'order' => array(
'type' => 'Integer',
'description' => __( 'Product category menu order', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context ) {
$order = get_term_meta( $source->term_id, 'order', true );
return ! empty( $order ) ? $order : 0;
},
),
)
);
}
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\\WooCommerce\\Type\\WPEnum\\Post_Type_Orderby_Enum' => $baseDir . '/includes/type/enum/class-post-type-orderby-enum.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Pricing_Field_Format' => $baseDir . '/includes/type/enum/class-pricing-field-format.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Product_Attribute_Types' => $baseDir . '/includes/type/enum/class-product-attribute-types.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Product_Category_Display' => $baseDir . '/includes/type/enum/class-product-category-display.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Product_Taxonomy' => $baseDir . '/includes/type/enum/class-product-taxonomy.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Product_Types' => $baseDir . '/includes/type/enum/class-product-types.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Products_Orderby_Enum' => $baseDir . '/includes/type/enum/class-products-orderby-enum.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 @@ -126,6 +126,7 @@ class ComposerStaticInit6ecb3b3e822b795cab805ed5884032b2
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Post_Type_Orderby_Enum' => __DIR__ . '/../..' . '/includes/type/enum/class-post-type-orderby-enum.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Pricing_Field_Format' => __DIR__ . '/../..' . '/includes/type/enum/class-pricing-field-format.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Product_Attribute_Types' => __DIR__ . '/../..' . '/includes/type/enum/class-product-attribute-types.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Product_Category_Display' => __DIR__ . '/../..' . '/includes/type/enum/class-product-category-display.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Product_Taxonomy' => __DIR__ . '/../..' . '/includes/type/enum/class-product-taxonomy.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Product_Types' => __DIR__ . '/../..' . '/includes/type/enum/class-product-types.php',
'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Products_Orderby_Enum' => __DIR__ . '/../..' . '/includes/type/enum/class-products-orderby-enum.php',
Expand Down

0 comments on commit 92ef2c8

Please sign in to comment.