2017-04-24 109 views
0

我想驗證我們的WooCommerce結帳字段之一,即billing_address_1。WooCommerce驗證billing_address_1結帳字段

現在,該字段將接受任何地址,但它應該在處理之前檢查地址字段是否包含數字。不知道這應該如何在WooCommerce中完成。我也會接受任何非WooCommerce的解決方案。

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

你應該在結賬時看着這個領域:

enter image description here

回答

0

您可以使用自定義驗證這一點。更改下面的代碼按您的要求:

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

你可以顯示你使用的代碼嗎? –

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

0

這就是我一直在尋找的答案,我直接從拿到添加到結帳的進展步驟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.'); 
    } 
}