Address number validation in WooCommerce

An easy fix for an issue that affects 4-5% of your orders.


Rob Migchels
Address number validation in WooCommerce

It’s a common issue for many WooCommerce shops: incomplete address numbers in your orders. WooCommerce doesn’t validate if there’s a missing address number. That could be an issue for bigger shops.

I’ve seen the effects myself: a webshop owned by a client saw 4-5% of the orders missing an address number, which required the client to manually contact their consumer, resulting in a huge loss of efficiency.

The solution to missing address numbers

It’s a relatively easy fix, as the script underneath is all you need to copy into your theme’s functions.php.

This snippet validates the address field for an existing address number in the address field during the checkout. If there’s no number found, it generates an notification similar to other validation errors.

//  Validate address field during checkout whether or not it contains a number.
add_action('woocommerce_after_checkout_validation', 'generate_street_error_notice', 10, 1);
function generate_street_error_notice($this_posted) {
    $shipping_add = (isset($this_posted['shipping_address_1']) && $this_posted['shipping_address_1'] != '') ? $this_posted['shipping_address_1'] : $this_posted['billing_address_1'];
    if (!isset($_SESSION['street_split'])) {
        if (!preg_match('#[0-9]#', $shipping_add)) {
            $_SESSION['street_split'] = 1;
            wc_add_notice('<div class="woocommerce-error">' . __('It seems like you forgot your address number.', 'woocommerce') . '</div>', 'error');
        }
    } else {
        unset($_SESSION['street_split']);
    }
}
Show Comments (1)

Comments

  • Steveprict

    Hello. And Bye.

    • Article Author
    • Reply

Related Articles

WordPress

Increase your WordPress speed: The set up

Every second of loading removes 7% conversion [source], on average. Does your WooCommerce website make €1000 a day? Every second costs you about €25,550! I’ve recently...

Posted on by Rob Migchels

Subscribe to my newsletter!

It's a once a half year dive into WordPress, full of insightful articles.

[mailpoet_form id="1"]