Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #126 from liquidweb/fix/custom-query-param-order
Browse files Browse the repository at this point in the history
Ensure the orders query is adjusted as late as possible.
  • Loading branch information
stevegrunwell authored Jul 15, 2019
2 parents 3b76465 + 2c14360 commit 25cc1ef
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion includes/class-wc-order-data-store-custom-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class WC_Order_Data_Store_Custom_Table extends WC_Order_Data_Store_CPT {
public function __construct() {

// When creating a WooCommerce order data store request, filter the MySQL query.
add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'WooCommerce_Custom_Orders_Table_Filters::filter_database_queries', 10, 2 );
add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'WooCommerce_Custom_Orders_Table_Filters::filter_database_queries', PHP_INT_MAX, 2 );
}

/**
Expand Down
54 changes: 54 additions & 0 deletions tests/test-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,60 @@ public function test_filter_database_queries() {
], WooCommerce_Custom_Orders_Table_Filters::filter_database_queries( $args, [] ) );
}

public function test_filter_database_queries_with_postmeta() {
$args = [
'meta_query' => [
'relation' => 'AND',
'customer_emails' => [
'key' => '_billing_email',
'value' => [ '[email protected]' ],
'compare' => 'IN',
],
'custom_key' => [
'key' => '_custom_meta_key',
'value' => 'value',
],
],
];

$this->assertEquals( [
'meta_query' => $args['meta_query'],
'_wc_has_meta_columns' => true,
'wc_order_meta_query' => [
[
'key' => 'billing_email',
'value' => [ '[email protected]' ],
'compare' => 'IN',
'_old_key' => '_billing_email',
],
],
], WooCommerce_Custom_Orders_Table_Filters::filter_database_queries( $args, [] ) );
}

public function test_wc_get_orders_custom_meta_key() {

$order = WC_Helper_Order::create_order();
$order->update_meta_data( '_custom_meta_key', 'value' );
$order->save();

// Because we hook into this filter, we need to ensure that if anyone else also hooks in, that we're
// properly handling the resulting postmeta table JOIN clause in the resulting SQL query.
add_filter('woocommerce_order_data_store_cpt_get_orders_query', function ( $query, $query_vars ) {
if ( ! empty( $query_vars['_custom_meta_key'] ) ) {
$query['meta_query'][] = array(
'key' => '_custom_meta_key',
'value' => esc_attr( $query_vars['_custom_meta_key'] ),
);
}

return $query;
}, 100, 2);

$orders = wc_get_orders( array( '_custom_meta_key' => 'value' ) );

$this->assertCount( 1, $orders );
}

public function test_filter_database_queries_without_meta_queries() {
$this->assertEquals( [
'foo' => 'bar',
Expand Down

0 comments on commit 25cc1ef

Please sign in to comment.