User Groups? how do i set which group is default?

So i have User Groups sorta figured out, but how do i set it so that new customer sign ups are assigned to a default user group?



right now i have have Customer group, and a Wholesale group. How do i set it so that customers who sign up to the site and sent to the Customer Group automatically… and I can worry about manually assigning users to the Wholesale group?

At this moment it isnt possible, the code must be changed for that… I know it makes no sense… maybe i can send a code for that here… but i cant promised this.

I’m currently battling with this as well. All of our pricing and shipping options are group-dependent. I had cs support working on it, but they were unsuccessful, and are referring me to the dev team.

i have the same problem

i need users to be automatically assigned to “customers” user group.



at that point if they want a wholesale account they need to request it, and I can review there info and get back to them



i just asked the support team about this, I have 2 user groups also, customers & Pros.



I will post with their reply, hopefully tomorrow.



hopefully they will give me some code to apply.



Ill let you know,



Picturepicasso.com

I haven’t tested this mod myself, but it looks like it will auto-assign a specific group upon account registration: [url]http://forum.cs-cart.com/showpost.php?p=72998&postcount=13[/url]



Glen

well, Cscart finally got back to me.

Apparently it will cost me $225 to have users that register get assigned to my “Customer” user group when the initially register.



So i went for it.



I have reg customer pricing & professional photographer pricing for the same products.



I set my products for customer pricing to be viewed by guests & customers who sign up

and I cloned the same products only to be viewed by my professional users with the discount automatically applied.



The problem occurred when a user registered they could not see any products until I assigned them to the user group “customers”



If I assigned my customer products to be view by “registered users” then the pros would see 2 sets of the same product with different pricing.



i hope anyone reading this follows what i am talking about, lol :stuck_out_tongue:

[quote]

Apparently it will cost me $225 to have users that register get assigned to my “Customer” user group when the initially register.

[/quote]

Wow, a lot for a this mod… Should be a single addons/my_changes/controllers/customer/profiles.post.php file…



Create addons/my_changes/controllers/customer/profiles.post.php with this content.

```php


if( !defined('AREA') ) die('Access denied');

// Check if user registration fits the parameters below.

$add_group_group_name = "VIP"; // The real name of the usergroup

// Only on an update where not already a user.
if($_SERVER['REQUEST_METHOD'] == 'POST' && $mode == "add" ) {

// Check for valid 'group name'
$gid = add_group_getgroup($add_group_group_name);
if( $gid ) { // Found the group
// Assumes $user_data is already set.
$_user_data = empty($_REQUEST['user_data']) ? array() : $_REQUEST['user_data'];
$user_id = add_group_userid($_user_data);
if(empty($_user_data['usergroups']) || !in_array($gid, $_user_data['usergroups']) ) {
$user_data['usergroups'][] = $gid;
}
add_group_addgroup($user_id, $gid);
$_auth = empty($_SESSION['auth']) ? array() : $_SESSION['auth'];
if( isset($_auth['usergroup_ids']) && !in_array($gid, $_auth['usergroup_ids']) ) {
$_auth['usergroup_ids'][] = $gid;
$_SESSION['auth'] = $_auth;
}
} // if gid
} // if POST and add
return array(CONTROLLER_STATUS_OK);

function add_group_get_user_data($uid) {
return db_get_row("SELECT * FROM ?:users WHERE user_id=?i", $uid);
}

function add_group_userid($user_data) {
return db_get_field("SELECT user_id FROM ?:users WHERE (email = ?s ?p)", $user_data['email'], (empty($user_data['user_login']) ? '' : db_quote(" OR user_login = ?s", $user_data['user_login'])) );
}

function add_group_getgroup($name) {
return db_get_field("SELECT usergroup_id FROM ?:usergroup_descriptions WHERE usergroup = ?s", $name);
}

function add_group_addgroup($uid, $gid) {
$ar = array(
'user_id' => $uid,
'usergroup_id' => $gid,
'status' => 'A'
);
db_query("INSERT INTO ?:usergroup_links ?e", $ar);
}
?>

```



Note that this is used for a profiles.add only. It will not change anything on an update. But it will assign the user to the group named in the variable $add_group_group_name



Note this was cut/pasted from other work I did previously for someone so it has not been really tested as-is. But it should definitely give you a good starting point for doing this simple chore.

[quote name=‘tbirnseth’]Wow, a lot for a this mod… Should be a single addons/my_changes/controllers/customer/profiles.post.php file…



Create addons/my_changes/controllers/customer/profiles.post.php with this content.

```php


if( !defined('AREA') ) die('Access denied');

// Check if user registration fits the parameters below.

$add_group_group_name = "VIP"; // The real name of the usergroup

// Only on an update where not already a user.
if($_SERVER['REQUEST_METHOD'] == 'POST' && $mode == "add" ) {

// Check for valid 'group name'
$gid = add_group_getgroup($add_group_group_name);
if( $gid ) { // Found the group
// Assumes $user_data is already set.
$_user_data = empty($_REQUEST['user_data']) ? array() : $_REQUEST['user_data'];
$user_id = add_group_userid($_user_data);
if(empty($_user_data['usergroups']) || !in_array($gid, $_user_data['usergroups']) ) {
$user_data['usergroups'][] = $gid;
}
add_group_addgroup($user_id, $gid);
$_auth = empty($_SESSION['auth']) ? array() : $_SESSION['auth'];
if( isset($_auth['usergroup_ids']) && !in_array($gid, $_auth['usergroup_ids']) ) {
$_auth['usergroup_ids'][] = $gid;
$_SESSION['auth'] = $_auth;
}
} // if gid
} // if POST and add
return array(CONTROLLER_STATUS_OK);

function add_group_get_user_data($uid) {
return db_get_row("SELECT * FROM ?:users WHERE user_id=?i", $uid);
}

function add_group_userid($user_data) {
return db_get_field("SELECT user_id FROM ?:users WHERE (email = ?s ?p)", $user_data['email'], (empty($user_data['user_login']) ? '' : db_quote(" OR user_login = ?s", $user_data['user_login'])) );
}

function add_group_getgroup($name) {
return db_get_field("SELECT usergroup_id FROM ?:usergroup_descriptions WHERE usergroup = ?s", $name);
}

function add_group_addgroup($uid, $gid) {
$ar = array(
'user_id' => $uid,
'usergroup_id' => $gid,
'status' => 'A'
);
db_query("INSERT INTO ?:usergroup_links ?e", $ar);
}
?>

```



Note that this is used for a profiles.add only. It will not change anything on an update. But it will assign the user to the group named in the variable $add_group_group_name



Note this was cut/pasted from other work I did previously for someone so it has not been really tested as-is. But it should definitely give you a good starting point for doing this simple chore.[/QUOTE]



I use the above code with the following result:



I have two user groups for registered user, namely

  1. Customer
  2. Distributor



    For newly registered user, the “Customer” group is set to “Active” and the “Distributor” group is set to “Available”



    My questions are
  3. what is the different between “Active” and “Available”
  4. How to change the “Distributor” group from “Available” to “Declined”



    Thanks in advance for the help.

As I said, I lifted this out of other work I did. I don’t have time to go back through it and modify it to suit your needs. Suggest you find a programmer who can understand what it’s doing and contract with them to have them adjust it to suit.

[quote name=‘tbirnseth’]As I said, I lifted this out of other work I did. I don’t have time to go back through it and modify it to suit your needs. Suggest you find a programmer who can understand what it’s doing and contract with them to have them adjust it to suit.[/QUOTE]



No problem.

If your schedule permit, I would like to engage your service. Please PM me if you are able to help.



Thanks.

The code edit:



The file is core/fn.users.php



Make a backup of your current file, I renamed the original to fn.users.php_org



Find the line: it’s around line 1239



Code:

$auth = fn_fill_auth($user_data);

}

}



Overwrite the above code with the following between the # dont copy #



###########################################



$auth = fn_fill_auth($user_data);

}

}



// New code to add user to a default group

db_query(“INSERT INTO ?:usergroup_links (status, user_id, usergroup_id) VALUES (‘A’, $user_id, ‘8’)”);

$auth = fn_fill_auth($user_data);



########################################





Setting the default user group:



In the above ‘8’ will be the default user group, you will need to look at your database under usergroups or just try the required number until the correct user group is set when creating a user.



Works great and easy to do even for novices.

I am using CS-Cart 2.14, and thought I would share how I solved this.



I inititally tried the code posted by tbirnseth. Because it makes use of the “my changes” add-on, it does not require editing the core code and thereby causing problems within installing upgrades in the future.



However, there were a few problems with that code. First, it only fires when someone creates a profile using the “register” button on the site, and not when someone registers during checkout. I solved that problem by creating a new file named addons/my_changes/controllers/customer/checkout.post.php, and modifying it to handle registrations during checkout. That worked to an extent, but then I ran into a big problem…



The two “my_changes” scripts do not have any way of checking whether the user profile was successfully created. They just blindly add the given email address to the given user group. This throws a “duplicate key” database error up on the screen when someone tries to register an email address which already exists in the customer list. There may be other situations in which it fails with an error. Not acceptable.



officeit’s change did not work for me, either, but it put me on the right track toward a solution.



What I ultimately did was to edit core/fn_users.php. Around line #1130, I found this text…



$user_id = db_query(“INSERT INTO ?:users ?e” , $user_data);



…and added this right below it:



db_query( “INSERT INTO ?:usergroup_links( user_id, usergroup_id, status ) VALUES( $user_id, 5, ‘A’ )” );



NOTE #1: replace the number 5 with the id number of the group to which new users should be automatically assigned.



NOTE #2: Whenever you apply upgrades using the Upgrade Center admin screen, the core/fn_users.php will be flagged as having local modifications. Thus, every time you upgrade, you will have to find the right location within the file, and add the above line back in. That’s a pain in the ass, but at least it’s only one line.



NOTE #3: I have not tested this in combination with a CSV import of customer records, but I’m guessing they probably don’t play well together. I would probably not use the two in combination, if I were you.

If there is a user_id value passed in the REQUEST then the user exists. If not, then it needs to be created. My change does just work with the profiles.add controller/mode I would have to check what happens during checkout, but I’m sure it will proably post to the profiles controller but may do so as an ajax request. Make sure you properly distinguish between POST and GET request methods.

Try to register using an email address that already exists - it will throw a db error to the screen.

Suggest yu refine as needed then… This was intended to be conceptual code, not something I’m supporting. You can always add a SELECT before the INSERT and use an UPDATE if the SELECT returns any data.