Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 电子商务注册上的用户角色选择已停止工作_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 电子商务注册上的用户角色选择已停止工作

Php 电子商务注册上的用户角色选择已停止工作,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我使用下拉菜单在我的Woocommerce注册表上的两个用户角色之间进行选择。更新到Woocommerce 3.0.8后,下拉菜单停止工作,我不知道为什么。下面是我一直在使用的代码。有什么想法吗 // Add two new roles. add_role('dealer', 'Dealer', array( 'delete_posts' => false, 'delete_published_posts' => false, 'edit_posts' => false, '

我使用下拉菜单在我的Woocommerce注册表上的两个用户角色之间进行选择。更新到Woocommerce 3.0.8后,下拉菜单停止工作,我不知道为什么。下面是我一直在使用的代码。有什么想法吗

// Add two new roles.
add_role('dealer', 'Dealer', array( 
'delete_posts' => false,
'delete_published_posts' => false,
'edit_posts' => false,
'edit_published_posts' => false,
'publish_posts' => false,
'read' => true,
'upload_files' => true,
'edit_users' => false
));
add_role('distributor', 'Distributor', array(
'delete_posts' => false,
'delete_published_posts' => false,
'edit_posts' => false,
'edit_published_posts' => false,
'publish_posts' => false,
'read' => true,
'upload_files' => true,
'edit_users' => false
));
add_action('register_form','role_registration_form');
function role_registration_form(){
$wp_roles = new WP_Roles();
$wp_roles->use_db = true;
$role_names = $wp_roles->get_names();

foreach( $role_names as $role_name ) {
    // Ensure that the options exclude default Wordpress roles
    if ( ($role_name !== 'Administrator') and ($role_name !== 'Editor') and ($role_name !== 'Author') and ($role_name !== 'Contributor' ) and ($role_name !== 'Subscriber') and ($role_name !== 'Customer') and ($role_name !== 'Shop Manager')) {
        //  Role value below needs to be in lowercase only
        $role_option .= "<option value='".strtolower($role_name)."'>";
        $role_option .= $role_name;
        $role_option .= "</option>";
    }
}
$html = '
<style type="text/css">
        #role {
        background:#FBFBFB none repeat scroll 0 0;
        border:1px solid #E5E5E5;
        font-size:15px;
                    color:#3a3a3a;
        margin-bottom:16px;
        margin-right:6px;
        margin-top:2px;
        padding:3px;
        width:35%;
    }
</style>

<div width="100%">
    <p>
        <label style="display: block; margin-bottom: 5px;">' . __('Are you a Dealer or Distributor?', 'Role') . '
            <select id="role" name="role" class="input">
            ' . $role_option . '
            </select>
        </label>
    </p>
</div>
';
echo $html;
}
add_action('user_register', 'register_role');
function register_role($user_id, $password="", $meta=array()) {
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['role'] = $_POST['role'];
// allow if a role is selected
if ( $userdata['role'] ){
  wp_update_user($userdata);
}
}
add_action( 'show_user_profile', 'role_selection_field' );
add_action( 'edit_user_profile', 'role_selection_field' );
function role_selection_field( $user ) {
$wp_roles = new WP_Roles();
$wp_roles->use_db = true;
$role_names = $wp_roles->get_names();
foreach( $role_names as $role_name ) {
    if ( ($role_name !== 'Administrator') and ($role_name !== 'Editor') and ($role_name !== 'Author') and ($role_name !== 'Contributor' ) and ($role_name !== 'Subscriber') and ($role_name !== 'Customer') and ($role_name !== 'Shop Manager')) {
        if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
            foreach ( $user->roles as $role ) {         
                if ( strtolower($role_name) == $role ) {
                    $role_option .= "<option value='".strtolower($role_name)."' selected='selected'>";
                    $currentrole = strtolower($role_name);
                } else {
                    $role_option .= "<option value='".strtolower($role_name)."'>";
                }

                $role_option .= $role_name;
                $role_option .= "</option>";
            }
        }
    }
}
?>

<?php }

add_action( 'personal_options_update', 'save_role_selection_field' );
add_action( 'edit_user_profile_update', 'save_role_selection_field' );
function save_role_selection_field( $user_id ) {
//if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'role', $_POST['role'] );

$user = new WP_User( $user_id );
// Remove role
$current_user_role = get_current_user_role();

$user->remove_role( $current_user_role );
// Add role
$user->add_role( $_POST['role'] );
}
function get_current_user_role () {
global $current_user;
get_currentuserinfo();
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
};

?>
//添加两个新角色。
添加_角色('经销商','经销商',数组(
“删除帖子”=>false,
“删除发布的帖子”=>false,
“编辑帖子”=>false,
“编辑发布的帖子”=>false,
“发布帖子”=>false,
“读取”=>正确,
“上载_文件”=>true,
“编辑用户”=>false
));
添加_角色('distributor','distributor',数组(
“删除帖子”=>false,
“删除发布的帖子”=>false,
“编辑帖子”=>false,
“编辑发布的帖子”=>false,
“发布帖子”=>false,
“读取”=>正确,
“上载_文件”=>true,
“编辑用户”=>false
));
添加行动(“登记表”、“角色登记表”);
职能角色登记表(){
$wp_roles=新wp_roles();
$wp\u roles->use\u db=true;
$role_names=$wp_roles->get_names();
foreach($role\u name作为$role\u name){
//确保选项排除默认Wordpress角色
如果($role\u name!=“Administrator”)和($role\u name!=“Editor”)和($role\u name!=“Author”)和($role\u name!=“Contributor”)和($role\u name!=“Subscriber”)和($role\u name!=“Customer”)和($role\u name!=“Shop Manager”)){
//下面的角色值只需小写
$role_选项=“”;
$role\u option.=$role\u name;
$role_选项=“”;
}
}
$html='1
#角色{
背景:#FBFB无重复滚动0;
边框:1px实心#e5;
字体大小:15px;
颜色:#3A3A;
边缘底部:16px;
右边距:6px;
边缘顶部:2倍;
填充:3倍;
宽度:35%;
}

“.”(“您是经销商还是分销商?”,“角色”)。”
“.$role_选项。”

'; echo$html; } 添加操作(“用户注册”、“注册角色”); 函数register\u role($user\u id,$password=“”,$meta=array()){ $userdata=array(); $userdata['ID']=$user\u ID; $userdata['role']=$\u POST['role']; //如果选择了角色,则允许 if($userdata['role'])){ wp_update_user($userdata); } } 添加操作('show_user_profile'、'role_selection_field'); 添加操作(“编辑用户配置文件”、“角色选择”字段); 功能角色\选择\字段($user){ $wp_roles=新wp_roles(); $wp\u roles->use\u db=true; $role_names=$wp_roles->get_names(); foreach($role\u name作为$role\u name){ 如果($role\u name!=“Administrator”)和($role\u name!=“Editor”)和($role\u name!=“Author”)和($role\u name!=“Contributor”)和($role\u name!=“Subscriber”)和($role\u name!=“Customer”)和($role\u name!=“Shop Manager”)){ 如果(!empty($user->roles)&&is_数组($user->roles)){ foreach($user->roles as$role){ if(strtolower($role\u name)==$role){ $role_选项=“”; $currentrole=strtolower($role\u name); }否则{ $role_选项=“”; } $role\u option.=$role\u name; $role_选项=“”; } } } } ?>
我决定废弃旧代码,用下面的代码替换它,现在就可以使用了

/* 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( 'Dealer or Distributor', 'woocommerce' ); ?></label>
    <select class="input-text" name="role" id="reg_role"> 
    <option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'dealer') esc_attr_e( 'selected' ); ?> value="dealer">Dealer</option> 
    <option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'distributor') esc_attr_e( 'selected' ); ?> value="distributor">Distributor</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', __('Dealer or Distributor is 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');
/*添加商业登记表自定义字段*/
函数WC\u extra\u registration\u fields(){?>

value=“分销商”>分销商


在我的例子中,这个保存行不能改变角色,所以我做了类似的事情,我改变了结尾:

function WC_extra_registation_fields() {?>
<p class="form-row form-row-first">
    <label for="reg_role"><?php _e( 'Dealer or Distributor', 'woocommerce' ); ?></label>
    <select class="input-text" name="role" id="reg_role"> 
    <option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'dealer') esc_attr_e( 'selected' ); ?> value="dealer">Dealer</option> 
    <option <?php if ( ! empty( $_POST['role'] ) && $_POST['role'] == 'distributor') esc_attr_e( 'selected' ); ?> value="distributor">Distributor</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', __('Dealer or Distributor is 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'])) {
    $user = get_user_by('id', $customer_id);
    $user->remove_role( 'customer' );
    $user->add_role( $_POST['billing_role'] );
}

}

add_action('woocommerce_created_customer', 'WC_save_registration_form_fields');
函数WC\u extra\u registration\u fields(){?>

value=“分销商”>分销商


第一件事:你的注册表单模板具体在哪里?它在什么目录中?表单是我帐户页面上的默认注册表单。不是什么URL,什么目录。它在WooCommerce插件文件夹中吗?你的主题文件夹?目录。它是WooCommerce插件文件夹。它停止工作的原因可能是o更新woocommerce时,woocommerce插件文件夹中的模板会被覆盖。为了在更新时使此更改“持续”,您需要使用woocommerce模板覆盖系统: