Add product attribute filter to Orders Report. (https://github.com/woocommerce/woocommerce-admin/pull/5068)

* Add product attribute filter to Orders report config.

* Add attribute args to Orders report controller.

* Include attribute filters in orders report query.

* Add attribute args to Orders Stats report controller.

* Include attribute filters in orders report stats query.

* Add test for product attribute filter in orders report.

* Add tests for invalid parameter values.

* Add tests for product attribute filter in order stats endpoint.

* Fix tests for PHP 5.6 and WC 3.8.x.
This commit is contained in:
Jeff Stieler 2020-09-03 10:08:06 -04:00 committed by GitHub
parent 8814f6b46a
commit ebfd28a9e6
8 changed files with 377 additions and 0 deletions

View File

@ -310,6 +310,50 @@ export const advancedFilters = applyFilters(
getLabels: getTaxRateLabels,
},
},
attribute: {
allowMultiple: true,
labels: {
add: __( 'Attribute', 'woocommerce-admin' ),
placeholder: __( 'Search attributes', 'woocommerce-admin' ),
remove: __(
'Remove attribute filter',
'woocommerce-admin'
),
rule: __(
'Select a product attribute filter match',
'woocommerce-admin'
),
/* translators: A sentence describing a Product filter. See screen shot for context: https://cloudup.com/cSsUY9VeCVJ */
title: __(
'{{title}}Attribute{{/title}} {{rule /}} {{filter /}}',
'woocommerce-admin'
),
filter: __( 'Select attributes', 'woocommerce-admin' ),
},
rules: [
{
value: 'is',
/* translators: Sentence fragment, logical, "Is" refers to searching for products matching a chosen attribute. Screenshot for context: https://cloudup.com/cSsUY9VeCVJ */
label: _x(
'Is',
'product attribute',
'woocommerce-admin'
),
},
{
value: 'is_not',
/* translators: Sentence fragment, logical, "Is Not" refers to searching for products that don\'t match a chosen attribute. Screenshot for context: https://cloudup.com/cSsUY9VeCVJ */
label: _x(
'Is Not',
'product attribute',
'woocommerce-admin'
),
},
],
input: {
component: 'ProductAttribute',
},
},
},
}
);

View File

@ -1179,6 +1179,78 @@ class DataStore extends SqlQuery {
return $customer_filter;
}
/**
* Returns product attribute subquery elements used in JOIN and WHERE clauses,
* based on query arguments from the user.
*
* @param array $query_args Parameters supplied by the user.
* @return array
*/
protected function get_attribute_subqueries( $query_args ) {
global $wpdb;
$sql_clauses = array(
'join' => array(),
'where' => array(),
);
$match_operator = $this->get_match_operator( $query_args );
$join_table = $wpdb->prefix . 'wc_order_product_lookup';
$post_meta_comparators = array(
'=' => 'attribute_is',
'!=' => 'attribute_is_not',
);
foreach ( $post_meta_comparators as $comparator => $arg ) {
if ( ! isset( $query_args[ $arg ] ) || ! is_array( $query_args[ $arg ] ) ) {
continue;
}
foreach ( $query_args[ $arg ] as $attribute_term ) {
// We expect tuples of IDs.
if ( ! is_array( $attribute_term ) || 2 !== count( $attribute_term ) ) {
continue;
}
$attribute_id = intval( $attribute_term[0] );
$term_id = intval( $attribute_term[1] );
// Tuple, but non-numeric.
if ( 0 === $attribute_id || 0 === $term_id ) {
continue;
}
// @todo: Use wc_get_attribute() instead ?
$attr_taxonomy = wc_attribute_taxonomy_name_by_id( $attribute_id );
// Invalid attribute ID.
if ( empty( $attr_taxonomy ) ) {
continue;
}
$attr_term = get_term_by( 'id', $term_id, $attr_taxonomy );
// Invalid term ID.
if ( false === $attr_term ) {
continue;
}
$meta_key = wc_variation_attribute_name( $attr_taxonomy );
$meta_value = $attr_term->slug;
$join_alias = 'wpm1';
// If we're matching all filters (AND), we'll need multiple JOINs on postmeta.
// If not, just one.
if ( 'AND' === $match_operator || empty( $sql_clauses['join'] ) ) {
$join_idx = count( $sql_clauses['join'] ) + 1;
$join_alias = 'wpm' . $join_idx;
$sql_clauses['join'][] = "JOIN {$wpdb->postmeta} as {$join_alias} ON {$join_alias}.post_id = {$join_table}.variation_id";
}
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$sql_clauses['where'][] = $wpdb->prepare( "( {$join_alias}.meta_key = %s AND {$join_alias}.meta_value {$comparator} %s )", $meta_key, $meta_value );
}
}
return $sql_clauses;
}
/**
* Returns logic operator for WHERE subclause based on 'match' query argument.
*

View File

@ -61,6 +61,8 @@ class Controller extends ReportsController implements ExportableInterface {
$args['match'] = $request['match'];
$args['order_includes'] = $request['order_includes'];
$args['order_excludes'] = $request['order_excludes'];
$args['attribute_is'] = (array) $request['attribute_is'];
$args['attribute_is_not'] = (array) $request['attribute_is_not'];
return $args;
}
@ -429,6 +431,24 @@ class Controller extends ReportsController implements ExportableInterface {
'type' => 'integer',
),
);
$params['attribute_is'] = array(
'description' => __( 'Limit result set to orders that include products with the specified attributes.', 'woocommerce-admin' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
$params['attribute_is_not'] = array(
'description' => __( 'Limit result set to orders that don\'t include products with the specified attributes.', 'woocommerce-admin' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}

View File

@ -160,6 +160,20 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$where_subquery[] = "{$order_tax_lookup_table}.tax_rate_id NOT IN ({$excluded_tax_rates}) OR {$order_tax_lookup_table}.tax_rate_id IS NULL";
}
$attribute_subqueries = $this->get_attribute_subqueries( $query_args );
if ( $attribute_subqueries['join'] && $attribute_subqueries['where'] ) {
// JOIN on product lookup if we haven't already.
if ( ! $included_products && ! $excluded_products ) {
$this->subquery->add_sql_clause( 'join', "JOIN {$order_product_lookup_table} ON {$order_stats_lookup_table}.order_id = {$order_product_lookup_table}.order_id" );
}
// Add JOINs for matching attributes.
foreach ( $attribute_subqueries['join'] as $attribute_join ) {
$this->subquery->add_sql_clause( 'join', $attribute_join );
}
// Add WHEREs for matching attributes.
$where_subquery = array_merge( $where_subquery, $attribute_subqueries['where'] );
}
if ( 0 < count( $where_subquery ) ) {
$this->subquery->add_sql_clause( 'where', 'AND (' . implode( " {$operator} ", $where_subquery ) . ')' );
}

View File

@ -59,6 +59,8 @@ class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
$args['tax_rate_excludes'] = (array) $request['tax_rate_excludes'];
$args['customer'] = $request['customer'];
$args['refunds'] = $request['refunds'];
$args['attribute_is'] = (array) $request['attribute_is'];
$args['attribute_is_not'] = (array) $request['attribute_is_not'];
$args['categories'] = (array) $request['categories'];
$args['segmentby'] = $request['segmentby'];
@ -492,6 +494,24 @@ class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['attribute_is'] = array(
'description' => __( 'Limit result set to orders that include products with the specified attributes.', 'woocommerce-admin' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
$params['attribute_is_not'] = array(
'description' => __( 'Limit result set to orders that don\'t include products with the specified attributes.', 'woocommerce-admin' ),
'type' => 'array',
'items' => array(
'type' => 'array',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
$params['segmentby'] = array(
'description' => __( 'Segment the response by additional constraint.', 'woocommerce-admin' ),
'type' => 'string',

View File

@ -178,6 +178,29 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
implode( ',', $query_args['tax_rate_excludes'] )
);
// Product attribute filters.
$attribute_subqueries = $this->get_attribute_subqueries( $query_args );
if ( $attribute_subqueries['join'] && $attribute_subqueries['where'] ) {
// Build a subquery for getting order IDs by product attribute(s).
// Done here since our use case is a little more complicated than get_object_where_filter() can handle.
$attribute_subquery = new SqlQuery();
$attribute_subquery->add_sql_clause( 'select', "{$orders_stats_table}.order_id" );
$attribute_subquery->add_sql_clause( 'from', $orders_stats_table );
// JOIN on product lookup.
$attribute_subquery->add_sql_clause( 'join', "JOIN {$product_lookup} ON {$orders_stats_table}.order_id = {$product_lookup}.order_id" );
// Add JOINs for matching attributes.
foreach ( $attribute_subqueries['join'] as $attribute_join ) {
$attribute_subquery->add_sql_clause( 'join', $attribute_join );
}
// Add WHEREs for matching attributes.
$attribute_subquery->add_sql_clause( 'where', 'AND (' . implode( " {$operator} ", $attribute_subqueries['where'] ) . ')' );
// Generate subquery statement and add to our where filters.
$where_filters[] = "{$orders_stats_table}.order_id IN (" . $attribute_subquery->get_query_statement() . ')';
}
$where_filters[] = $this->get_customer_subquery( $query_args );
$refund_subquery = $this->get_refund_subquery( $query_args );
$from_clause .= $refund_subquery['from_clause'];

View File

@ -127,4 +127,83 @@ class WC_Tests_API_Reports_Orders_Stats extends WC_REST_Unit_Test_Case {
$this->assertArrayHasKey( 'total_customers', $subtotals );
$this->assertArrayHasKey( 'segments', $subtotals );
}
/**
* Test filtering by product attribute(s).
*/
public function test_product_attributes_filter() {
global $wpdb;
wp_set_current_user( $this->user );
WC_Helper_Reports::reset_stats_dbs();
// Create a variable product.
$variable_product = WC_Helper_Product::create_variation_product( new WC_Product_Variable() );
$product_variations = $variable_product->get_children();
$order_variation_1 = wc_get_product( $product_variations[1] ); // Variation: size = large.
$order_variation_2 = wc_get_product( $product_variations[3] ); // Variation: size = huge, colour = red, number = 2.
// Create orders for variations.
$variation_order_1 = WC_Helper_Order::create_order( $this->user, $order_variation_1 );
$variation_order_1->set_status( 'completed' );
$variation_order_1->save();
$variation_order_2 = WC_Helper_Order::create_order( $this->user, $order_variation_2 );
$variation_order_2->set_status( 'completed' );
$variation_order_2->save();
// Create more orders for simple products.
for ( $i = 0; $i < 10; $i++ ) {
$order = WC_Helper_Order::create_order( $this->user );
$order->set_status( 'completed' );
$order->save();
}
WC_Helper_Queue::run_all_pending();
$request = new WP_REST_Request( 'GET', $this->endpoint );
$request->set_query_params( array( 'per_page' => 15 ) );
$response = $this->server->dispatch( $request );
$response_orders = $response->get_data();
// Sanity check before filtering by attribute.
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 12, $response_orders['totals']['orders_count'] );
// Filter by the "size" attribute, with value "large".
$size_attr_id = wc_attribute_taxonomy_id_by_name( 'pa_size' );
$small_term = get_term_by( 'slug', 'large', 'pa_size' );
$request = new WP_REST_Request( 'GET', $this->endpoint );
$request->set_query_params(
array(
'attribute_is' => array(
array( $size_attr_id, $small_term->term_id ),
),
)
);
$response = $this->server->dispatch( $request );
$response_orders = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, $response_orders['totals']['orders_count'] );
$this->assertEquals( $variation_order_1->get_total(), $response_orders['totals']['total_sales'] );
// Filter by excluding the "size" attribute, with value "large".
$size_attr_id = wc_attribute_taxonomy_id_by_name( 'pa_size' );
$small_term = get_term_by( 'slug', 'large', 'pa_size' );
$request = new WP_REST_Request( 'GET', $this->endpoint );
$request->set_query_params(
array(
'attribute_is_not' => array(
array( $size_attr_id, $small_term->term_id ),
),
)
);
$response = $this->server->dispatch( $request );
$response_orders = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, $response_orders['totals']['orders_count'] );
// This should be the second variation order.
$this->assertEquals( $variation_order_2->get_total(), $response_orders['totals']['total_sales'] );
}
}

View File

@ -128,4 +128,109 @@ class WC_Tests_API_Reports_Orders extends WC_REST_Unit_Test_Case {
$this->assertArrayHasKey( 'customer_type', $properties );
$this->assertArrayHasKey( 'extended_info', $properties );
}
/**
* Test filtering by product attribute(s).
*/
public function test_product_attributes_filter() {
global $wpdb;
wp_set_current_user( $this->user );
WC_Helper_Reports::reset_stats_dbs();
// Create a variable product.
$variable_product = WC_Helper_Product::create_variation_product( new WC_Product_Variable() );
$product_variations = $variable_product->get_children();
$order_variation_1 = wc_get_product( $product_variations[0] ); // Variation: size = small.
$order_variation_2 = wc_get_product( $product_variations[2] ); // Variation: size = huge, colour = red, number = 0.
// Create orders for variations.
$variation_order_1 = WC_Helper_Order::create_order( $this->user, $order_variation_1 );
$variation_order_1->set_status( 'completed' );
$variation_order_1->save();
$variation_order_2 = WC_Helper_Order::create_order( $this->user, $order_variation_2 );
$variation_order_2->set_status( 'completed' );
$variation_order_2->save();
// Create more orders for simple products.
for ( $i = 0; $i < 10; $i++ ) {
$order = WC_Helper_Order::create_order( $this->user );
$order->set_status( 'completed' );
$order->save();
}
WC_Helper_Queue::run_all_pending();
$request = new WP_REST_Request( 'GET', $this->endpoint );
$request->set_query_params( array( 'per_page' => 15 ) );
$response = $this->server->dispatch( $request );
$response_orders = $response->get_data();
// Sanity check before filtering by attribute.
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 12, count( $response_orders ) );
// To filter by later.
$size_attr_id = wc_attribute_taxonomy_id_by_name( 'pa_size' );
$small_term = get_term_by( 'slug', 'small', 'pa_size' );
// Test bad values to filter parameter.
$bad_args = array(
'not an array!', // Not an array.
array( 1, 2, 3 ), // Not a tuple.
array( 'a', 1 ), // Non-numeric attribute ID.
array( 1, 'a' ), // Non-numeric term ID.
array( -1, $small_term->term_id ), // Invaid attribute ID.
array( $size_attr_id, -1 ), // Invaid term ID.
);
foreach ( $bad_args as $bad_arg ) {
$request = new WP_REST_Request( 'GET', $this->endpoint );
$request->set_query_params(
array(
'per_page' => 15,
'attribute_is' => $bad_arg,
)
);
$response = $this->server->dispatch( $request );
$response_orders = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
// We expect all results since the attribute param is malformed.
$this->assertEquals( 12, count( $response_orders ) );
}
// Filter by the "size" attribute, with value "small".
$request = new WP_REST_Request( 'GET', $this->endpoint );
$request->set_query_params(
array(
'attribute_is' => array(
array( $size_attr_id, $small_term->term_id ),
),
)
);
$response = $this->server->dispatch( $request );
$response_orders = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, count( $response_orders ) );
$this->assertEquals( $response_orders[0]['order_id'], $variation_order_1->get_id() );
// Verify the opposite result set.
$request = new WP_REST_Request( 'GET', $this->endpoint );
$request->set_query_params(
array(
'attribute_is_not' => array(
array( $size_attr_id, $small_term->term_id ),
),
'per_page' => 15,
)
);
$response = $this->server->dispatch( $request );
$response_orders = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, count( $response_orders ) );
$this->assertEquals( $response_orders[0]['order_id'], $variation_order_2->get_id() );
}
}