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
This commit is contained in:
Albert Juhé Lluveras 2019-04-01 10:09:45 +02:00 committed by GitHub
parent 9c3faea12f
commit 458f3ed065
3 changed files with 21 additions and 51 deletions

View File

@ -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 ); 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. * Fills FROM clause of SQL request based on user supplied parameters.
* *

View File

@ -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. * Fills FROM clause of SQL request based on user supplied parameters.
* *
* @param array $query_args Parameters supplied by the user. * @param array $query_args Parameters supplied by the user.
* * @param string $arg_name Name of the FROM sql param.
* @return array * @return array
*/ */
protected function get_order_by_sql_params( $query_args ) { protected function get_from_sql_params( $query_args, $arg_name ) {
global $wpdb; global $wpdb;
$order_product_lookup_table = $wpdb->prefix . self::TABLE_NAME; $order_product_lookup_table = $wpdb->prefix . self::TABLE_NAME;
$sql_query['order_by_clause'] = ''; $sql_query['from_clause'] = '';
if ( isset( $query_args['orderby'] ) ) { $sql_query['outer_from_clause'] = '';
$sql_query['order_by_clause'] = $this->normalize_order_by( $query_args['orderby'] ); 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'";
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';
} }
return $sql_query; return $sql_query;
@ -111,11 +102,9 @@ 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. * 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 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. * @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; global $wpdb;
$order_product_lookup_table = $wpdb->prefix . self::TABLE_NAME; $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_limit_sql_params( $query_args ) );
$sql_query_params = array_merge( $sql_query_params, $this->get_order_by_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 ); $included_products = $this->get_included_products( $query_args );
if ( $included_products ) { if ( $included_products ) {
$sql_query_params['where_clause'] .= " AND {$order_product_lookup_table}.product_id IN ({$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 ); $order_status_filter = $this->get_status_subquery( $query_args );
$sql_query_params['outer_from_clause'] = '';
if ( $order_status_filter ) { 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 ( {$order_status_filter} )";
$sql_query_params['where_clause'] .= ' AND variation_id > 0'; $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 ); $included_products = $this->get_included_products_array( $query_args );
if ( count( $included_products ) > 0 && count( $query_args['variations'] ) > 0 ) { 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'] ) { if ( 'date' === $query_args['orderby'] ) {
$selections .= ", {$table_name}.date_created"; $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 $right_join = "RIGHT JOIN ( {$ids_table} ) AS default_results
ON default_results.variation_id = {$table_name}.variation_id"; ON default_results.variation_id = {$table_name}.variation_id";
} else { } 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( $db_records_count = (int) $wpdb->get_var(
"SELECT COUNT(*) FROM ( "SELECT COUNT(*) FROM (