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

Ensure the orders query is adjusted as late as possible. #126

Merged
merged 5 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
stevegrunwell marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
64 changes: 64 additions & 0 deletions tests/test-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [ '[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',
],
],
'meta_query' => [
bswatson marked this conversation as resolved.
Show resolved Hide resolved
'relation' => 'AND',
'customer_emails' => [
'key' => '_billing_email',
'value' => [ '[email protected]' ],
'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 ) {
bswatson marked this conversation as resolved.
Show resolved Hide resolved
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) );
bswatson marked this conversation as resolved.
Show resolved Hide resolved
}

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