Require a House Number in the Street Address
Catches addresses typed without a house or building number, one of the most common reasons parcels come back undelivered.
Opens WPIDE on
Code
$wpide_house_number_skip = array( 'IE' ); // Countries to skip.
add_action( 'woocommerce_after_checkout_validation', function ( $data, $errors ) use ( $wpide_house_number_skip ) {
if ( empty( WC()->cart ) || ! WC()->cart->needs_shipping() ) {
return;
}
$prefix = ! empty( $data['ship_to_different_address'] ) ? 'shipping_' : 'billing_';
$country = (string) ( $data[ $prefix . 'country' ] ?? '' );
$line = trim( (string) ( $data[ $prefix . 'address_1' ] ?? '' ) . ' ' . (string) ( $data[ $prefix . 'address_2' ] ?? '' ) );
if ( '' !== $line && ! in_array( $country, $wpide_house_number_skip, true ) && ! preg_match( '/\d/', $line ) ) {
$errors->add( 'validation', 'Please include your house or building number in the street address.' );
}
}, 10, 2 );
Notes
Only orders that need shipping are checked; add countries where addresses often have no numbers to $wpide_house_number_skip.
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.