Skip to content

Commit

Permalink
feat: collectionStats query added (#785)
Browse files Browse the repository at this point in the history
* feat: collectionStats query added

* fix: `collectionStats` query completed and tested.

* chore: Linter and PHPStan compliance met

* chore: ProductTaxonomy values fixed.

* chore: CollectionStatsQueryTest tweaked for CI
  • Loading branch information
kidunot89 committed Sep 23, 2023
1 parent 356d705 commit 6b0c798
Show file tree
Hide file tree
Showing 14 changed files with 922 additions and 3 deletions.
6 changes: 6 additions & 0 deletions includes/class-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function init() {
Type\WPEnum\Orders_Orderby_Enum::register();
Type\WPEnum\Id_Type_Enums::register();
Type\WPEnum\Cart_Error_Type::register();
Type\WPEnum\Product_Attribute_Enum::register();
Type\WPEnum\Attribute_Operator_Enum::register();

/**
* InputObjects.
Expand All @@ -60,6 +62,9 @@ public function init() {
Type\WPInputObject\Product_Taxonomy_Filter_Input::register();
Type\WPInputObject\Product_Taxonomy_Input::register();
Type\WPInputObject\Orderby_Inputs::register();
Type\WPInputObject\Collection_Stats_Query_Input::register();
Type\WPInputObject\Collection_Stats_Where_Args::register();
Type\WPInputObject\Product_Attribute_Filter_Input::register();

/**
* Interfaces.
Expand Down Expand Up @@ -104,6 +109,7 @@ public function init() {
Type\WPObject\Cart_Error_Types::register();
Type\WPObject\Payment_Token_Types::register();
Type\WPObject\Country_State_Type::register();
Type\WPObject\Collection_Stats_Type::register();

/**
* Object fields.
Expand Down
6 changes: 6 additions & 0 deletions includes/class-wp-graphql-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ private function includes() {
require $include_directory_path . 'type/enum/class-tax-rate-connection-orderby-enum.php';
require $include_directory_path . 'type/enum/class-tax-status.php';
require $include_directory_path . 'type/enum/class-taxonomy-operator.php';
require $include_directory_path . 'type/enum/class-attribute-operator-enum.php';
require $include_directory_path . 'type/enum/class-product-attribute-enum.php';

// Include interface type class files.
require $include_directory_path . 'type/interface/class-attribute.php';
Expand Down Expand Up @@ -278,6 +280,7 @@ private function includes() {
require $include_directory_path . 'type/object/class-variation-attribute-type.php';
require $include_directory_path . 'type/object/class-payment-token-types.php';
require $include_directory_path . 'type/object/class-country-state-type.php';
require $include_directory_path . 'type/object/class-collection-stats-type.php';

// Include input type class files.
require $include_directory_path . 'type/input/class-cart-item-input.php';
Expand All @@ -293,6 +296,9 @@ private function includes() {
require $include_directory_path . 'type/input/class-product-taxonomy-input.php';
require $include_directory_path . 'type/input/class-shipping-line-input.php';
require $include_directory_path . 'type/input/class-tax-rate-connection-orderby-input.php';
require $include_directory_path . 'type/input/class-collection-stats-query-input.php';
require $include_directory_path . 'type/input/class-collection-stats-where-args.php';
require $include_directory_path . 'type/input/class-product-attribute-filter-input.php';

// Include mutation type class files.
require $include_directory_path . 'mutation/class-cart-add-fee.php';
Expand Down
4 changes: 4 additions & 0 deletions includes/connection/class-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ public static function get_connection_args( $extra_args = [] ): array {
'type' => 'Boolean',
'description' => __( 'Include variations in the result set.', 'wp-graphql-woocommerce' ),
],
'rating' => [
'type' => [ 'list_of' => 'Integer' ],
'description' => __( 'Limit result set to products with a specific average rating. Must be between 1 and 5', 'wp-graphql-woocommerce' ),
],
];

if ( wc_tax_enabled() ) {
Expand Down
15 changes: 15 additions & 0 deletions includes/data/connection/class-product-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,19 @@ public function sanitize_input_fields( array $where_args ) {
}//end switch
}//end if

if ( ! empty( $where_args['rating'] ) ) {
$rating = $where_args['rating'];
$rating_terms = [];
foreach ( $rating as $value ) {
$rating_terms[] = 'rated-' . $value;
}
$tax_query[] = [
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => $rating_terms,
];
}

// Process "taxonomyFilter".
$tax_filter_query = [];
if ( ! empty( $where_args['taxonomyFilter'] ) ) {
Expand Down Expand Up @@ -563,6 +576,8 @@ public function sanitize_input_fields( array $where_args ) {
$query_args[ $on_sale_key ] = $on_sale_ids;
}



/**
* {@inheritDoc}
*/
Expand Down
33 changes: 33 additions & 0 deletions includes/type/enum/class-attribute-operator-enum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* WPEnum Type - AttributeOperatorEnum
*
* @package WPGraphQL\WooCommerce\Type\WPEnum
* @since TBD
*/

namespace WPGraphQL\WooCommerce\Type\WPEnum;

/**
* Class Attribute_Operator_Enum
*/
class Attribute_Operator_Enum {
/**
* Registers type
*
* @return void
*/
public static function register() {
register_graphql_enum_type(
'AttributeOperatorEnum',
[
'description' => __( 'Collection statistic attributes operators', 'wp-graphql-woocommerce' ),
'values' => [
'IN' => [ 'value' => 'IN' ],
'NOT_IN' => [ 'value' => 'NOT IN' ],
'AND' => [ 'value' => 'AND' ],
],
]
);
}
}
43 changes: 43 additions & 0 deletions includes/type/enum/class-product-attribute-enum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* WPEnum Type - ProductAttributeEnum
*
* @package WPGraphQL\WooCommerce\Type\WPEnum
* @since TBD
*/

namespace WPGraphQL\WooCommerce\Type\WPEnum;

use WPGraphQL\Type\WPEnumType;

/**
* Class Product_Attribute_Enum
*/
class Product_Attribute_Enum {
/**
* Registers type
*
* @return void
*/
public static function register() {
// Get values from product attributes.
$taxonomy_values = [];
$taxonomies = wc_get_attribute_taxonomy_names();

foreach ( $taxonomies as $taxonomy ) {
$tax_object = get_taxonomy( $taxonomy );

if ( false !== $tax_object && in_array( 'product', $tax_object->object_type, true ) ) {
$taxonomy_values[ WPEnumType::get_safe_name( $taxonomy ) ] = [ 'value' => $taxonomy ];
}
}

register_graphql_enum_type(
'ProductAttributeEnum',
[
'description' => __( 'Product attribute taxonomies', 'wp-graphql-woocommerce' ),
'values' => $taxonomy_values,
]
);
}
}
2 changes: 1 addition & 1 deletion includes/type/enum/class-product-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function register() {
$tax_object = get_taxonomy( $taxonomy );

if ( false !== $tax_object && in_array( 'product', $tax_object->object_type, true ) ) {
$taxonomy_values[ WPEnumType::get_safe_name( $tax_object->graphql_single_name ) ] = [ 'value' => $taxonomy ];
$taxonomy_values[ WPEnumType::get_safe_name( $taxonomy ) ] = [ 'value' => $taxonomy ];
}
}

Expand Down
38 changes: 38 additions & 0 deletions includes/type/input/class-collection-stats-query-input.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* WPInputObjectType - CollectionStatsQueryInput
*
* @package WPGraphQL\WooCommerce\Type\WPInputObject
* @since TBD
*/

namespace WPGraphQL\WooCommerce\Type\WPInputObject;

/**
* Class Collection_Stats_Query_Input
*/
class Collection_Stats_Query_Input {
/**
* Registers type
*
* @return void
*/
public static function register() {
register_graphql_input_type(
'CollectionStatsQueryInput',
[
'description' => __( 'Taxonomy query', 'wp-graphql-woocommerce' ),
'fields' => [
'taxonomy' => [
'type' => [ 'non_null' => 'ProductAttributeEnum' ],
'description' => __( 'Product Taxonomy', 'wp-graphql-woocommerce' ),
],
'relation' => [
'type' => [ 'non_null' => 'RelationEnum' ],
'description' => __( 'Taxonomy relation to query', 'wp-graphql-woocommerce' ),
],
],
]
);
}
}
110 changes: 110 additions & 0 deletions includes/type/input/class-collection-stats-where-args.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* WPInputObjectType - CollectionStatsWhereArgs
*
* @package WPGraphQL\WooCommerce\Type\WPInputObject
* @since TBD
*/

namespace WPGraphQL\WooCommerce\Type\WPInputObject;

/**
* Class Collection_Stats_Where_Args
*/
class Collection_Stats_Where_Args {
/**
* Registers type
*
* @return void
*/
public static function register() {
register_graphql_input_type(
'CollectionStatsWhereArgs',
[
'description' => __( 'Arguments used to filter the collection results', 'wp-graphql-woocommerce' ),
'fields' => [
'search' => [
'type' => 'String',
'description' => __( 'Limit result set to products based on a keyword search.', 'wp-graphql-woocommerce' ),
],
'slugIn' => [
'type' => [ 'list_of' => 'String' ],
'description' => __( 'Limit result set to products with specific slugs.', 'wp-graphql-woocommerce' ),
],
'typeIn' => [
'type' => [ 'list_of' => 'ProductTypesEnum' ],
'description' => __( 'Limit result set to products assigned to a group of specific types.', 'wp-graphql-woocommerce' ),
],
'exclude' => [
'type' => [ 'list_of' => 'Int' ],
'description' => __( 'Ensure result set excludes specific IDs.', 'wp-graphql-woocommerce' ),
],
'include' => [
'type' => [ 'list_of' => 'Int' ],
'description' => __( 'Limit result set to specific ids.', 'wp-graphql-woocommerce' ),
],
'sku' => [
'type' => 'String',
'description' => __( 'Limit result set to products with specific SKU(s). Use commas to separate.', 'wp-graphql-woocommerce' ),
],
'featured' => [
'type' => 'Boolean',
'description' => __( 'Limit result set to featured products.', 'wp-graphql-woocommerce' ),
],
'parentIn' => [
'type' => [ 'list_of' => 'Int' ],
'description' => __( 'Specify objects whose parent is in an array.', 'wp-graphql-woocommerce' ),
],
'parentNotIn' => [
'type' => [ 'list_of' => 'Int' ],
'description' => __( 'Specify objects whose parent is not in an array.', 'wp-graphql-woocommerce' ),
],
'categoryIn' => [
'type' => [ 'list_of' => 'String' ],
'description' => __( 'Limit result set to products assigned to a group of specific categories by name.', 'wp-graphql-woocommerce' ),
],
'categoryIdIn' => [
'type' => [ 'list_of' => 'Int' ],
'description' => __( 'Limit result set to products assigned to a specific group of category IDs.', 'wp-graphql-woocommerce' ),
],
'tagIn' => [
'type' => [ 'list_of' => 'String' ],
'description' => __( 'Limit result set to products assigned to a specific group of tags by name.', 'wp-graphql-woocommerce' ),
],
'tagIdIn' => [
'type' => [ 'list_of' => 'Int' ],
'description' => __( 'Limit result set to products assigned to a specific group of tag IDs.', 'wp-graphql-woocommerce' ),
],
'attributes' => [
'type' => [ 'list_of' => 'ProductAttributeFilterInput' ],
'description' => __( 'Limit result set to products with a specific attribute. Use the taxonomy name/attribute slug.', 'wp-graphql-woocommerce' ),
],
'stockStatus' => [
'type' => [ 'list_of' => 'StockStatusEnum' ],
'description' => __( 'Limit result set to products in stock or out of stock.', 'wp-graphql-woocommerce' ),
],
'onSale' => [
'type' => 'Boolean',
'description' => __( 'Limit result set to products on sale.', 'wp-graphql-woocommerce' ),
],
'minPrice' => [
'type' => 'Float',
'description' => __( 'Limit result set to products based on a minimum price.', 'wp-graphql-woocommerce' ),
],
'maxPrice' => [
'type' => 'Float',
'description' => __( 'Limit result set to products based on a maximum price.', 'wp-graphql-woocommerce' ),
],
'visibility' => [
'type' => 'CatalogVisibilityEnum',
'description' => __( 'Limit result set to products with a specific visibility level.', 'wp-graphql-woocommerce' ),
],
'rating' => [
'type' => [ 'list_of' => 'Integer' ],
'description' => __( 'Limit result set to products with a specific average rating. Must be between 1 and 5', 'wp-graphql-woocommerce' ),
],
],
]
);
}
}
46 changes: 46 additions & 0 deletions includes/type/input/class-product-attribute-filter-input.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* WPInputObjectType - ProductAttributeFilterInput
*
* @package WPGraphQL\WooCommerce\Type\WPInputObject
* @since TBD
*/

namespace WPGraphQL\WooCommerce\Type\WPInputObject;

/**
* Class Product_Attribute_Filter_Input
*/
class Product_Attribute_Filter_Input {
/**
* Registers type
*
* @return void
*/
public static function register() {
register_graphql_input_type(
'ProductAttributeFilterInput',
[
'description' => __( 'Product filter', 'wp-graphql-woocommerce' ),
'fields' => [
'taxonomy' => [
'type' => [ 'non_null' => 'ProductAttributeEnum' ],
'description' => __( 'Which field to select taxonomy term by.', 'wp-graphql-woocommerce' ),
],
'terms' => [
'type' => [ 'list_of' => 'String' ],
'description' => __( 'A list of term slugs', 'wp-graphql-woocommerce' ),
],
'ids' => [
'type' => [ 'list_of' => 'Int' ],
'description' => __( 'A list of term ids', 'wp-graphql-woocommerce' ),
],
'operator' => [
'type' => 'AttributeOperatorEnum',
'description' => __( 'Filter operation type', 'wp-graphql-woocommerce' ),
],
],
]
);
}
}

0 comments on commit 6b0c798

Please sign in to comment.