diff --git a/plugins/woocommerce-admin/client/analytics/report/taxes/table.js b/plugins/woocommerce-admin/client/analytics/report/taxes/table.js index 700904c44df..76757947be6 100644 --- a/plugins/woocommerce-admin/client/analytics/report/taxes/table.js +++ b/plugins/woocommerce-admin/client/analytics/report/taxes/table.js @@ -32,8 +32,7 @@ export default class TaxesReportTable extends Component { return [ { label: __( 'Tax Code', 'woocommerce-admin' ), - // @todo It should be the tax code, not the ID - key: 'tax_rate_id', + key: 'tax_code', required: true, isLeftAligned: true, isSortable: true, @@ -72,19 +71,20 @@ export default class TaxesReportTable extends Component { getRowsContent( taxes ) { return map( taxes, tax => { - const { order_tax, orders_count, tax_rate, tax_rate_id, total_tax, shipping_tax } = tax; + const { order_tax, orders_count, tax_rate, total_tax, shipping_tax } = tax; + const taxCode = getTaxCode( tax ); // @todo Must link to the tax detail report const taxLink = ( - { getTaxCode( tax ) } + { taxCode } ); return [ { display: taxLink, - value: tax_rate_id, + value: taxCode, }, { display: tax_rate.toFixed( 2 ) + '%', diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-controller.php index 33bc06c2fb6..6729c9a3e89 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-taxes-controller.php @@ -264,6 +264,7 @@ class WC_Admin_REST_Reports_Taxes_Controller extends WC_REST_Reports_Controller 'enum' => array( 'name', 'tax_rate_id', + 'tax_code', 'rate', 'order_tax', 'total_tax', diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-data-store.php index 11d199d8a1f..a85941b1cba 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-taxes-data-store.php @@ -295,7 +295,9 @@ class WC_Admin_Reports_Taxes_Data_Store extends WC_Admin_Reports_Data_Store impl protected function normalize_order_by( $order_by ) { global $wpdb; - if ( 'rate' === $order_by ) { + if ( 'tax_code' === $order_by ) { + return 'CONCAT_WS( "-", NULLIF(tax_rate_country, ""), NULLIF(tax_rate_state, ""), NULLIF(tax_rate_name, ""), NULLIF(tax_rate_priority, "") )'; + } else if ( 'rate' === $order_by ) { return "CAST({$wpdb->prefix}woocommerce_tax_rates.tax_rate as DECIMAL(7,4))"; } diff --git a/plugins/woocommerce-admin/tests/api/reports-taxes.php b/plugins/woocommerce-admin/tests/api/reports-taxes.php index 4ab9e98c442..e0de47dbe73 100644 --- a/plugins/woocommerce-admin/tests/api/reports-taxes.php +++ b/plugins/woocommerce-admin/tests/api/reports-taxes.php @@ -269,6 +269,61 @@ class WC_Tests_API_Reports_Taxes extends WC_REST_Unit_Test_Case { $this->assertEquals( 10, $reports[1]['tax_rate'] ); } + /** + * Test getting reports with param `orderby=tax_code`. + * + * @since 3.5.0 + */ + public function test_get_reports_orderby_tax_code() { + 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' => 'tax_code', + '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( 2, $reports[0]['tax_rate_id'] ); + + $this->assertEquals( 1, $reports[1]['tax_rate_id'] ); + } + /** * Test getting reports without valid permissions. *