Puddinq.com sharing knowledge

WooCommerce Checkout require number if not ‘local pickup’

WooCommerce Checkout require number if not 'local pickup'

In the Netherlands WooCommerce removed the second address line. This made the error rate for people failing to insert the housenumber rise. I had two options, put the second line back and make it rweuired, or check if the one line that is there has a number in it..

add_action('woocommerce_checkout_process', 'custom_validation_process');

function custom_validation_process()

{

    global $woocommerce;

    $chosen_shipping = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping = explode(':', $chosen_shipping);

    if ( $chosen_shipping[0] != 'local_pickup' ) {

        if (isset($_POST['billing_address_1']) and $_POST['billing_address_1'] != '') {
            if (!preg_match('/([0-9]+)/Uis', $_POST['billing_address_1'])) {

                if (function_exists('wc_add_notice')) {
                    wc_add_notice(__('Om je pakketje te bezorgen hebben we ook je huisnummer nodig. Voeg deze toe in het adresveld.'),
                        'error');
                } else {
                    $woocommerce->add_error(__('Om je pakketje te bezorgen hebben we ook je huisnummer nodig. Voeg deze toe in het adresveld.'));
                }

            }

        }


        if (isset($_POST['ship_to_different_address'])) {

            if (isset($_POST['shipping_address_1']) and $_POST['shipping_address_1'] != '') {

                if (!preg_match('/([0-9]+)/Uis', $_POST['shipping_address_1'])) {

                    if (function_exists('wc_add_notice')) {
                        wc_add_notice(__('Om je pakketje te bezorgen hebben we ook je huisnummer nodig. Voeg deze toe in het adresveld.'),
                            'error');
                    } else {
                        $woocommerce->add_error(__('Om je pakketje te bezorgen hebben we ook je huisnummer nodig. Voeg deze toe in het adresveld.'));
                    }

                }

            }

        }
    }

}