Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Check if WooCommerce address_field_1 contains house number before processing order

Sometimes, in WooCommerce, the customer is required to fill in street name and house number in a single field.

In this case, we want to then validate the billing_address_1 WooCommerce checkout field to check if it contains numbers before processing the order. We have tried a number of methods to get this done but without any luck.

This standard WooCommerce method doesn't work:

add_action('woocommerce_checkout_process', 'custom_checkout_field_check');

    function custom_checkout_field_check() {
        // Check if set, if its not set add an error.
        if ( $_POST['billing_address_1'] && strpbrk($_POST['billing_address_1'], '1234567890') )
            wc_add_notice( __( 'Het adresveld moet minimaal een huisnummer bevatten' ), 'error' );
    }

These return bool(false) on the checkout page:

var_dump($_POST['billing_address_1'] == true);
var_dump($_POST['billing_address_2'] == true);
var_dump($_POST['billing_postcode'] == true);
var_dump($_POST['billing_email'] == true);

This front-end workaround doesn't work.

document.querySelector("#place_order").addEventListener("click", validateAddressField);

function validateAddressField () {
    console.log('Okay dan!');
}

What else can I try to ensure validation takes place before the order is processed?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

    // Check if address field contains house number otherwise provide error message

    add_action( 'woocommerce_after_checkout_validation', 'validate_checkout', 10, 2); 

    function validate_checkout( $data, $errors ){ 
        if (  ! preg_match('/[0-9]/', $data[ 'billing_address_1' ] ) ){ 
            $errors->add( 'address', 'Sorry, but the address you provided does not contain a house number.' ); 
        }
    }
about 4 years ago · Santiago Trujillo Report

0

This isn't working correctly in your code: strpbrk($_POST['billing_address_1'], '1234567890').
he PHP function preg_match() is more appropriate here.

So I have make some little changes in your code to make it work as you expect:

add_action('woocommerce_checkout_process', 'address_field_validation', 10, 0);
function address_field_validation() {

    // The field value to check
    $post_value = $_POST['billing_address_1'];

    // If there is no number in this field value, stops the checkout process
    // and displays an error message.
    if ( $post_value && ! preg_match( '/[0-9]+/', $post_value ) ) {

        // The error message
        throw new Exception( sprintf( __( 'Het adresveld moet minimaal een huisnummer bevatten', 'woocommerce' ) ) );
    }
}

This code is tested and works on WooCommerce versions 2.6.x and 3.0+…

*This code goes in function.php file of your active child theme (or theme) or also in any plugin file.


Reference: WC_Checkout - process_checkout() source code

about 4 years ago · Santiago Trujillo Report

0

You can try using hook :- woocommerce_after_checkout_validation

Please refer how to use this hook and sample code here

Let me know if you need anything else...

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!