2017-06-17 43 views
0

我使用下拉菜單在我的Woocommerce註冊表單上選擇兩個用戶角色。更新到Woocommerce 3.0.8後,下拉菜單停止工作,我無法弄清楚爲什麼。以下是我一直在使用的代碼。有任何想法嗎?用戶角色選擇Woocommerce註冊已停止工作

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

?> 
+0

首先要說的是:您的註冊表格模板在哪裏?它在哪個目錄? –

+0

該表單是「我的帳戶」頁面上的默認註冊表單。 – Kevin

+0

不是什麼URL,什麼*目錄*。它在WooCommerce插件文件夾中嗎?你的主題文件夾?目錄。 –

回答

0

我決定取消舊的代碼,並用下面的代碼替換它,現在它可以工作。

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