diff --git a/plugins/woocommerce-admin/client/analytics/report/stock/table.js b/plugins/woocommerce-admin/client/analytics/report/stock/table.js index 4e8f523da82..ffdf8e15fb6 100644 --- a/plugins/woocommerce-admin/client/analytics/report/stock/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/stock/table.js @@ -43,12 +43,13 @@ export default class StockReportTable extends Component { { label: __( 'Status', 'wc-admin' ), key: 'stock_status', + isSortable: true, + defaultSort: true, }, { label: __( 'Stock', 'wc-admin' ), key: 'stock_quantity', isSortable: true, - defaultSort: true, }, ]; } @@ -136,8 +137,8 @@ export default class StockReportTable extends Component { // getSummary={ this.getSummary } query={ query } tableQuery={ { - orderby: query.orderby || 'stock_quantity', - order: query.order || 'desc', + orderby: query.orderby || 'stock_status', + order: query.order || 'asc', type: query.type || 'all', } } title={ __( 'Stock', 'wc-admin' ) } diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-controller.php index 903766f3d5a..785acc1a828 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-stock-controller.php @@ -51,6 +51,23 @@ class WC_Admin_REST_Reports_Stock_Controller extends WC_REST_Reports_Controller if ( 'date' === $args['orderby'] ) { $args['orderby'] = 'date ID'; + } elseif ( 'stock_status' === $args['orderby'] ) { + $args['meta_query'] = array( // WPCS: slow query ok. + 'relation' => 'AND', + '_stock_status' => array( + 'key' => '_stock_status', + 'compare' => 'EXISTS', + ), + '_stock' => array( + 'key' => '_stock', + 'compare' => 'EXISTS', + 'type' => 'NUMERIC', + ), + ); + $args['orderby'] = array( + '_stock_status' => $args['order'], + '_stock' => 'desc' === $args['order'] ? 'asc' : 'desc', + ); } elseif ( 'stock_quantity' === $args['orderby'] ) { $args['meta_key'] = '_stock'; // WPCS: slow query ok. $args['orderby'] = 'meta_value_num'; @@ -353,8 +370,9 @@ class WC_Admin_REST_Reports_Stock_Controller extends WC_REST_Reports_Controller $params['orderby'] = array( 'description' => __( 'Sort collection by object attribute.', 'wc-admin' ), 'type' => 'string', - 'default' => 'stock_quantity', + 'default' => 'stock_status', 'enum' => array( + 'stock_status', 'stock_quantity', 'date', 'id',