From faf89b2c75a5327ac70c6279d7a4b13c09506df6 Mon Sep 17 00:00:00 2001 From: Brian Watson Date: Fri, 12 Jul 2019 16:09:14 -0400 Subject: [PATCH 1/4] Ensure the orders query is adjusted as late as possible. --- ...class-wc-order-data-store-custom-table.php | 2 +- tests/test-filters.php | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-order-data-store-custom-table.php b/includes/class-wc-order-data-store-custom-table.php index 6b60a17..610eea3 100644 --- a/includes/class-wc-order-data-store-custom-table.php +++ b/includes/class-wc-order-data-store-custom-table.php @@ -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 ); } /** diff --git a/tests/test-filters.php b/tests/test-filters.php index 2e12651..add208d 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -45,6 +45,70 @@ 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' => [ 'test@example.com' ], + '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' => [ 'test@example.com' ], + 'compare' => 'IN', + '_old_key' => '_billing_email', + ], + ], + 'meta_query' => [ + 'relation' => 'AND', + 'customer_emails' => [ + 'key' => '_billing_email', + 'value' => [ 'test@example.com' ], + 'compare' => 'IN', + ], + 'custom_key' => [ + 'key' => '_custom_meta_key', + 'value' => 'value', + ], + ], + ], 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(); + + 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->assertEquals( 1, count($orders) ); + } + public function test_filter_database_queries_without_meta_queries() { $this->assertEquals( [ 'foo' => 'bar', From 30594a56122674860009b3dc8d1e640b7fb2a3d8 Mon Sep 17 00:00:00 2001 From: Brian Watson Date: Sat, 13 Jul 2019 17:06:20 -0400 Subject: [PATCH 2/4] Remove duplicate `meta_query` in assertion. --- tests/test-filters.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/test-filters.php b/tests/test-filters.php index add208d..de30436 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -72,18 +72,6 @@ public function test_filter_database_queries_with_postmeta() { '_old_key' => '_billing_email', ], ], - 'meta_query' => [ - 'relation' => 'AND', - 'customer_emails' => [ - 'key' => '_billing_email', - 'value' => [ 'test@example.com' ], - 'compare' => 'IN', - ], - 'custom_key' => [ - 'key' => '_custom_meta_key', - 'value' => 'value', - ], - ], ], WooCommerce_Custom_Orders_Table_Filters::filter_database_queries( $args, [] ) ); } From c04ef883f16e5561232997016f86860f23c55e49 Mon Sep 17 00:00:00 2001 From: Brian Watson Date: Sat, 13 Jul 2019 17:06:52 -0400 Subject: [PATCH 3/4] Use `assertCount` instead of `assertEquals` to clean up the usage. --- tests/test-filters.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-filters.php b/tests/test-filters.php index de30436..253252f 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -94,7 +94,7 @@ public function test_wc_get_orders_custom_meta_key() { $orders = wc_get_orders( array( '_custom_meta_key' => 'value' ) ); - $this->assertEquals( 1, count($orders) ); + $this->assertCount( 1, $orders ); } public function test_filter_database_queries_without_meta_queries() { From 2c14360c38c538d72fae770b0e25c130ed1a2406 Mon Sep 17 00:00:00 2001 From: Brian Watson Date: Sat, 13 Jul 2019 17:07:38 -0400 Subject: [PATCH 4/4] Add inline documentation to help anyone later who may wonder why we're hooking into this particular filter. --- tests/test-filters.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test-filters.php b/tests/test-filters.php index 253252f..d31939f 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -81,6 +81,8 @@ public function test_wc_get_orders_custom_meta_key() { $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(