public static function create_user()
 {
     if (empty($_POST['user_email'])) {
         wc_crm_add_notice(__('Please enter an e-mail address.', 'wc_crm'), 'error');
     } elseif (!is_email($_POST['user_email'])) {
         wc_crm_add_notice(__("The email address isn't correct.", 'wc_crm'), 'error');
     } elseif (email_exists($_POST['user_email'])) {
         wc_crm_add_notice(__("This email is already registered, please choose another one.", 'wc_crm'), 'error');
     }
     if (wc_crm_notice_count('error') > 0) {
         return;
     }
     global $wpdb;
     $nickname = str_replace(' ', '', ucfirst(strtolower($_POST['first_name']))) . str_replace(' ', '', ucfirst(strtolower($_POST['last_name'])));
     $username_opt = get_option('wc_crm_username_add_customer');
     switch ($username_opt) {
         case 2:
             $username = str_replace(' ', '', strtolower($_POST['first_name'])) . '-' . str_replace(' ', '', strtolower($_POST['last_name']));
             break;
         case 3:
             $username = $_POST['user_email'];
             break;
         default:
             $username = strtolower($nickname);
             break;
     }
     $username = _truncate_post_slug($username, 60);
     $check_sql = "SELECT user_login FROM {$wpdb->users} WHERE user_login = '******' LIMIT 1";
     $user_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $username));
     if ($user_name_check) {
         $suffix = 1;
         do {
             $alt_user_name = _truncate_post_slug($username, 60 - (strlen($suffix) + 1)) . "-{$suffix}";
             $user_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_user_name));
             $suffix++;
         } while ($user_name_check);
         $username = $alt_user_name;
     }
     add_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password');
     $user_id = wc_create_new_customer($_POST['user_email'], $username);
     remove_filter('pre_option_woocommerce_registration_generate_password', 'wcrm_enable_generate_password');
     do_action('wc_crm_create_customer', $user_id);
     if (!is_wp_error($user_id)) {
         update_user_meta($user_id, 'nickname', $nickname);
         wp_update_user(array('ID' => $user_id, 'role' => 'customer'));
         $customer_id = $wpdb->get_var("SELECT c_id FROM {$wpdb->prefix}wc_crm_customer_list WHERE user_id = {$user_id} ");
         if ($customer_id) {
             WC_CRM_Screen_Customers_Edit::save($customer_id, true);
         }
         wc_crm_add_notice(__("Customer created.", 'wc_crm'), 'success');
         wp_safe_redirect(admin_url() . 'admin.php?page=' . WC_CRM_TOKEN);
     } else {
         wc_crm_add_notice($user_id->get_error_message(), 'error');
     }
 }
    /**
     * Add group admin panel
     *
     * Shows the interface for adding new groups
     */
    public static function add_group()
    {
        if (wc_crm_notice_count('error') == 0) {
            $_POST = array();
        }
        // Grab the submitted data
        $group_name = isset($_POST['group_name']) ? (string) stripslashes($_POST['group_name']) : '';
        $group_slug = isset($_POST['group_slug']) ? wc_sanitize_taxonomy_name(stripslashes((string) $_POST['group_slug'])) : '';
        $group_type = isset($_POST['group_type']) ? (string) stripslashes($_POST['group_type']) : '';
        $group_total_spent_mark = isset($_POST['group_total_spent_mark']) ? (string) stripslashes($_POST['group_total_spent_mark']) : '';
        $group_total_spent = isset($_POST['group_total_spent']) ? (string) stripslashes($_POST['group_total_spent']) : '';
        $group_user_role = isset($_POST['group_user_role']) ? (string) stripslashes($_POST['group_user_role']) : '';
        $group_customer_status = isset($_POST['group_customer_status']) ? $_POST['group_customer_status'] : array();
        $group_product_categories = isset($_POST['group_product_categories']) ? $_POST['group_product_categories'] : array();
        $group_order_status = isset($_POST['group_order_status']) ? $_POST['group_order_status'] : array();
        $group_last_order = isset($_POST['group_last_order']) ? (string) stripslashes($_POST['group_last_order']) : '';
        $group_last_order_from = isset($_POST['group_last_order_from']) ? (string) stripslashes($_POST['group_last_order_from']) : '';
        $group_last_order_to = isset($_POST['group_last_order_to']) ? (string) stripslashes($_POST['group_last_order_to']) : '';
        ?>
		<div class="wrap woocommerce">
			<div class="icon32 icon32-groups" id="icon-woocommerce"><br/></div>
		    <h2><?php 
        _e('Customer Groups', 'woocommerce');
        ?>
</h2>
		    <?php 
        wc_crm_print_notices();
        ?>
		    <br class="clear" />
		    <div id="col-container">
		    	<div id="col-right">
		    		<div class="col-wrap">
			    		<form action="admin.php?page=wc_crm-groups" method="post">
			    			<?php 
        WC_CRM()->tables['groups']->prepare_items();
        WC_CRM()->tables['groups']->display();
        ?>
			    		</form>
		    		</div>
		    	</div>
		    	<div id="col-left">
		    		<div class="col-wrap">
		    			<div class="form-wrap">
		    				<h3><?php 
        _e('Add New group', 'wc_crm');
        ?>
</h3>
		    				<p><?php 
        _e('Groups are used to organise your customers. Please Note: you cannot rename a group later.', 'wc_crm');
        ?>
</p>
		    				<form action="admin.php?page=wc_crm-groups" method="post" style=" padding-bottom: 150px;">
								<div class="form-field">
									<label for="f_group_name"><?php 
        _e('Name', 'woocommerce');
        ?>
</label>
									<input name="group_name" id="f_group_name" type="text" value="<?php 
        echo $group_name;
        ?>
" />
									<p class="description"><?php 
        _e('Name for the group.', 'wc_crm');
        ?>
</p>
								</div>

								<div class="form-field">
									<label for="f_group_slug"><?php 
        _e('Slug', 'woocommerce');
        ?>
</label>
									<input name="group_slug" id="f_group_slug" type="text" value="<?php 
        echo $group_slug;
        ?>
" maxlength="28" />
									<p class="description"><?php 
        _e('Unique slug/reference for the group; must be shorter than 28 characters.', 'wc_crm');
        ?>
</p>
								</div>

								<div class="form-field">
									<label for="f_group_type"><?php 
        _e('Type', 'wc_crm');
        ?>
</label>
									<select name="group_type" id="f_group_type">
										<option value="dynamic" <?php 
        selected($group_type, 'dynamic');
        ?>
 ><?php 
        _e('Dynamic', 'wc_crm');
        ?>
</option>
										<option value="static" <?php 
        selected($group_type, 'static');
        ?>
 ><?php 
        _e('Static', 'wc_crm');
        ?>
</option>
										<?php 
        do_action('wc_crm_customer_group_types');
        ?>
									</select>
									<p class="description"><?php 
        _e('Determines how you select group for customers.', 'wc_crm');
        ?>
</p>
								</div>
								<div class="form-field dynamic_group_type">
									<label for="group_total_spent"><?php 
        _e('Total Spent', 'wc_crm');
        ?>
</label>
									<select name="group_total_spent_mark" id="group_total_spent_mark">
										<option value="equal" <?php 
        selected($group_total_spent_mark, 'equal');
        ?>
><?php 
        _e('=', 'wc_crm');
        ?>
</option>
										<option value="greater" <?php 
        selected($group_total_spent_mark, 'greater');
        ?>
><?php 
        _e('&gt;', 'wc_crm');
        ?>
</option>
										<option value="less" <?php 
        selected($group_total_spent_mark, 'less');
        ?>
><?php 
        _e('&lt;', 'wc_crm');
        ?>
</option>
										<option value="greater_or_equal" <?php 
        selected($group_total_spent_mark, 'greater_or_equal');
        ?>
><?php 
        _e('&ge;', 'wc_crm');
        ?>
</option>
										<option value="less_or_equal" <?php 
        selected($group_total_spent_mark, 'less_or_equal');
        ?>
><?php 
        _e('&le;', 'wc_crm');
        ?>
</option>
									</select>
									<input type="number" step="any" id="group_total_spent" name="group_total_spent" value="<?php 
        echo $group_total_spent;
        ?>
">
								</div>
								<div class="form-field dynamic_group_type">
									<label for="group_user_role"><?php 
        _e('User Role', 'wc_crm');
        ?>
</label>
									<select name="group_user_role" id="group_user_role"  class="wc-enhanced-select">
										<option value="any">
								            	<?php 
        _e('Any', 'wc_crm');
        ?>
							            </option>
													<option value="guest">
								            	<?php 
        _e('Guest', 'wc_crm');
        ?>
							            </option>
													<?php 
        global $wp_roles;
        foreach ($wp_roles->role_names as $role => $name) {
            ?>
								            <option value="<?php 
            echo strtolower($name);
            ?>
" <?php 
            selected($group_user_role, strtolower($name));
            ?>
>
								            	<?php 
            _e($name, 'wc_crm');
            ?>
								            </option>
								          <?php 
        }
        ?>
									</select>
								</div>
								<div class="form-field dynamic_group_type">
									<label for="group_customer_status"><?php 
        _e('Customer Status', 'wc_crm');
        ?>
</label>
									<select name="group_customer_status[]" id="group_customer_status" multiple="multiple" data-placeholder="<?php 
        _e('Choose a Customer Status...', 'wc_crm');
        ?>
" class="wc-enhanced-select">
										<?php 
        $statuses = wc_crm_get_statuses();
        foreach ($statuses as $status) {
            ?>
								            <option value="<?php 
            echo strtolower($status->status_slug);
            ?>
" <?php 
            echo in_array(strtolower($status->status_slug), $group_customer_status) ? 'selected="selected"' : '';
            ?>
>
								            	<?php 
            echo $status->status_name;
            ?>
								            </option>
								          <?php 
        }
        ?>
									</select>
								</div>
								<div class="form-field dynamic_group_type">
									<label for="group_product_categories"><?php 
        _e('Product Category', 'wc_crm');
        ?>
</label>
									<select name="group_product_categories[]" id="group_product_categories" multiple="multiple" data-placeholder="<?php 
        _e('Choose a Product Category...', 'wc_crm');
        ?>
" class="wc-enhanced-select">
										<?php 
        $all_cat = get_terms(array('product_cat'), array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false));
        if (!empty($all_cat)) {
            foreach ($all_cat as $cat) {
                ?>
							            	<option value="<?php 
                echo $cat->term_id;
                ?>
" <?php 
                echo in_array($cat->term_id, $group_product_categories) ? 'selected="selected"' : '';
                ?>
>
							            		<?php 
                echo $cat->name;
                ?>
							            	</option>
							          	<?php 
            }
        }
        ?>
									</select>
								</div>
								<div class="form-field dynamic_group_type">
									<label for="group_order_status"><?php 
        _e('Order Status', 'wc_crm');
        ?>
</label>
									<select name="group_order_status[]" id="group_order_status" multiple="multiple" data-placeholder="<?php 
        _e('Choose a Product Category...', 'wc_crm');
        ?>
" class="wc-enhanced-select">
										<?php 
        $wc_statuses = wc_get_order_statuses();
        if (!empty($wc_statuses)) {
            foreach ($wc_statuses as $key => $status_name) {
                ?>
					            	<option value="<?php 
                echo $key;
                ?>
" <?php 
                echo in_array($key, $group_order_status) ? 'selected="selected"' : '';
                ?>
>
					            		<?php 
                echo $status_name;
                ?>
					            	</option>
					          	<?php 
            }
        }
        ?>
									</select>
								</div>
								<div class="form-field dynamic_group_type">
									<label for="group_last_order"><?php 
        _e('Last Order', 'wc_crm');
        ?>
</label>
									<div class="wrap_date">
										<select name="group_last_order" id="group_last_order">
											<option value="between" <?php 
        selected($group_last_order, 'between');
        ?>
><?php 
        _e('Between', 'wc_crm');
        ?>
</option>
											<option value="before"  <?php 
        selected($group_last_order, 'before');
        ?>
><?php 
        _e('Before', 'wc_crm');
        ?>
</option>
											<option value="after"   <?php 
        selected($group_last_order, 'after');
        ?>
><?php 
        _e('After', 'wc_crm');
        ?>
</option>
										</select>
									</div>
									<div class="wrap_date">
										<input type="text" id="group_last_order_from" name="group_last_order_from" value="<?php 
        echo $group_last_order_from;
        ?>
">
										<i class="ico_calendar"></i>
									</div>
									<div class="wrap_date group_last_order_between" style="height: 30px; line-height: 30px; padding: 0 10px;">
										to
									</div>
									<div class="wrap_date group_last_order_between">
										<input type="text" id="group_last_order_to" name="group_last_order_to" value="<?php 
        echo $group_last_order_to;
        ?>
">
										<i class="ico_calendar"></i>
									</div>
									<div class="clear"></div>
								</div>

								<p class="submit"><input type="submit" name="wc_crm_add_new_group" id="submit" class="button" value="<?php 
        _e('Add group', 'wc_crm');
        ?>
"></p>
								<?php 
        wp_nonce_field('wc-crm-add-new-group');
        ?>
		    				</form>
		    			</div>
		    		</div>
		    	</div>
		    </div>
		    <script type="text/javascript">
			/* <![CDATA[ */

				jQuery('a.delete').click(function(){
		    		var answer = confirm ("<?php 
        _e('Are you sure you want to delete this group?', 'wc_crm');
        ?>
");
					if (answer) return true;
					return false;
		    	});

			/* ]]> */
			</script>
		</div>
		<?php 
    }
/**
 * Prints messages and errors which are stored in the session, then clears them.
 *
 * @since 2.1
 */
function wc_crm_print_notices()
{
    if (!did_action('woocommerce_init')) {
        _doing_it_wrong(__FUNCTION__, __('This function should not be called before woocommerce_init.', 'woocommerce'), '2.3');
        return;
    }
    $all_notices = wc_crm_get_notices();
    $notice_types = apply_filters('woocommerce_notice_types', array('error', 'success', 'notice'));
    foreach ($notice_types as $notice_type) {
        if (wc_crm_notice_count($notice_type) > 0) {
            foreach ($all_notices[$notice_type] as $message) {
                wc_crm_print_notice($message, $notice_type);
            }
        }
    }
    wc_crm_clear_notices();
}