WooCommerce验证billing_address_1结帐字段

问题描述:

我想验证我们的WooCommerce结帐字段之一,即billing_address_1。WooCommerce验证billing_address_1结帐字段

现在,该字段将接受任何地址,但它应该在处理之前检查地址字段是否包含数字。不知道这应该如何在WooCommerce中完成。我也会接受任何非WooCommerce的解决方案。

的网址是:https://www.prikkabelled.nl

你应该在结账时看着这个领域:

enter image description here

您可以使用自定义验证这一点。更改下面的代码按您的要求:

add_action('woocommerce_checkout_process', 'is_phone'); 

function is_phone() { 
    $addr1 = $_POST['billing_address_1']; 
    // check condition if number not found in address 
    if(false) 
     wc_add_notice(__('Address does not contain a number.'), 'error'); 
} 
+0

也不是这样.. – mdarmanin

+0

你可以显示你使用的代码吗? –

这里是你可以通过的functions.php

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') !== FALSE) 
     wc_add_notice(__('Your address field contains numeric value'), 'error'); 
} 
+0

这不起作用.. – mdarmanin

+0

你能分享你的代码吗? –

+0

我没有做任何特别的事情,只是粘贴你在functions.php – mdarmanin

这就是我一直在寻找的答案,我直接从拿到添加到结帐的进展步骤WooCommerce开发者社区。

// 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.'); 
    } 
}