我添加了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
}

推荐答案

try 以下重新访问的代码,这些代码根据选定的用户角色处理显示/隐藏字段,验证正确的字段(可选),并将字段值保存到数据库.

// Display fields in registration account form
add_action('woocommerce_register_form_start', 'display_account_registration_field');
function display_account_registration_field() {
    wc_enqueue_js("var role = $('select[name=role]').val();
    function showHideRegFields( role ) {
        if ( role == 'subscriber') {
            $('#billing_company_field').hide();
            $('#billing_product_field').show();
        } else {
            $('#billing_company_field').show();
            $('#billing_product_field').hide();
        }
        console.log(role); // For testing, to be removed
    }
    showHideRegFields( role );
    $(document.body).on('change', 'select[name=role]', function() {
        showHideRegFields( $(this).val() );
    });");

    $role = isset($_POST['role']) && ! empty($_POST['role']) ? $_POST['role'] : 'customer';
    ?>
    <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 value="customer"<?php echo $role === 'customer' ? ' selected' : '';?> ><?php _e( 'Client', 'woocommerce' ); ?></option>
            <option value="subscriber"<?php echo $role === 'subscriber' ? ' selected' : '';?> ><?php _e( 'Agent', 'woocommerce' ); ?></option>
        </select>
    </p>
    <p class="form-row form-row-last" id="billing_company_field" style="display:none;">
        <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" id="billing_product_field" style="display:none;">
        <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
}

// registration Field validation
add_filter( 'woocommerce_registration_errors', 'account_registration_field_validation', 10, 3 );
function account_registration_field_validation( $errors, $username, $email ) {
    if ( $_POST['role'] === 'customer' && isset($_POST['billing_company']) && empty($_POST['billing_company']) ) {
        $errors->add( 'billing_company_error', __( 'Name Company is a required field', 'woocommerce' ) );
    }
    if ( $_POST['role'] === 'subscriber' && isset($_POST['billing_product']) && empty($_POST['billing_product']) ) {
        $errors->add( 'billing_product_error', __( 'Company Product is a required field', 'woocommerce' ) );
    }
    return $errors;
}

// Save registration Field value
add_action( 'woocommerce_created_customer', 'save_account_registration_field' );
function save_account_registration_field( $customer_id ) {
    if ( $_POST['role'] === 'customer' && isset($_POST['billing_company']) && ! empty($_POST['billing_company']) ) {
        update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
    }
    if ( $_POST['role'] === 'subscriber' && isset($_POST['billing_product']) && ! empty($_POST['billing_product']) ) {
        update_user_meta( $customer_id, 'billing_product', sanitize_text_field( $_POST['billing_product'] ) );
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

如何使用PHP停止foreach循环中的重复项?

Yahoo Finance API文件_GET_CONTENTS 429请求太多

LaravelEloquent 的地方条件父/子与第三模型多对多

使用.htaccess将任何路由重定向到文件

让所有具有相同名称的产品在WooCommerce中更改价格

如何在php中生成包含第100秒的时间序列?

不包括WooCommerce中单独销售的产品

Chillerlan/php-iOS名称字段的QR vCard错误

如何在laravel中使用script type= text/html/script

针对给定产品类别的WooCommerce计数审核

允许客户定义特定WooCommerce产品的价格

在WooCommerce上设置简单和可变产品的最小订单量

execute_query 和prepare+execute 有什么区别?

在 WooCommerce 订阅续订订单中设置送货方式

无法允许元素(img:'src')与symfony html-sanitizer(symfony6.3 php 8.2)

WooCommerce 以编程方式添加的费用不会持续存在

在php中计算两个没有假期的日期之间的小时差

我的功能以舒适的方式显示数组 struct

在WooCommerce购物车和 checkout 页面中更改总计文本

PHP获取根据辞职日期自动续订的合同终止日期