2014-02-28 20 views
0
//Add phone number field to customer registration 
add_filter('register_form', 'custom_phone_no_registration_field'); 
function custom_phone_no_registration_field() { 
    echo '<div class="form-row form-row-wide"><label for="reg_phone">'.__('Phone Number', 'woocommerce').' <span class="required">*</span></label> 
    <input type="text" class="input-text" name="phone" id="reg_phone" size="30" value="'.esc_attr($_POST['phone']).'" /></div>'; 

} 

//Validation registration form after submission using the filter registration_errors 
add_action('registration_errors', 'custom_registration_errors_validation', 10,3); 
function custom_registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) { 
    global $woocommerce; 
    extract($_POST); // extracting $_POST into separate variables 
    if(empty($phone)) : 
     $woocommerce->add_error(__('Please, fill in all the required fields.', 'woocommerce')); 
    endif; 
    return $reg_errors; 
} 

我使用register_form中功能的電話號碼字段添加到註冊form.it工作,但是registration_errors功能無法正常工作。任何人都可以請幫助我的代碼?在woocommerce自定義記錄域沒有驗證

回答

0

我認爲你需要在woocommerce_process_registration_errors過濾器上進行驗證。看看includes/class-wc-form-handler.php中的process_registration()方法。

add_action('woocommerce_process_registration_errors', 'custom_registration_error'); 

function custom_registration_errors($validation_error){ 
    if (! isset($_POST['phone'])){ 
     $validation_error = new WP_Error('phone-error', __('Please enter a phone number.', 'your-plugin')); 
    } 
    return $validation_error; 
}