我正在尝试为我的网站定制注册表单。我一直在使用本指南,并已成功为我的注册表单创建/存储其他字段。定制注册指南。
I have created a drop-down/select field in my registration form for the user to select whether they want a Standard or Education account-type when registering:
![enter image description here](https://i.stack.imgur.com/rWVHc.png)
如果他们选择教育帐户类型,我希望显示其他字段,以便我可以收集他们学校的其他数据。有没有办法让我根据表单中另一个字段的当前值提供条件逻辑来判断某个字段是否可见?
Here's kind of an example of what I would want to accomplish:
![enter image description here](https://i.stack.imgur.com/1edA4.png)
有人可以指出我完成条件表单字段的正确方向吗?另外,我不想为此使用第三方插件。对此,拥有完全控制权非常重要。
编辑:
根据要求,这是我的函数的完整代码:
<?php
/*
Plugin Name: Custom Account Fields
Plugin Author: Case Silva
*/
//Create Custom Fields
if(!function_exists('get_custom_fields')){
function get_custom_fields(){
return apply_filters('custom_fields', array(
'verified_education_acct' => array(
'type' => 'checkbox',
'label' => __('Verified?'),
'required' => false,
'hide_in_account' => true,
'hide_in_admin' => false,
'hide_in_registration' => true,
'hide_in_checkout' => true
),
'customer_id_num' => array(
'type' => 'text',
'label' => __('Customer ID# '),
'placeholder' => __('e.g. 1234567890'),
'required' => false,
'hide_in_account' => true,
'hide_in_admin' => false,
'hide_in_checkout' => true,
'hide_in_registration' => true
),
'account_type' => array(
'type' => 'select',
'label' => __('What type of account will this be?'),
'options' => array(
'' => __('Select an option...'),
1 => __('Education'),
2 => __('Standard')
),
'required' => true,
'hide_in_account' => true,
'hide_in_admin' => false,
'hide_in_checkout' => false,
'hide_in_registration' => false
),
'school_name' => array(
'type' => 'text',
'label' => __('School Name'),
'placeholder' => __('e.g. North Middle School'),
'required' => true,
'hide_in_account' => 'account_type' != 1,
'hide_in_admin' => false,
'hide_in_checkout' => 'account_type' != 1,
'hide_in_registration' => 'account_type' != 1
),
));
}
}
//Add them to User Area
if(!function_exists('print_user_frontend_fields')){
function print_user_frontend_fields(){
$fields = get_custom_fields();
$user_logged_in = is_user_logged_in();
foreach ($fields as $key => $field_args) {
if($user_logged_in && !empty($field_args['hide_in_account'])){
continue;
}
if(! $user_logged_in && ! empty($field_args['hide_in_registration'])){
continue;
}
woocommerce_form_field($key, $field_args);
}
}
}
//Add them to Admin Area
if(!function_exists('print_user_admin_fields')){
function print_user_admin_fields(){
$fields = get_custom_fields();
?>
<h2><?php _e('Education/School Information'); ?></h2>
<table class = "form-table" id = "additional-information">
<tbody>
<?php foreach ($fields as $key => $field_args) { ?>
<?php
if(! empty($field_args['hide_in_admin'])){
continue;
}
$user_id = get_edit_user_id();
$value = st_get_userdata($user_id, $key);
?>
<tr>
<th>
<label for="<?php echo $key; ?>"><?php echo $field_args['label']; ?></label>
</th>
<td>
<?php $field_args['label'] = false; ?>
<?php woocommerce_form_field($key, $field_args, $value); ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
}
}
//Save them to the database
if(!function_exists('save_acct_fields')){
function save_acct_fields($customer_id){
$fields = get_custom_fields();
$sanitized_data = array();
foreach ($fields as $key => $field_args) {
if(! is_field_visible($field_args)){
continue;
}
$sanitize = isset($field_args['sanitize']) ? $field_args['sanitize'] : 'wc_clean';
$value = isset($_POST[$key]) ? call_user_func($sanitize, $_POST[$key]) : '';
if(is_userdata($key)){
$sanitized_data[$key] = $value;
continue;
}
update_user_meta($customer_id, $key, $value);
}
if(! empty($sanitized_data)){
$sanitized_data['ID'] = $customer_id;
wp_update_user($sanitized_data);
}
}
}
//Check if field is visible on page
if(!function_exists('is_field_visible')){
function is_field_visible($field_args){
$visible = true;
$action = filter_input(INPUT_POST, action);
if(is_admin() && ! empty($field_args['hide_in_admin'])){
$visible = false;
} elseif((is_account_page() || $action === 'save_account_details') && is_user_logged_in() && ! empty($field_args['hide_in_account'])){
$visible = false;
} elseif((is_account_page() || $action === 'save_account_details') && ! is_user_logged_in() && ! empty($field_args['hide_in_registration'])){
$visible = false;
} elseif(is_checkout() && ! empty($field_args['hide_in_checkout'])){
$visible = false;
}
return $visible;
}
}
//Check if field is predefined
if(!function_exists('is_userdata')){
function is_userdata($key){
$userdata = array(
'user_pass',
'user_login',
'user_nicename',
'user_url',
'user_email',
'display_name',
'nickname',
'first_name',
'last_name',
'description',
'rich_editing',
'user_registered',
'role',
'jabber',
'aim',
'yim',
'show_admin_bar_front'
);
return in_array($key, $userdata);
}
}
//Populate form with submitted data
if(!function_exists('get_edit_user_id')){
function get_edit_user_id(){
return isset($_GET['user_id']) ? (int) $_GET['user_id'] : get_current_user_id();
}
}
//Access saved data
if(!function_exists('st_get_userdata')){
function st_get_userdata($user_id, $key){
if(!is_userdata($key)){
return get_user_meta($user_id, $key, true);
}
$userdata = get_userdata($user_id);
if(!$userdata || ! isset($userdata->{$key})){
return '';
}
return $userdata->{$key};
}
}
add_action('woocommerce_register_form', 'print_user_frontend_fields', 10);
add_action('woocommerce_edit_account_form', 'print_user_frontend_fields', 10);
add_action('show_user_profile', 'print_user_admin_fields', 30);
add_action('edit_user_profile', 'print_user_admin_fields', 30);
add_action('woocommerce_created_customer', 'save_acct_fields');
add_action('personal_options_update', 'save_acct_fields');
add_action('edit_user_profile_update', 'save_acct_fields');
add_action('woocommerce_save_account_details', 'save_acct_fields');
?>