Check if the billing address contains a house number in WooCommerce

Currently, when using the Checkout block from the WooCommerce Blocks plugin, there’s no check if the billing or shipping address field contains a house number. The following code snippets allow to check if these fields contain a number.

To check if the billing address contains a house number, please use the following code snippet:

add_action( 'woocommerce_store_api_checkout_update_order_from_request', 'woo_blocks_address_field_validation', 10, 2);
function woo_blocks_address_field_validation( WC_Order $order, $request ) {
    $billing_address  = $order->get_address('billing')['address_1'];
    
    if ( $billing_address && ! preg_match( '/[0-9]+/', $billing_address ) ) {
        throw new Exception( 'Your billing address must contain a house number!' );
    }
}

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme.