Fields
<?php
function wh_phoneValidateCheckoutFields() {
$billing_phone = $_POST['billing_phone'];
if (strlen($billing_phone) < 8) {
wc_add_notice(__('Verkeerd <strong>telefoonnummer</strong>, graag minimaal 8 cijfers invoeren.', 'balans'), 'error');
}
$address = $_POST['billing_address_1'];
$address = trim($address);
if ( !preg_match( '/[0-9]+/', $address ) ) {
wc_add_notice(__('<strong>Adres</strong> moet een getal bevatten.', 'balans'), 'error');
}
}
add_action('woocommerce_checkout_process', 'wh_phoneValidateCheckoutFields');
add_action( 'woocommerce_after_checkout_validation', function($fields, $errors) {
foreach(['billing', 'shipping'] as $type) {
$field = $fields[$type . '_address_1'];
if(!empty($field) && 0 === preg_match('~[0-9]~', $field)) {
$errors->add('validation', __('<strong>Straat en huisnummer</strong> bevat geen huisnummer', 'suiteseven'));
}
}
}, 10, 2);
add_action( 'woocommerce_after_checkout_validation', function($fields, $errors) {
if($fields['billing_country'] !== 'NL') {
if(empty($fields['billing_house_number_not_nl'])) {
$errors->add('validation', __('<strong>Facturering Huisnummer</strong> is een verplicht veld', 'suiteseven'));
}
}
if($fields['shipping_country'] !== 'NL' && $fields['ship_to_different_address'] == true) {
if(empty($fields['shipping_house_number_not_nl'])) {
$errors->add('validation', __('<strong>Huisnummer verzending</strong> is een verplicht veld', 'suiteseven'));
}
}
}, 10, 2);
add_action( 'woocommerce_checkout_update_order_meta', function($order_id, $data) {
foreach(['billing', 'shipping'] as $type) {
if(!empty($data[$type . '_house_number_not_nl']) && !empty($data[$type . '_address_1'])) {
$house_number = $data[$type . '_house_number_not_nl'];
$number = preg_replace('/\D+/', '', $house_number);
$suffix = preg_replace('/\d+/', '', $house_number);
$street_name = $data[$type . '_address_1'];
$field = sanitize_text_field($street_name. ' ' . $house_number);
update_post_meta($order_id, '_' . $type . '_address_1', $field);
update_post_meta($order_id, '_' . $type . '_house_number', sanitize_text_field($number));
update_post_meta($order_id, '_' . $type . '_street_name', sanitize_text_field($street_name));
update_post_meta($order_id, '_' . $type . '_house_number_suffix', sanitize_text_field($suffix));
}
}
}, 99, 2);