From 92ef2c8053c6b564016b29a35f1eb97e1d6694ba Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Tue, 7 Jul 2020 12:03:17 -0400 Subject: [PATCH] Two new fields added to the "ProductCategory" type. --- includes/class-type-registry.php | 1 + .../enum/class-product-category-display.php | 44 +++++++++++++++++++ .../object/class-product-category-type.php | 37 +++++++++++----- vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + 5 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 includes/type/enum/class-product-category-display.php diff --git a/includes/class-type-registry.php b/includes/class-type-registry.php index 35e0a6c4b..0464e592d 100644 --- a/includes/class-type-registry.php +++ b/includes/class-type-registry.php @@ -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(); diff --git a/includes/type/enum/class-product-category-display.php b/includes/type/enum/class-product-category-display.php new file mode 100644 index 000000000..4b98ec4c6 --- /dev/null +++ b/includes/type/enum/class-product-category-display.php @@ -0,0 +1,44 @@ + __( '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' ), + ), + ), + ) + ); + } +} diff --git a/includes/type/object/class-product-category-type.php b/includes/type/object/class-product-category-type.php index 605237f7a..39190c60e 100644 --- a/includes/type/object/class-product-category-type.php +++ b/includes/type/object/class-product-category-type.php @@ -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; + }, + ), ) ); } diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index b01ae8b09..df1eccba3 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -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', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 8fdfe49bf..8f1a18a65 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -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',