From ba582c307b68104ed631861ef32a3548a7f1e08a Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 24 Feb 2015 12:18:56 +0000 Subject: [PATCH] Dashboard - use same query as reports Closes #7529 --- includes/admin/class-wc-admin-dashboard.php | 33 ++++++++----------- .../reports/class-wc-report-low-in-stock.php | 6 ++-- .../reports/class-wc-report-out-of-stock.php | 6 ++-- 3 files changed, 17 insertions(+), 28 deletions(-) diff --git a/includes/admin/class-wc-admin-dashboard.php b/includes/admin/class-wc-admin-dashboard.php index fdabcd8e248..e6f93890175 100644 --- a/includes/admin/class-wc-admin-dashboard.php +++ b/includes/admin/class-wc-admin-dashboard.php @@ -96,35 +96,28 @@ class WC_Admin_Dashboard { $stock = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) ); $nostock = absint( max( get_option( 'woocommerce_notify_no_stock_amount' ), 0 ) ); - $query_from = "FROM {$wpdb->posts} as posts + $query_from = apply_filters( 'woocommerce_report_low_in_stock_query_from', "FROM {$wpdb->posts} as posts INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id WHERE 1=1 - AND posts.post_type IN ('product', 'product_variation') - AND posts.post_status = 'publish' - AND ( - postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$stock}' AND CAST(postmeta.meta_value AS SIGNED) > '{$nostock}' AND postmeta.meta_value != '' - ) - AND ( - ( postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' ) OR ( posts.post_type = 'product_variation' ) - ) - "; + AND posts.post_type IN ( 'product', 'product_variation' ) + AND posts.post_status = 'publish' + AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' + AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$stock}' + AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) > '{$nostock}' + " ); $lowinstock_count = absint( $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" ) ); - $query_from = "FROM {$wpdb->posts} as posts + $query_from = apply_filters( 'woocommerce_report_out_of_stock_query_from', "FROM {$wpdb->posts} as posts INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id WHERE 1=1 - AND posts.post_type IN ('product', 'product_variation') - AND posts.post_status = 'publish' - AND ( - postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$nostock}' AND postmeta.meta_value != '' - ) - AND ( - ( postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' ) OR ( posts.post_type = 'product_variation' ) - ) - "; + AND posts.post_type IN ( 'product', 'product_variation' ) + AND posts.post_status = 'publish' + AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' + AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$nostock}' + " ); $outofstock_count = absint( $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" ) ); ?> diff --git a/includes/admin/reports/class-wc-report-low-in-stock.php b/includes/admin/reports/class-wc-report-low-in-stock.php index c834cc217e0..465a653d28d 100644 --- a/includes/admin/reports/class-wc-report-low-in-stock.php +++ b/includes/admin/reports/class-wc-report-low-in-stock.php @@ -38,7 +38,7 @@ class WC_Report_Low_In_Stock extends WC_Report_Stock { $stock = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) ); $nostock = absint( max( get_option( 'woocommerce_notify_no_stock_amount' ), 0 ) ); - $query_from = "FROM {$wpdb->posts} as posts + $query_from = apply_filters( 'woocommerce_report_low_in_stock_query_from', "FROM {$wpdb->posts} as posts INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id WHERE 1=1 @@ -47,9 +47,7 @@ class WC_Report_Low_In_Stock extends WC_Report_Stock { AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$stock}' AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) > '{$nostock}' - "; - - $query_from = apply_filters( 'woocommerce_report_low_in_stock_query_from', $query_from ); + " ); $this->items = $wpdb->get_results( $wpdb->prepare( "SELECT posts.ID as id, posts.post_parent as parent {$query_from} GROUP BY posts.ID ORDER BY posts.post_title DESC LIMIT %d, %d;", ( $current_page - 1 ) * $per_page, $per_page ) ); $this->max_items = $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" ); diff --git a/includes/admin/reports/class-wc-report-out-of-stock.php b/includes/admin/reports/class-wc-report-out-of-stock.php index f8225fb7ce4..a4029091bf2 100644 --- a/includes/admin/reports/class-wc-report-out-of-stock.php +++ b/includes/admin/reports/class-wc-report-out-of-stock.php @@ -37,7 +37,7 @@ class WC_Report_Out_Of_Stock extends WC_Report_Stock { // Get products using a query - this is too advanced for get_posts :( $stock = absint( max( get_option( 'woocommerce_notify_no_stock_amount' ), 0 ) ); - $query_from = "FROM {$wpdb->posts} as posts + $query_from = apply_filters( 'woocommerce_report_out_of_stock_query_from', "FROM {$wpdb->posts} as posts INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id WHERE 1=1 @@ -45,9 +45,7 @@ class WC_Report_Out_Of_Stock extends WC_Report_Stock { AND posts.post_status = 'publish' AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$stock}' - "; - - $query_from = apply_filters( 'woocommerce_report_out_of_stock_query_from', $query_from ); + " ); $this->items = $wpdb->get_results( $wpdb->prepare( "SELECT posts.ID as id, posts.post_parent as parent {$query_from} GROUP BY posts.ID ORDER BY posts.post_title DESC LIMIT %d, %d;", ( $current_page - 1 ) * $per_page, $per_page ) ); $this->max_items = $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" );