Filter Variations report variation attributes correctly (#37223)

* FIx Variations report
* Fix Orders report
* Remove ability to pass table into get_attribute_subqueries since it should always be the same table we join on
This commit is contained in:
Matt Sherman 2023-03-17 14:05:51 -04:00 committed by GitHub
parent 345ad58919
commit c5564a15c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 10 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fixes filtering by attributes in the Analytics Orders and Variations reports.

View File

@ -1305,11 +1305,10 @@ class DataStore extends SqlQuery {
* 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.
* @param string $table_name Database table name.
* @param array $query_args Parameters supplied by the user.
* @return array
*/
protected function get_attribute_subqueries( $query_args, $table_name ) {
protected function get_attribute_subqueries( $query_args ) {
global $wpdb;
$sql_clauses = array(
@ -1363,11 +1362,11 @@ class DataStore extends SqlQuery {
$meta_value = esc_sql( $attribute_term[1] );
}
$join_alias = 'orderitemmeta1';
$join_alias = 'orderitemmeta1';
$table_to_join_on = "{$wpdb->prefix}wc_order_product_lookup";
if ( empty( $sql_clauses['join'] ) ) {
$table_name = esc_sql( $table_name );
$sql_clauses['join'][] = "JOIN {$wpdb->prefix}woocommerce_order_items orderitems ON orderitems.order_id = {$table_name}.order_id";
$sql_clauses['join'][] = "JOIN {$wpdb->prefix}woocommerce_order_items orderitems ON orderitems.order_id = {$table_to_join_on}.order_id";
}
// If we're matching all filters (AND), we'll need multiple JOINs on postmeta.
@ -1375,7 +1374,7 @@ class DataStore extends SqlQuery {
if ( 'AND' === $match_operator || 1 === count( $sql_clauses['join'] ) ) {
$join_idx = count( $sql_clauses['join'] );
$join_alias = 'orderitemmeta' . $join_idx;
$sql_clauses['join'][] = "JOIN {$wpdb->prefix}woocommerce_order_itemmeta as {$join_alias} ON {$join_alias}.order_item_id = orderitems.order_item_id";
$sql_clauses['join'][] = "JOIN {$wpdb->prefix}woocommerce_order_itemmeta as {$join_alias} ON {$join_alias}.order_item_id = {$table_to_join_on}.order_item_id";
}
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared

View File

@ -191,7 +191,7 @@ 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, $order_stats_lookup_table );
$attribute_subqueries = $this->get_attribute_subqueries( $query_args );
if ( $attribute_subqueries['join'] && $attribute_subqueries['where'] ) {
$this->subquery->add_sql_clause( 'join', "JOIN {$order_product_lookup_table} ON {$order_stats_lookup_table}.order_id = {$order_product_lookup_table}.order_id" );

View File

@ -208,7 +208,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
);
// Product attribute filters.
$attribute_subqueries = $this->get_attribute_subqueries( $query_args, $orders_stats_table );
$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.

View File

@ -119,7 +119,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
protected function get_order_item_by_attribute_subquery( $query_args ) {
$order_product_lookup_table = self::get_db_table_name();
$attribute_subqueries = $this->get_attribute_subqueries( $query_args, $order_product_lookup_table );
$attribute_subqueries = $this->get_attribute_subqueries( $query_args );
if ( $attribute_subqueries['join'] && $attribute_subqueries['where'] ) {
// Perform a subquery for DISTINCT order items that match our attribute filters.