我添加了2个自定义字段到注册表单,我需要隐藏它们取决于用户 Select 的"代理"角色,但我不能这样做,以下是我的代码示例,我将很高兴得到任何帮助.
//that's how I added the fields that need to be hidden if the agent user role is selected
add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields');
function text_domain_woo_reg_form_fields() {
?>
<p class="form-row form-row-first">
<label for="billing_company"><?php _e('Name Company', 'text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_company" id="billing_company" value="<?php if (!empty($_POST['billing_company'])) esc_attr_e($_POST['billing_company']); ?>" />
</p>
<p class="form-row form-row-last">
<label for="billing_product"><?php _e('Company Product', 'text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_product" id="billing_product" value="<?php if (!empty($_POST['billing_product'])) esc_attr_e($_POST['billing_product']); ?>" />
</p>
<div class="clear"></div>
<?php
}
//Thats how I tried to hide the fields
add_action( 'woocommerce_register_form', 'hide_show_field', 9999 );
function hide_show_field() {
wc_enqueue_js( "
$('#reg_role').keyup(function() {
if ($(this).val().length == 'subscriber') {
$('#billing_company').hide();
} else {
$('#billing_product').hide();
}
});
" );
}
//adding a role selection field
add_action( 'woocommerce_register_form', 'wc_extra_registation_fields' );
function wc_extra_registation_fields() {
?>
<p class="form-row form-row-first">
<label for="reg_role"><?php _e( 'Agent?', 'woocommerce' ); ?></label>
<select class="input-text" name="role" id="reg_role">
<option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'customer') esc_attr_e( 'selected' ); ?> value="customer">Client</option>
<option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'subscriber') esc_attr_e( 'selected' ); ?> value="subscriber">Agent</option>
</select>
</p>
<?php
}