From d00f60684944027f16df5bb075e1845f97e96b61 Mon Sep 17 00:00:00 2001 From: Vedanshu Jain Date: Wed, 28 Dec 2022 15:44:56 +0530 Subject: [PATCH] Add support for sorting by includes param. --- .../DataStores/Orders/OrdersTableQuery.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php index 8b71df2aed5..76c3c167bd9 100644 --- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php +++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php @@ -509,8 +509,17 @@ class OrdersTableQuery { return; } + // No need to sanitize, will be processed in calling function. + if ( 'include' === $orderby || 'post__in' === $orderby ) { + return; + } + if ( is_string( $orderby ) ) { - $orderby = array( $orderby => $order ); + $orderby_fields = array_map( 'trim', explode( ' ', $orderby ) ); + $orderby = array(); + foreach ( $orderby_fields as $field ) { + $orderby[ $field ] = $order; + } } $this->args['orderby'] = array(); @@ -977,6 +986,13 @@ class OrdersTableQuery { return; } + if ( 'include' === $orderby || 'post__in' === $orderby ) { + $ids = $this->args['id']; + $ids = array_map( 'absint', $ids ); + $this->orderby = array( "FIELD( {$this->tables['orders']}.id, " . implode( ',', $ids ) . ' )' ); + return; + } + $orderby_array = array(); foreach ( $this->args['orderby'] as $_orderby => $order ) { $orderby_array[] = "{$_orderby} {$order}";