1. Documentation /
  2. Check if address field contains house number (using WooCommerce Blocks)

Check if address field contains house number (using WooCommerce Blocks)

Note: This snippet does not work with multiple shipping packages. If used, only the first package will be considered.

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.

You need to add code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme.

Check if the shipping address contains a house number

↑ Back to top

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

Check if the billing address contains a house number

↑ Back to top

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

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!' );
    }
}
View on Github

Check if both the billing and the shipping address contain a house number

↑ Back to top

To check if both the billing and the shipping address contain a house number, please use the following code snippet:

get_address('shipping')['address_1'];
    $billing_address  = $order->get_address('billing')['address_1'];

    if ( $shipping_address && ! preg_match( '/[0-9]+/', $shipping_address ) ) {
        throw new Exception( 'Your shipping address must contain a house number!' );
    }

    if ( $billing_address && ! preg_match( '/[0-9]+/', $billing_address ) ) {
        throw new Exception( 'Your billing address must contain a house number!' );
    }
}
View on Github

WooCommerce

The most customizable ecommerce platform for building your online business.

  • 30-day money-back guarantee
  • Support teams across the world
  • Safe and secure online payment