繁体   English   中英

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

[英]Additional user role select field in Woocommerce registration

.

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');

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

用户角色使用meta_key wp_capabilities用于值数组的形式注册在wp_usermeta表中,因为用户可以具有许多用户角色。

在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;
}

您将添加以下内容:

对于案例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');
        }
    }
}

因此,在数据库中已正确注册(因为只有当用户角色“经销商”存在时,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');
        }
    }
}

因此,在数据库中已正确注册(因为只有当用户角色“经销商”存在时,Wordpress才应该读取它):

在此处输入图片说明

代码进入您的活动子主题(或主题)的function.php文件中。 经过测试和工作。


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

$user_roles = wp_get_current_user()->roles;
print_r($user_roles);

我得到这个显示:

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

当然,您肯定需要使用“ 用户角色编辑器”之类的插件(如果尚未完成)来创建和定义“经销商”用户角色的功能……

您可以使用该插件来自动执行所有操作。 https://wordpress.org/plugins/user-drop-down-roles-in-registration/

安装它,然后会有一个名为“添加用户角色”的菜单项

进入它,并将您希望客户能够选择的所有角色移到右侧:

在此处输入图片说明

按提交后,您的选择将被保存,您的注册页面将如下所示:

在此处输入图片说明

编辑:您是否需要在php中? 这样更容易...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM