Skip to content

fix: Add postcode validation for Latvia - #67460

Merged
opr merged 3 commits into
woocommerce:trunkfrom
Saggre:feature/lv-postcode-validation
Aug 12, 2026
Merged

opr merged 3 commits into
woocommerce:trunkfrom
Saggre:feature/lv-postcode-validation

Conversation

@Saggre

@Saggre Saggre commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Submission Review Guidelines:

Changes proposed in this Pull Request:

  • Add support for LV postcode validation

  • Add LV cases to WC_Validation_Test

LV currently falls through to default: $valid = true, so anything passes. I added formatting support for LV in #45478 but never the validation half, so wc_format_postcode( '1050', 'LV' ) returns LV-1050 while is_postcode( 'anything', 'LV' ) returns true.

The pattern is 4 digits with an optional LV- prefix, prefix handling like the existing DK case, case insensitive like ES and NL.

Documentation on the format:

https://www.upu.int/UPU/media/upu/PostalEntitiesFiles/addressingUnit/lvaEn.pdf
https://en.wikipedia.org/wiki/Postal_codes_in_Latvia
LV-NNNN

Closes #67459.

How to test the changes in this Pull Request:

Covered by new data provider cases. Run with:

pnpm --filter="@woocommerce/plugin-woocommerce" test:php:env --filter="WC_Validation_Test"

Manual:

  1. Sell to Latvia, go to checkout with a product in the cart.
  2. Postcode ZZZZ: accepted on trunk, rejected on this branch.
  3. LV-1050, lv-1050 and 1050 still pass.

Testing that has already taken place:

Test runs above on wp-env, only WooCommerce active. PHPCS and PHPStan report nothing new on the changed files.

Milestone

Changelog entry

  • Automatically create a changelog entry from the details below.
  • This Pull Request does not require a changelog entry. (Comment required below)

Changelog entry is committed in the branch at plugins/woocommerce/changelog/67459-lv-postcode-validation (patch / fix).

@Saggre Saggre mentioned this pull request Aug 6, 2026
2 of 5 tasks
@coderabbitai

coderabbitai Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro Plus

Run ID: 22febcc0-5e46-4731-8d71-61ce7b69a5c2

📥 Commits

Reviewing files that changed from the base of the PR and between 50c57ba and ae653db.

📒 Files selected for processing (3)
  • plugins/woocommerce/changelog/67459-lv-postcode-validation
  • plugins/woocommerce/includes/class-wc-validation.php
  • plugins/woocommerce/tests/php/includes/class-wc-validation-test.php
🚧 Files skipped from review as they are similar to previous changes (3)
  • plugins/woocommerce/changelog/67459-lv-postcode-validation
  • plugins/woocommerce/tests/php/includes/class-wc-validation-test.php
  • plugins/woocommerce/includes/class-wc-validation.php

📝 Walkthrough

Walkthrough

The change adds Latvia postcode validation to WC_Validation::is_postcode, tests valid and invalid formats, and documents the fix in the changelog.

Changes

Latvia postcode validation

Layer / File(s) Summary
Implement and verify Latvia postcode validation
plugins/woocommerce/includes/class-wc-validation.php, plugins/woocommerce/tests/php/includes/class-wc-validation-test.php, plugins/woocommerce/changelog/67459-lv-postcode-validation
The validator accepts optional LV- prefixes and four-digit postcodes. Tests cover valid and invalid values. The changelog records the fix.

Estimated code review effort: 1 (Trivial) | ~3 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding Latvian postcode validation.
Description check ✅ Passed The description explains the Latvia validation bug, the proposed fix, test coverage, and validation behavior.
Linked Issues check ✅ Passed The changes address issue #67459 by validating four-digit Latvian postcodes with optional case-insensitive LV- prefixes and rejecting invalid values.
Out of Scope Changes check ✅ Passed The changes are limited to Latvian postcode validation, related tests, and the corresponding changelog entry.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@plugins/woocommerce/changelog/67459-lv-postcode-validation`:
- Around line 1-4: Update the changelog entry wording to state that Latvia
previously accepted invalid values, rather than claiming it accepted any value;
preserve the existing significance and type metadata.

In `@plugins/woocommerce/includes/class-wc-validation.php`:
- Around line 146-148: Update the LV case in the postcode validation switch to
use a non-capturing optional LV- prefix and anchor the pattern with \z instead
of $, ensuring a trailing newline is rejected. Add a validation fixture covering
an LV postcode ending in a newline and assert it is invalid.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro Plus

Run ID: 59b514cb-3bdf-48cf-a6c1-da29325f697c

📥 Commits

Reviewing files that changed from the base of the PR and between 6000919 and 50c57ba.

📒 Files selected for processing (3)
  • plugins/woocommerce/changelog/67459-lv-postcode-validation
  • plugins/woocommerce/includes/class-wc-validation.php
  • plugins/woocommerce/tests/php/includes/class-wc-validation-test.php

Comment thread plugins/woocommerce/changelog/67459-lv-postcode-validation Outdated
Comment thread plugins/woocommerce/includes/class-wc-validation.php
@ralucaStan

Copy link
Copy Markdown
Contributor

@Saggre thank you for your contribution. There are some comments from CodeRabbit that need your attention. Could you take a look?

After that please assign the PR to woocommerce/rubik to get a review from us.

@Saggre

Saggre commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

@ralucaStan I don't have permission to assign to woocommerce/rubik, but the changes have been made.

@ralucaStan
ralucaStan requested review from a team and opr and removed request for a team August 12, 2026 07:30
@github-actions github-actions Bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Aug 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Testing Guidelines

Hi ,

Apart from reviewing the code changes, please make sure to review the testing instructions (Guide) and verify that relevant tests (E2E, Unit, Integration, etc.) have been added or updated as needed.

Reminder: PR reviewers are required to document testing performed. This includes:

  • 🖼️ Screenshots or screen recordings.
  • 📝 List of functionality tested / steps followed.
  • 🌐 Site details (environment attributes such as hosting type, plugins, theme, store size, store age, and relevant settings).
  • 🔍 Any analysis performed, such as assessing potential impacts on environment attributes and other plugins, conducting performance profiling, or using LLM/AI-based analysis.

⚠️ Within the testing details you provide, please ensure that no sensitive information (such as API keys, passwords, user data, etc.) is included in this public issue.

@opr opr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Saggre this looks great and tests out well 👏🏼 thanks for the contribution.

@opr
opr merged commit 6fcb578 into woocommerce:trunk Aug 12, 2026
44 of 46 checks passed
@github-actions github-actions Bot added this to the 11.2.0 milestone Aug 12, 2026
@github-actions github-actions Bot added the needs: documentation The issue/PR requires documentation to be added. label Aug 12, 2026
opr added a commit that referenced this pull request Aug 13, 2026
WC_Validation::is_postcode() does not normalise its input, so a Latvian
postcode can reach it as "LV1050" or "LV 1050" rather than "LV-1050".
The rule added in #67460 required the literal hyphen, so it rejected
both. Before that PR they fell through to the fail-open default and were
accepted.

POST /wc/store/v1/checkout reaches this on a normal request.
AbstractAddressSchema::sanitize_callback() formats the postcode with the
raw country value but validates against the uppercased one, so a client
sending country "lv" skips the LV case in wc_format_postcode() (which
would reinsert the hyphen) and then hits the LV case in is_postcode().
Verified against a local store: rejected on trunk, accepted here.

The separator is a literal space rather than \s, so a newline or tab
between the prefix and the digits is still rejected.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
opr added a commit that referenced this pull request Aug 17, 2026
* fix: accept Latvian postcodes without the LV prefix hyphen

WC_Validation::is_postcode() does not normalise its input, so a Latvian
postcode can reach it as "LV1050" or "LV 1050" rather than "LV-1050".
The rule added in #67460 required the literal hyphen, so it rejected
both. Before that PR they fell through to the fail-open default and were
accepted.

POST /wc/store/v1/checkout reaches this on a normal request.
AbstractAddressSchema::sanitize_callback() formats the postcode with the
raw country value but validates against the uppercased one, so a client
sending country "lv" skips the LV case in wc_format_postcode() (which
would reinsert the hyphen) and then hits the LV case in is_postcode().
Verified against a local store: rejected on trunk, accepted here.

The separator is a literal space rather than \s, so a newline or tab
between the prefix and the digits is still rejected.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* docs: document the LV postcode pattern and cover its edges

Explain the optional prefix, the single hyphen or space separator, and
why the pattern uses a literal space and \z rather than \s and $.

Add rows for repeated and invalid separators, the top of the four digit
range, and trailing characters. No behaviour change; every one of these
already validated this way.

Raised by CodeRabbit on #67684.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs: documentation The issue/PR requires documentation to be added. plugin: woocommerce Issues related to the WooCommerce Core plugin.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Latvian postcodes are not validated

3 participants

Sponsor
SponsoredKunjungi sekarang
Promo