Commit 053fda7ec4 for woocommerce
commit 053fda7ec4edcf3ec0c9bc2e97c066ba05ef3fd0
Author: Ismael Martín Alabarce <info@ismaeld.com>
Date: Thu Apr 17 09:37:14 2025 +0200
Fix tax rate API support for non-Latin class names (#57013)
* Update `create_or_update_tax` to properly handle field keys
ensuring the switch cases are executed accordingly
* Add a test for tax classes with non-Latin characters
* Add changefile(s) from automation for the following project(s): woocommerce
* Add missing empty line to changelog
---------
Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Allie Mims <60988591+allie500@users.noreply.github.com>
Co-authored-by: Sam Najian <dev@najian.info>
diff --git a/plugins/woocommerce/changelog/57013-fix-45677-tax-rate-api-support-non-latin-class b/plugins/woocommerce/changelog/57013-fix-45677-tax-rate-api-support-non-latin-class
new file mode 100644
index 0000000000..5cc8a92924
--- /dev/null
+++ b/plugins/woocommerce/changelog/57013-fix-45677-tax-rate-api-support-non-latin-class
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix tax rate API support for non-Latin class names.
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-taxes-v1-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-taxes-v1-controller.php
index 2af61aa7a9..575f4a934b 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-taxes-v1-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-taxes-v1-controller.php
@@ -358,7 +358,7 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
}
// Add to data array.
- switch ( $key ) {
+ switch ( $field ) {
case 'tax_rate_priority':
case 'tax_rate_compound':
case 'tax_rate_shipping':
@@ -366,7 +366,7 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
$data[ $field ] = absint( $request[ $key ] );
break;
case 'tax_rate_class':
- $data[ $field ] = 'standard' !== $request['tax_rate_class'] ? $request['tax_rate_class'] : '';
+ $data[ $field ] = 'standard' !== $request[ $key ] ? $request[ $key ] : '';
break;
default:
$data[ $field ] = wc_clean( $request[ $key ] );
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-taxes-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-taxes-controller-tests.php
index 8b9137e8e8..2d517b2e0c 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-taxes-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-taxes-controller-tests.php
@@ -225,7 +225,7 @@ class WC_REST_Taxes_Controller_Tests extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$data = array_values( $response->get_data() );
$ids = array_map(
- function( $item ) {
+ function ( $item ) {
return $item['id'];
},
$data
@@ -277,7 +277,7 @@ class WC_REST_Taxes_Controller_Tests extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$data = array_values( $response->get_data() );
$ids = array_map(
- function( $item ) {
+ function ( $item ) {
return $item['id'];
},
$data
@@ -285,4 +285,31 @@ class WC_REST_Taxes_Controller_Tests extends WC_REST_Unit_Test_Case {
$this->assertEquals( array( $tax_ids_by_class[ $class ] ), $ids );
}
+
+ /**
+ * @testdox Tax rates with non-Latin characters in tax class names are properly created and associated with the correct class.
+ */
+ public function test_can_create_tax_rate_with_non_latin_tax_class() {
+ wp_set_current_user( $this->user );
+
+ $tax_class_name = '∑';
+ $tax_class_slug = WC_Tax::create_tax_class( $tax_class_name )['slug'];
+
+ $controller = new WC_REST_Taxes_V1_Controller();
+ $request = new WP_REST_Request( 'POST', '/wc/v1/taxes' );
+ $request->set_body_params(
+ array(
+ 'class' => $tax_class_slug,
+ )
+ );
+
+ $response = $controller->create_item( $request );
+ $tax_rate_id = $response->get_data()['id'];
+
+ $tax_rate = WC_Tax::_get_tax_rate( $tax_rate_id );
+ $this->assertEquals( $tax_class_slug, $tax_rate['tax_rate_class'] );
+
+ WC_Tax::_delete_tax_rate( $tax_rate_id );
+ WC_Tax::delete_tax_class_by( 'slug', $tax_class_slug );
+ }
}