REST API: Allow sorting by stock_status in stocks endpoint (https://github.com/woocommerce/woocommerce-admin/pull/1335)

* REST API: Allow sorting by stock_status in stocks endpoint

* Set stock_status as default orderby

* Sort by both stock status and quantity in meta query

* Use stock_status sort order by default in Stock table (https://github.com/woocommerce/woocommerce-admin/pull/1340)

* Reverse stock quantity order when sorting by stock status
This commit is contained in:
Joshua T Flowers 2019-01-22 01:11:20 +08:00 committed by GitHub
parent c9df5861a8
commit 4869401ec4
2 changed files with 23 additions and 4 deletions

View File

@ -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' ) }

View File

@ -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',