Add date searching to wc_get_orders.
This commit is contained in:
parent
cb04d5985a
commit
b660eda4b8
|
@ -367,6 +367,14 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
|
||||||
$wp_query_args['no_found_rows'] = true;
|
$wp_query_args['no_found_rows'] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( ! empty( $args['date_before'] ) ) {
|
||||||
|
$wp_query_args['date_query']['before'] = $args['date_before'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! empty( $args['date_after'] ) ) {
|
||||||
|
$wp_query_args['date_query']['after'] = $args['date_after'];
|
||||||
|
}
|
||||||
|
|
||||||
// Get results.
|
// Get results.
|
||||||
$orders = new WP_Query( apply_filters( 'woocommerce_order_data_store_cpt_get_orders_query', $wp_query_args, $args, $this ) );
|
$orders = new WP_Query( apply_filters( 'woocommerce_order_data_store_cpt_get_orders_query', $wp_query_args, $args, $this ) );
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,8 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
* limit int Maximum of orders to retrieve.
|
* limit int Maximum of orders to retrieve.
|
||||||
* offset int Offset of orders to retrieve.
|
* offset int Offset of orders to retrieve.
|
||||||
* page int Page of orders to retrieve. Ignored when using the 'offset' arg.
|
* page int Page of orders to retrieve. Ignored when using the 'offset' arg.
|
||||||
|
* date_before string Get orders before a certain date ( strtotime() compatibile string )
|
||||||
|
* date_after string Get orders after a certain date ( strtotime() compatibile string )
|
||||||
* exclude array Order IDs to exclude from the query.
|
* exclude array Order IDs to exclude from the query.
|
||||||
* orderby string Order by date, title, id, modified, rand etc
|
* orderby string Order by date, title, id, modified, rand etc
|
||||||
* order string ASC or DESC
|
* order string ASC or DESC
|
||||||
|
@ -59,6 +61,8 @@ function wc_get_orders( $args ) {
|
||||||
'order' => 'DESC',
|
'order' => 'DESC',
|
||||||
'return' => 'objects',
|
'return' => 'objects',
|
||||||
'paginate' => false,
|
'paginate' => false,
|
||||||
|
'date_before' => '',
|
||||||
|
'date_after' => '',
|
||||||
) );
|
) );
|
||||||
|
|
||||||
// Handle some BW compatibility arg names where wp_query args differ in naming.
|
// Handle some BW compatibility arg names where wp_query args differ in naming.
|
||||||
|
|
|
@ -147,4 +147,45 @@ class WC_Tests_Order_Functions extends WC_Unit_Test_Case {
|
||||||
|
|
||||||
$this->assertCount( 1, $order->get_payment_tokens() );
|
$this->assertCount( 1, $order->get_payment_tokens() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the before and after date parameters for wc_get_orders.
|
||||||
|
*
|
||||||
|
* @since 2.7
|
||||||
|
*/
|
||||||
|
public function test_wc_get_orders_date_params() {
|
||||||
|
$order = WC_Helper_Order::create_order();
|
||||||
|
$order->set_date_created( '2015-01-01 05:20:30' );
|
||||||
|
$order->save();
|
||||||
|
$order_1 = $order->get_id();
|
||||||
|
$order = WC_Helper_Order::create_order();
|
||||||
|
$order->set_date_created( '2017-01-01' );
|
||||||
|
$order->save();
|
||||||
|
$order_2 = $order->get_id();
|
||||||
|
$order = WC_Helper_Order::create_order();
|
||||||
|
$order->set_date_created( '2017-01-01' );
|
||||||
|
$order->save();
|
||||||
|
$order_3 = $order->get_id();
|
||||||
|
|
||||||
|
$orders = wc_get_orders( array( 'date_before' => '2017-01-15', 'return' => 'ids' ) );
|
||||||
|
$expected = array( $order_1, $order_2, $order_3 );
|
||||||
|
$this->assertEquals( sort( $expected ), sort( $orders ) );
|
||||||
|
|
||||||
|
$orders = wc_get_orders( array( 'date_before' => '2017-01-01', 'return' => 'ids' ) );
|
||||||
|
$expected = array( $order_1 );
|
||||||
|
$this->assertEquals( sort( $expected ), sort( $orders ) );
|
||||||
|
|
||||||
|
$orders = wc_get_orders( array( 'date_before' => '2016-12-31', 'return' => 'ids' ) );
|
||||||
|
$expected = array( $order_1 );
|
||||||
|
$this->assertEquals( sort( $expected ), sort( $orders ) );
|
||||||
|
|
||||||
|
$orders = wc_get_orders( array( 'date_after' => '2015-01-01 00:00:00', 'return' => 'ids' ) );
|
||||||
|
$expected = array( $order_1, $order_2, $order_3 );
|
||||||
|
$this->assertEquals( sort( $expected ), sort( $orders ) );
|
||||||
|
|
||||||
|
$orders = wc_get_orders( array( 'date_after' => '2016-01-01', 'return' => 'ids' ) );
|
||||||
|
$expected = array( $order_2, $order_3 );
|
||||||
|
$this->assertEquals( sort( $expected ), sort( $orders ) );
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue