Woocommerce 注册中的附加用户角色选择字段

Joh*_*ohn 4 php wordpress user-registration user-roles woocommerce

.

WooCommerce 注册表中需要用户角色选择。用户应该有一个下拉菜单,可以在“客户”和“经销商”(ID)wordpress 角色之间进行选择。不幸的是,我组装代码的尝试失败了。

以下代码未分配所选角色,我被卡住了。尽管选择了“经销商”,但注册用户仍获得默认角色“客户”。

我已经更改了这个答案的代码(我不知道这段代码是否有效)

我做错了什么?

我正在使用的代码:

/* To add WooCommerce registration form custom fields. */

function WC_extra_registation_fields() {?>
<p class="form-row form-row-first">
    <label for="reg_role"><?php _e( 'Privat or commercial?', '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">private</option> 
    <option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'reseller') esc_attr_e( 'selected' ); ?> value="reseller">commercial</option>
    </select> 
</p>

<?php
}

add_action( 'woocommerce_register_form', 'WC_extra_registation_fields');


/* To validate WooCommerce registration form custom fields.  */
function WC_validate_reg_form_fields($username, $email, $validation_errors) {
if (isset($_POST['role']) && empty($_POST['role']) ) {
    $validation_errors->add('role_error', __('Role required!', 'woocommerce'));
}

return $validation_errors;
}

add_action('woocommerce_register_post', 'WC_validate_reg_form_fields', 10, 3);


/* To save WooCommerce registration form custom fields. */
function WC_save_registration_form_fields($customer_id) {

//Role field
if (isset($_POST['role'])) {
    update_user_meta($customer_id, 'role', sanitize_text_field($_POST['role']));
}

}

add_action('woocommerce_created_customer', 'WC_save_registration_form_fields');
Run Code Online (Sandbox Code Playgroud)

Loi*_*tec 6

需要首先在 Wordpress 中创建用户角色“经销商”。这可以通过代码或插件来完成,但这不是这个问题/答案的目的。

用户角色wp_usermeta使用meta_key wp_capabilitiesfor 值数组在表中注册,因为用户可以拥有多个用户角色。

在 Woocommerce 注册中,用户无论如何都会注册'customer'用户角色。因此,您只需要为'reseller'选择而进行更改

此时你可以做两件事:

  • 案例 1 - 将用户角色从“客户”更改为“经销商”
  • 案例 2 - 添加到客户用户角色,即“经销商”用户角色

所以你的代码将是:

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( 'Privat or commercial?', '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">private</option>
            <option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'reseller') esc_attr_e( 'selected' ); ?> value="reseller">commercial</option>
            </select>
        </p>
    <?php
}

// Validate WooCommerce registration form custom fields.
add_action( 'woocommerce_register_post', 'wc_validate_reg_form_fields', 10, 3 );
function wc_validate_reg_form_fields($username, $email, $validation_errors) {
    if (isset($_POST['role']) && empty($_POST['role']) ) {
        $validation_errors->add('role_error', __('Role required!', 'woocommerce'));
    }
    return $validation_errors;
}
Run Code Online (Sandbox Code Playgroud)

您将添加以下内容:

对于案例 1

// To save WooCommerce registration form custom fields.
add_action( 'woocommerce_created_customer', 'wc_save_registration_form_fields' );
function wc_save_registration_form_fields( $customer_id ) {
    if ( isset($_POST['role']) ) {
        if( $_POST['role'] == 'reseller' ){
            $user = new WP_User($customer_id);
            $user->set_role('reseller');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以在数据库中被正确注册(因为它应该被 Wordpress 读取,但前提是用户角色“经销商”存在):

在此处输入图片说明

或者对于案例 2

// To save WooCommerce registration form custom fields.
add_action( 'woocommerce_created_customer', 'wc_save_registration_form_fields' );
function wc_save_registration_form_fields( $customer_id ) {
    if ( isset($_POST['role']) ) {
        if( $_POST['role'] == 'reseller' ){
            $user = new WP_User($customer_id);
            $user->add_role('reseller');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以在数据库中被正确注册(因为它应该被 Wordpress 读取,但前提是用户角色“经销商”存在):

在此处输入图片说明

代码位于活动子主题(或主题)的 function.php 文件中。测试和工作。


如果我在用户注册后使用以下代码:

$user_roles = wp_get_current_user()->roles;
print_r($user_roles);
Run Code Online (Sandbox Code Playgroud)

我得到这个显示:

  • 对于情况 1: Array ( [0] => reseller )
  • 对于情况 2: Array ( [0] => customer [1] => reseller )

您当然需要使用像用户角色编辑器这样的插件(如果还没有完成)来创建和定义您的“经销商”用户角色的功能……