From 25b42700c2f39f26420ac6d758993b547f876e9d Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 9 Sep 2016 14:51:41 -0300 Subject: [PATCH 1/2] Added support for the new US postcode format with 9 digits --- includes/wc-formatting-functions.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/includes/wc-formatting-functions.php b/includes/wc-formatting-functions.php index 29393f0af02..1030b961d3f 100644 --- a/includes/wc-formatting-functions.php +++ b/includes/wc-formatting-functions.php @@ -685,13 +685,16 @@ function wc_format_postcode( $postcode, $country ) { break; case 'BR' : case 'PL' : - $postcode = trim( substr_replace( $postcode, '-', -3, 0 ) ); + $postcode = substr_replace( $postcode, '-', -3, 0 ); break; case 'JP' : - $postcode = trim( substr_replace( $postcode, '-', 3, 0 ) ); + $postcode = substr_replace( $postcode, '-', 3, 0 ); break; case 'PT' : - $postcode = trim( substr_replace( $postcode, '-', 4, 0 ) ); + $postcode = substr_replace( $postcode, '-', 4, 0 ); + break; + case 'US' : + $postcode = rtrim( substr_replace( $postcode, '-', 5, 0 ), '-' ); break; } From 7a4e0da1c1178e7f7146cc64fc215d34be2f2ac0 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 9 Sep 2016 15:05:44 -0300 Subject: [PATCH 2/2] Update tests for wc_format_postcode() --- tests/unit-tests/formatting/functions.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/unit-tests/formatting/functions.php b/tests/unit-tests/formatting/functions.php index b372eeb847a..c02ba2c760c 100644 --- a/tests/unit-tests/formatting/functions.php +++ b/tests/unit-tests/formatting/functions.php @@ -615,9 +615,12 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case { */ public function test_wc_format_postcode() { - // generic postcode + // Generic postcode $this->assertEquals( '02111', wc_format_postcode( ' 02111 ', 'US' ) ); + // US 9-digit postcode + $this->assertEquals( '02111-9999', wc_format_postcode( ' 021119999 ', 'US' ) ); + // UK postcode $this->assertEquals( 'PCRN 1ZZ', wc_format_postcode( 'pcrn1zz', 'GB' ) ); }