Commit ed2ae4522b1 for woocommerce
commit ed2ae4522b14e1292b1c626593dc860c1df1d64d
Author: Aarón Fas <yo@aaron.com.es>
Date: Wed Dec 2 18:15:02 2026 +0100
Reject unknown tax classes in tax rate queries (#71453)
* Reject unknown tax classes in tax rate queries
An empty result for a misspelled class can lead callers to report that a store has no matching rates. Return the existing tax class error so callers can retry with one of the configured classes.
* Add changelog entry for tax rate query errors
Tell merchants that queries now list the available tax classes when a class name is unknown.
* Reject empty tax class filters and verify the full error message
An empty filter produced an error with a blank class name. Reject it during schema validation and check the full message for unknown classes because MCP clients rely on that text to retry.
* Explain blank tax class values in ability errors
Printing an empty or whitespace-only class leaves callers with a missing name in the error. Explain that the class is blank and show the configured alternatives while preserving the existing error code.
diff --git a/plugins/woocommerce/changelog/fix-tax-query-unknown-class b/plugins/woocommerce/changelog/fix-tax-query-unknown-class
new file mode 100644
index 00000000000..d77657fe42c
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-tax-query-unknown-class
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Show the available tax classes when a tax rate query uses an unknown class.
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/TaxRatesQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/TaxRatesQuery.php
index fb246d5f489..d393b61db1d 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/TaxRatesQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/TaxRatesQuery.php
@@ -68,6 +68,7 @@ class TaxRatesQuery extends AbstractDomainAbility implements AbilityDefinition {
),
'class' => array(
'type' => 'string',
+ 'minLength' => 1,
'description' => __( 'Tax class slug; standard for the standard rates.', 'woocommerce' ),
),
'page' => array(
@@ -107,11 +108,11 @@ class TaxRatesQuery extends AbstractDomainAbility implements AbilityDefinition {
* List tax rates.
*
* @param array $input Ability input.
- * @return array
+ * @return array|\WP_Error
*
* @since 11.4.0
*/
- public static function execute( array $input ): array {
+ public static function execute( array $input ) {
global $wpdb;
$page = $input['page'] ?? 1;
@@ -127,13 +128,7 @@ class TaxRatesQuery extends AbstractDomainAbility implements AbilityDefinition {
if ( isset( $input['class'] ) ) {
$tax_class = self::get_stored_tax_class( $input['class'] );
if ( null === $tax_class ) {
- return array(
- 'rates' => array(),
- 'total' => 0,
- 'total_pages' => 0,
- 'page' => $page,
- 'per_page' => $per_page,
- );
+ return self::get_tax_class_not_found_error( $input['class'] );
}
$conditions[] = $wpdb->prepare( 'tax_rate_class = %s', $tax_class );
}
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/TaxRateAbilityTrait.php b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/TaxRateAbilityTrait.php
index f6d817e2a68..1d246f5dce5 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/TaxRateAbilityTrait.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/TaxRateAbilityTrait.php
@@ -36,10 +36,18 @@ trait TaxRateAbilityTrait {
* @return \WP_Error
*/
private static function get_tax_class_not_found_error( string $slug ): \WP_Error {
+ $classes = implode( ', ', array_merge( array( 'standard' ), \WC_Tax::get_tax_class_slugs() ) );
+ if ( preg_match( '/^\s*$/u', $slug ) ) {
+ /* translators: %s: comma-separated tax class slugs. */
+ $message = sprintf( __( 'Tax class cannot be blank. Use one of: %s.', 'woocommerce' ), $classes );
+ } else {
+ /* translators: 1: tax class slug, 2: comma-separated tax class slugs. */
+ $message = sprintf( __( 'There is no tax class %1$s. Use one of: %2$s.', 'woocommerce' ), $slug, $classes );
+ }
+
return new \WP_Error(
'woocommerce_ability_tax_rate_class_not_found',
- /* translators: 1: tax class slug, 2: comma-separated tax class slugs. */
- sprintf( __( 'There is no tax class %1$s. Use one of: %2$s.', 'woocommerce' ), $slug, implode( ', ', array_merge( array( 'standard' ), \WC_Tax::get_tax_class_slugs() ) ) ),
+ $message,
array( 'status' => 400 )
);
}
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ShippingAndTaxAbilitiesTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ShippingAndTaxAbilitiesTest.php
index 5ec8f36082c..30d7ab7db13 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ShippingAndTaxAbilitiesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ShippingAndTaxAbilitiesTest.php
@@ -305,7 +305,7 @@ class ShippingAndTaxAbilitiesTest extends \WC_Unit_Test_Case {
}
/**
- * @testdox Should page tax rates in the order of the tax settings, and return none for an unknown class.
+ * @testdox Should page tax rates in the order of the tax settings.
*/
public function test_tax_rates_are_paged(): void {
$ids = array();
@@ -319,22 +319,49 @@ class ShippingAndTaxAbilitiesTest extends \WC_Unit_Test_Case {
);
}
- $first = $this->run_ability( 'woocommerce/tax-rates-query', array( 'per_page' => 2 ) );
- $second = $this->run_ability(
+ $first = $this->run_ability( 'woocommerce/tax-rates-query', array( 'per_page' => 2 ) );
+ $second = $this->run_ability(
'woocommerce/tax-rates-query',
array(
'per_page' => 2,
'page' => 2,
)
);
- $unknown = $this->run_ability( 'woocommerce/tax-rates-query', array( 'class' => 'no-such-class' ) );
$this->assertSame( array( $ids[0], $ids[1] ), array_column( $first['rates'], 'id' ) );
$this->assertSame( array( $ids[2] ), array_column( $second['rates'], 'id' ) );
$this->assertSame( 3, $first['total'] );
$this->assertSame( 2, $first['total_pages'] );
- $this->assertSame( array(), $unknown['rates'] );
- $this->assertSame( 0, $unknown['total'] );
+ }
+
+ /**
+ * @testdox Should distinguish a class with no rates from an unknown class and list valid alternatives.
+ */
+ public function test_tax_rates_validate_the_class_filter(): void {
+ $empty = $this->run_ability( 'woocommerce/tax-rates-query', array( 'class' => 'Reduced rate' ) );
+ $this->assertSame( array(), $empty['rates'] );
+ $this->assertSame( 0, $empty['total'] );
+
+ $error = $this->run_ability( 'woocommerce/tax-rates-query', array( 'class' => 'reduced-rat' ) );
+ $this->assertWPError( $error );
+ $this->assertSame( 'woocommerce_ability_tax_rate_class_not_found', $error->get_error_code() );
+ $this->assertSame( 400, $error->get_error_data()['status'] );
+ $this->assertSame( 'There is no tax class reduced-rat. Use one of: standard, reduced-rate, zero-rate.', $error->get_error_message() );
+
+ $blank = $this->run_ability( 'woocommerce/tax-rates-query', array( 'class' => ' ' ) );
+ $this->assertWPError( $blank );
+ $this->assertSame( 'woocommerce_ability_tax_rate_class_not_found', $blank->get_error_code() );
+ $this->assertSame( 'Tax class cannot be blank. Use one of: standard, reduced-rate, zero-rate.', $blank->get_error_message() );
+ }
+
+ /**
+ * @testdox Should reject an explicitly empty tax class during input validation.
+ */
+ public function test_tax_rates_reject_an_empty_class(): void {
+ $error = $this->run_ability( 'woocommerce/tax-rates-query', array( 'class' => '' ) );
+
+ $this->assertWPError( $error );
+ $this->assertSame( 'ability_invalid_input', $error->get_error_code() );
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/TaxRateAbilitiesTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/TaxRateAbilitiesTest.php
index 6c10b752b6a..d2e4ee5d3c2 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/TaxRateAbilitiesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/TaxRateAbilitiesTest.php
@@ -444,18 +444,26 @@ class TaxRateAbilitiesTest extends \WC_Unit_Test_Case {
}
/**
- * @testdox Should refuse a tax class that does not exist, on create and on update.
+ * @testdox Should explain why an invalid class was refused on create and update.
+ *
+ * @testWith ["no-such-class", "There is no tax class no-such-class. Use one of: standard, reduced-rate, zero-rate."]
+ * ["", "Tax class cannot be blank. Use one of: standard, reduced-rate, zero-rate."]
+ * [" ", "Tax class cannot be blank. Use one of: standard, reduced-rate, zero-rate."]
+ *
+ * @param string $tax_class Class sent to the ability.
+ * @param string $message Expected error message.
*/
- public function test_unknown_class_is_refused(): void {
+ public function test_unknown_class_is_refused( string $tax_class, string $message ): void {
$create = $this->run_ability(
'woocommerce/tax-rate-create',
array(
'rate' => '19',
- 'class' => 'no-such-class',
+ 'class' => $tax_class,
)
);
$this->assertWPError( $create );
+ $this->assertSame( $message, $create->get_error_message() );
$this->assertSame( 'woocommerce_ability_tax_rate_class_not_found', $create->get_error_code() );
$this->assertSame( 400, $create->get_error_data()['status'] );
$this->assertSame( 0, $this->run_ability( 'woocommerce/tax-rates-query' )['total'], 'No rate should be created.' );
@@ -466,11 +474,12 @@ class TaxRateAbilitiesTest extends \WC_Unit_Test_Case {
array(
'id' => $rate['id'],
'rate' => '7',
- 'class' => 'no-such-class',
+ 'class' => $tax_class,
)
);
$this->assertWPError( $update );
+ $this->assertSame( $message, $update->get_error_message() );
$this->assertSame( 'woocommerce_ability_tax_rate_class_not_found', $update->get_error_code() );
$this->assertSame( '19.0000', $this->run_ability( 'woocommerce/tax-rates-query' )['rates'][0]['rate'], 'The rate should not change.' );
}