Require Latin Characters in Addresses
Asks customers to type their name and address in Latin letters, for international carriers whose labels cannot print other alphabets.
Opens WPIDE on
Code
$wpide_latin_fields = array( 'first_name', 'last_name', 'address_1', 'address_2', 'city' ); // Fields to check.
add_action( 'woocommerce_after_checkout_validation', function ( $data, $errors ) use ( $wpide_latin_fields ) {
foreach ( array( 'billing', 'shipping' ) as $type ) {
if ( 'shipping' === $type && empty( $data['ship_to_different_address'] ) ) {
continue;
}
foreach ( $wpide_latin_fields as $field ) {
$value = (string) ( $data[ $type . '_' . $field ] ?? '' );
if ( '' !== $value && ! preg_match( '/^[\p{Latin}\p{N}\s\'\-.,\/#&()]+$/u', $value ) ) {
$errors->add( 'validation', 'Please enter your name and address using Latin letters (A to Z) so the courier can read the label.' );
return;
}
}
}
}, 10, 2 );
Notes
Accented letters such as é, ü and ø are allowed; edit $wpide_latin_fields to check more or fewer fields.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.