Skip to content

Commit

Permalink
Taxes report: sort tax rates numerically instead of alphabetically (w…
Browse files Browse the repository at this point in the history
…oocommerce#1766)

* Taxes report: sort tax rates numerically instead of alphabetically

* Use DECIMAL(7,4) for tax_rate sorting
  • Loading branch information
Aljullu authored Mar 12, 2019
1 parent f4b1d93 commit 3b2e63d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ protected function normalize_order_by( $order_by ) {
global $wpdb;

if ( 'rate' === $order_by ) {
return $wpdb->prefix . 'woocommerce_tax_rates.tax_rate';
return "CAST({$wpdb->prefix}woocommerce_tax_rates.tax_rate as DECIMAL(7,4))";
}

return $order_by;
Expand Down
57 changes: 57 additions & 0 deletions tests/api/reports-taxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,63 @@ public function test_get_reports_taxes_param() {
$this->assertEquals( 1, $tax_report['orders_count'] );
}

/**
* Test getting reports with param `orderby=rate`.
*
* @since 3.5.0
*/
public function test_get_reports_orderby_tax_rate() {
global $wpdb;
wp_set_current_user( $this->user );
WC_Helper_Reports::reset_stats_dbs();

$wpdb->insert(
$wpdb->prefix . 'woocommerce_tax_rates',
array(
'tax_rate_id' => 1,
'tax_rate' => '7',
'tax_rate_country' => 'US',
'tax_rate_state' => 'GA',
'tax_rate_name' => 'TestTax',
'tax_rate_priority' => 1,
'tax_rate_order' => 1,
)
);

$wpdb->insert(
$wpdb->prefix . 'woocommerce_tax_rates',
array(
'tax_rate_id' => 2,
'tax_rate' => '10',
'tax_rate_country' => 'CA',
'tax_rate_state' => 'ON',
'tax_rate_name' => 'TestTax 2',
'tax_rate_priority' => 1,
'tax_rate_order' => 1,
)
);

$request = new WP_REST_Request( 'GET', $this->endpoint );
$request->set_query_params(
array(
'order' => 'asc',
'orderby' => 'rate',
'taxes' => '1,2',
)
);
$response = $this->server->dispatch( $request );
$reports = $response->get_data();

$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 2, count( $reports ) );

$this->assertEquals( 1, $reports[0]['tax_rate_id'] );
$this->assertEquals( 7, $reports[0]['tax_rate'] );

$this->assertEquals( 2, $reports[1]['tax_rate_id'] );
$this->assertEquals( 10, $reports[1]['tax_rate'] );
}

/**
* Test getting reports without valid permissions.
*
Expand Down

0 comments on commit 3b2e63d

Please sign in to comment.