Basic working order queries

This commit is contained in:
claudiulodro 2017-04-25 14:52:17 -07:00
parent 3d5dabc886
commit efa1dbbda5
6 changed files with 119 additions and 7 deletions

View File

@ -75,10 +75,10 @@ abstract class WC_Object_Query {
'status' => array( 'publish', 'pending', 'draft', 'future', 'private', 'inherit' ),
'per_page' => 10,
'nopaging' => false,
'page' => 1,
'offset' => 0,
'per_page' => -1,
'nopaging' => '',
'page' => '',
'offset' => '',
'order' => 'DESC',
'orderby' => 'date',
@ -95,10 +95,10 @@ abstract class WC_Object_Query {
'meta_key' => '',
'meta_value' => '',
'meta_value_num' => '',
'meta_compare' => '=',
'meta_compare' => '',
's' => '',
'exact' => true,
'exact' => '',
'sentence' => '',
'fields' => '',

View File

@ -75,6 +75,6 @@ class WC_Order_Query extends WC_Object_Query {
* @return array of WC_Order objects
*/
public function get_orders() {
return WC_Data_Store::load( 'order' )->query_orders( $this->query_vars );
return WC_Data_Store::load( 'order' )->query( $this->query_vars );
}
}

View File

@ -194,4 +194,66 @@ class WC_Data_Store_WP {
return $props_to_update;
}
public function get_wp_query_args( $query_vars ) {
$skipped_values = array( '', array(), null );
$wp_query_args = array(
'meta_query' => array(),
);
foreach( $query_vars as $key => $value ) {
if ( in_array( $value, $skipped_values, true ) || 'meta_query' === $key ) {
continue;
}
if ( in_array( '_' . $key, $this->internal_meta_keys ) ) {
$wp_query_args['meta_query'][] = array(
'key' => '_' . $key,
'value' => $value,
'compare' => '=',
);
} else {
switch ( $key ) {
case 'parent':
$wp_query_args['post_parent'] = $value;
break;
case 'parent__in':
$wp_query_args['post_parent__in'] = $value;
break;
case 'parent__not_in':
$wp_query_args['post_parent__not_in'] = $value;
break;
case 'in':
$wp_query_args['post__in'] = $value;
break;
case 'not_in':
$wp_query_args['post__not_in'] = $value;
break;
case 'password':
$wp_query_args['post_password'] = $value;
break;
case 'status':
$wp_query_args['post_status'] = $value;
break;
case 'per_page':
$wp_query_args['posts_per_page'] = $value;
break;
case 'type':
$wp_query_args['post_type'] = $value;
default:
$wp_query_args[ $key ] = $value;
}
}
}
if ( ! empty( $query_vars['meta_query'] ) ) {
if ( empty( $wp_query_args['meta_query'] ) ) {
$wp_query_args['meta_query'] = $query_vars['meta_query'];
} else {
$wp_query_args['meta_query'][] = $query_vars['meta_query'];
}
}
return $wp_query_args;
}
}

View File

@ -634,4 +634,10 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
public function get_order_type( $order_id ) {
return get_post_type( $order_id );
}
public function query( $query_vars ) {
$args = $this->get_wp_query_args( $query_vars );
$query = new WP_Query( $args );
return array_map( 'wc_get_order', $query->posts );
}
}

View File

@ -67,6 +67,23 @@ class WC_Tests_Data_Store extends WC_Unit_Test_Case {
$this->assertEquals( 'WC_Dummy_Data_Store_CPT', $store->get_current_class_name() );
}
function test_get_wp_query_args() {
$store = new WC_Data_Store_WP();
$query = new WC_Mock_WC_Object_Query( array(
'parent__in' => array( 1 ),
'status' => 'publish',
'somethingelse' => 'value',
'ignored' => '',
) );
$args = $store->get_wp_query_args( $query->query_vars );
$this->assertEquals( array( 1 ), $args['post_parent__in'] );
$this->assertEquals( 'publish', $args['post_status'] );
$this->assertEquals( 'value', $args['somethingelse'] );
$this->assertFalse( isset( $args['ignored'] ) );
}
/* Helper Functions. */
/**

View File

@ -12,4 +12,31 @@ class WC_Tests_WC_Order_Query extends WC_Unit_Test_Case {
$this->assertEquals( wc_get_order_types( 'view-orders' ), $query->get( 'type' ) );
}
public function test_standard_order_query() {
$order1 = new WC_Order();
$order1->set_prices_include_tax( true );
$order1->set_total( '100.00' );
$order1->save();
$order2 = new WC_Order();
$order2->set_prices_include_tax( false );
$order2->set_total( '100.00' );
$order2->save();
$query = new WC_Order_Query();
$results = $query->get_orders();
$this->assertEquals( 2, count( $results ) );
$query->set( 'prices_include_tax', 'no' );
$results = $query->get_orders();
$this->assertEquals( 1, count( $results ) );
$this->assertEquals( $results[0]->get_id(), $order2->get_id() );
$query->set( 'prices_include_tax', '' );
$query->set( 'total', '100.00' );
$results = $query->get_orders();
$this->assertEquals( 2, count( $results ) );
}
}