Skip to content

fix(tax): skip wu_get_country_states() for wildcard '*' country rates#512

Merged
superdav42 merged 1 commit intomainfrom
feat/t511-universal-tax-fallback
Mar 26, 2026
Merged

fix(tax): skip wu_get_country_states() for wildcard '*' country rates#512
superdav42 merged 1 commit intomainfrom
feat/t511-universal-tax-fallback

Conversation

@superdav42
Copy link
Collaborator

Summary

Completes the universal tax fallback feature (issue #511) by fixing the one missing piece: when a tax rate has country='*' (Apply to all countries), wu_get_country_states('*') was being called unnecessarily. This guard ensures wildcard rates receive an empty state_options array instead.

What was already in place on main

The bulk of the feature was already implemented across three prior PRs/branches:

  • wu_get_applicable_tax_rates() (inc/functions/tax.php) — already returns * rates as a catch-all fallback when no country-specific rate matches the customer's country. Country-specific rates always take precedence.
  • Tax Rates UI (views/taxes/list.php) — the country dropdown already includes <option value="*">Apply to all countries</option> as the first option.
  • Unit tests (tests/WP_Ultimo/Functions/Tax_Functions_Test.php) — comprehensive tests for wildcard fallback behaviour already exist and pass.

What this PR adds

inc/tax/class-tax.phpget_tax_rates() method:

// Before
$rate['state_options'] = wu_get_country_states($rate['country'], 'slug', 'name');

// After
$rate['state_options'] = ('*' !== $rate['country'])
    ? wu_get_country_states($rate['country'], 'slug', 'name')
    : [];

When $fetch_state_options = true (called from the AJAX handler that serves the Tax Rates admin page), passing '*' to wu_get_country_states() would attempt to instantiate Country_* (which doesn't exist), fall back to Country_Default, and return an empty/meaningless result anyway. The guard makes this explicit and avoids the unnecessary lookup.

Testing

  • PHP syntax: php -l passes on all three tax files
  • Static review: diff is 8 lines, single-purpose, no logic change to the fallback mechanism
  • Unit tests: 6 wildcard-specific tests in Tax_Functions_Test.php cover the fallback behaviour end-to-end — CI will run these

References

When a tax rate has country='*' (Apply to all countries), calling
wu_get_country_states('*') is meaningless — there are no states for a
wildcard country. Guard the call so wildcard rates receive an empty
state_options array instead of a spurious lookup.

The rest of the universal tax fallback feature was already in place on main:
- wu_get_applicable_tax_rates() returns '*' rates as a catch-all fallback
  when no country-specific rate matches (inc/functions/tax.php)
- The country dropdown in the Tax Rates UI includes 'Apply to all countries'
  as the first option with value='*' (views/taxes/list.php)
- Comprehensive unit tests for the wildcard fallback behaviour
  (tests/WP_Ultimo/Functions/Tax_Functions_Test.php)

Closes #511
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 26, 2026

Warning

Rate limit exceeded

@superdav42 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 25 minutes and 32 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fb8c1922-8d84-40a3-bd30-b4e924011e65

📥 Commits

Reviewing files that changed from the base of the PR and between 87e9e46 and b64b66c.

📒 Files selected for processing (1)
  • inc/tax/class-tax.php
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/t511-universal-tax-fallback

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

🔨 Build Complete - Ready for Testing!

📦 Download Build Artifact (Recommended)

Download the zip build, upload to WordPress and test:

🌐 Test in WordPress Playground (Very Experimental)

Click the link below to instantly test this PR in your browser - no installation needed!
Playground support for multisite is very limitied, hopefully it will get better in the future.

🚀 Launch in Playground

Login credentials: admin / password

@superdav42
Copy link
Collaborator Author

Note: WP Performance Metrics check failed on this PR. This is not a required check (required: PHP 8.2-8.5, Cypress), so it does not block merge. However, the tax fallback change may have introduced a performance regression — maintainer should review the performance results before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(tax): universal tax fallback — redesigned implementation (apply to all countries option)

1 participant