From 458f3ed0651a6c1a78efa5629161c43af0cfbe44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Mon, 1 Apr 2019 10:09:45 +0200 Subject: [PATCH] Fix SKU sorting in the Variations report when a comparison is active (https://github.com/woocommerce/woocommerce-admin/pull/1952) * make is sortable true. * Variations endpoint: Fix sorting by SKU when there is an active comparison * Cleanup * Extract FROM parameter logic to 'get_from_sql_params' * Remove unnecessary 'get_order_by_sql_params' from Products and Variations data stores --- .../report/products/table-variations.js | 2 +- ...s-wc-admin-reports-products-data-store.php | 24 ---------- ...wc-admin-reports-variations-data-store.php | 46 ++++++++----------- 3 files changed, 21 insertions(+), 51 deletions(-) diff --git a/plugins/woocommerce-admin/client/analytics/report/products/table-variations.js b/plugins/woocommerce-admin/client/analytics/report/products/table-variations.js index d97a9b6d49d..755b9932e5c 100644 --- a/plugins/woocommerce-admin/client/analytics/report/products/table-variations.js +++ b/plugins/woocommerce-admin/client/analytics/report/products/table-variations.js @@ -40,7 +40,7 @@ export default class VariationsReportTable extends Component { label: __( 'SKU', 'woocommerce-admin' ), key: 'sku', hiddenByDefault: true, - isSortable: true, + isSortable: true, }, { label: __( 'Items Sold', 'woocommerce-admin' ), diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php index 75dc0aa218a..29c684e23f8 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-products-data-store.php @@ -92,30 +92,6 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i add_action( 'woocommerce_reports_delete_order_stats', array( __CLASS__, 'sync_on_order_delete' ), 10 ); } - /** - * Fills ORDER BY clause of SQL request based on user supplied parameters. - * - * @param array $query_args Parameters supplied by the user. - * @return array - */ - protected function get_order_by_sql_params( $query_args ) { - global $wpdb; - $order_product_lookup_table = $wpdb->prefix . self::TABLE_NAME; - - $sql_query['order_by_clause'] = ''; - if ( isset( $query_args['orderby'] ) ) { - $sql_query['order_by_clause'] = $this->normalize_order_by( $query_args['orderby'] ); - } - - if ( isset( $query_args['order'] ) ) { - $sql_query['order_by_clause'] .= ' ' . $query_args['order']; - } else { - $sql_query['order_by_clause'] .= ' DESC'; - } - - return $sql_query; - } - /** * Fills FROM clause of SQL request based on user supplied parameters. * diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-variations-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-variations-data-store.php index de7e16ac872..06fc27b71da 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-variations-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-variations-data-store.php @@ -79,29 +79,20 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store } /** - * Fills ORDER BY clause of SQL request based on user supplied parameters. - * - * @param array $query_args Parameters supplied by the user. + * Fills FROM clause of SQL request based on user supplied parameters. * + * @param array $query_args Parameters supplied by the user. + * @param string $arg_name Name of the FROM sql param. * @return array */ - protected function get_order_by_sql_params( $query_args ) { + protected function get_from_sql_params( $query_args, $arg_name ) { global $wpdb; $order_product_lookup_table = $wpdb->prefix . self::TABLE_NAME; - $sql_query['order_by_clause'] = ''; - if ( isset( $query_args['orderby'] ) ) { - $sql_query['order_by_clause'] = $this->normalize_order_by( $query_args['orderby'] ); - } - - if ( 'meta_value' === $sql_query['order_by_clause'] ) { - $sql_query['from_clause'] .= " JOIN {$wpdb->prefix}postmeta AS postmeta ON {$order_product_lookup_table}.variation_id = postmeta.post_id AND postmeta.meta_key = '_sku'"; - } - - if ( isset( $query_args['order'] ) ) { - $sql_query['order_by_clause'] .= ' ' . $query_args['order']; - } else { - $sql_query['order_by_clause'] .= ' DESC'; + $sql_query['from_clause'] = ''; + $sql_query['outer_from_clause'] = ''; + if ( 'sku' === $query_args['orderby'] ) { + $sql_query[ $arg_name ] .= " JOIN {$wpdb->prefix}postmeta AS postmeta ON {$order_product_lookup_table}.variation_id = postmeta.post_id AND postmeta.meta_key = '_sku'"; } return $sql_query; @@ -110,12 +101,10 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store /** * Updates the database query with parameters used for Products report: categories and order status. * - * @param array $query_args Query arguments supplied by the user. - * @param string $from_arg Name of the FROM sql param. - * - * @return array Array of parameters used for SQL query. + * @param array $query_args Query arguments supplied by the user. + * @return array Array of parameters used for SQL query. */ - protected function get_sql_query_params( $query_args, $from_arg ) { + protected function get_sql_query_params( $query_args ) { global $wpdb; $order_product_lookup_table = $wpdb->prefix . self::TABLE_NAME; @@ -123,6 +112,12 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store $sql_query_params = array_merge( $sql_query_params, $this->get_limit_sql_params( $query_args ) ); $sql_query_params = array_merge( $sql_query_params, $this->get_order_by_sql_params( $query_args ) ); + if ( count( $query_args['variations'] ) > 0 ) { + $sql_query_params = array_merge( $sql_query_params, $this->get_from_sql_params( $query_args, 'outer_from_clause' ) ); + } else { + $sql_query_params = array_merge( $sql_query_params, $this->get_from_sql_params( $query_args, 'from_clause' ) ); + } + $included_products = $this->get_included_products( $query_args ); if ( $included_products ) { $sql_query_params['where_clause'] .= " AND {$order_product_lookup_table}.product_id IN ({$included_products})"; @@ -134,9 +129,8 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store } $order_status_filter = $this->get_status_subquery( $query_args ); - $sql_query_params['outer_from_clause'] = ''; if ( $order_status_filter ) { - $sql_query_params[ $from_arg ] .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_product_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id"; + $sql_query_params['from_clause'] .= " JOIN {$wpdb->prefix}wc_order_stats ON {$order_product_lookup_table}.order_id = {$wpdb->prefix}wc_order_stats.order_id"; $sql_query_params['where_clause'] .= " AND ( {$order_status_filter} )"; $sql_query_params['where_clause'] .= ' AND variation_id > 0'; } @@ -257,7 +251,7 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store $included_products = $this->get_included_products_array( $query_args ); if ( count( $included_products ) > 0 && count( $query_args['variations'] ) > 0 ) { - $sql_query_params = $this->get_sql_query_params( $query_args, 'from_clause' ); + $sql_query_params = $this->get_sql_query_params( $query_args ); if ( 'date' === $query_args['orderby'] ) { $selections .= ", {$table_name}.date_created"; @@ -275,7 +269,7 @@ class WC_Admin_Reports_Variations_Data_Store extends WC_Admin_Reports_Data_Store $right_join = "RIGHT JOIN ( {$ids_table} ) AS default_results ON default_results.variation_id = {$table_name}.variation_id"; } else { - $sql_query_params = $this->get_sql_query_params( $query_args, 'from_clause' ); + $sql_query_params = $this->get_sql_query_params( $query_args ); $db_records_count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM (