Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 416
Marco V Avatar asked Oct 24 '25 23:10

Marco V


2 Answers

    // 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.' ); 
        }
    }
like image 174
Marco V Avatar answered Oct 26 '25 13:10

Marco V


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

like image 23
LoicTheAztec Avatar answered Oct 26 '25 12:10

LoicTheAztec