Add LV postcode formatting and tests

This commit is contained in:
Saggre 2022-02-01 14:42:38 +02:00 committed by vedanshujain
parent 33cca70beb
commit 474494dcab
2 changed files with 11 additions and 0 deletions

View File

@ -993,6 +993,11 @@ function wc_format_postcode( $postcode, $country ) {
case 'NL':
$postcode = substr_replace( $postcode, ' ', 4, 0 );
break;
case 'LV':
if ( preg_match( '/(?:LV)?-?(\d+)/i', $postcode, $matches ) ) {
$postcode = count( $matches ) >= 2 ? "LV-$matches[1]" : $postcode;
}
break;
}
return apply_filters( 'woocommerce_format_postcode', trim( $postcode ), $country );

View File

@ -801,6 +801,12 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
// JP postcode.
$this->assertEquals( '999-9999', wc_format_postcode( '9999999', 'JP' ) );
// Test LV postcode without mandatory country code.
$this->assertEquals( 'LV-1337', wc_format_postcode( '1337', 'LV' ) );
// Test LV postcode with incorrect format (no dash).
$this->assertEquals( 'LV-1337', wc_format_postcode( 'lv1337', 'LV' ) );
// Test empty NL postcode.
$this->assertEquals( '', wc_format_postcode( '', 'NL' ) );
}