5 replies [Last post]
chrispeat's picture
Offline
Joined: 10/23/2009
Juice: 20
Was this information Helpful?

Dear all,

I have a live site running and wondered who all the new users were. I since realised that they were ubercart customers so would like to know the following:

When an anonymous user completes a purchase can they be automatically assigned a role other than 'authenticated user'?
Where in the settings is this? (I've looked everywhere!)
Do I have to do this with Auto Assign?
Do I have to do this on every product? (undesirable due to the number of products)

If I could find a site wide setting I'd be very happy, and think that it seems reasonable but cant find anything in the config / docs / forums?

Many many thanks,

Chris

splash112@drupal.org's picture
Offline
Joined: 04/01/2008
Juice: 413
Yes, Did you enable the

Yes,

Did you enable the "Roles" module for Ubercart?
Also check here:

http://www.ubercart.org/docs/user/3366/selling_site_access_role_promotions

chrispeat's picture
Offline
Joined: 10/23/2009
Juice: 20
Yes, it's installed.. any other ideas?

Thanks for the heads up. I have already got the roles module installed, and I think I can use this on a product-by-product basis but... we have lots of products and it would be much more efficient to be able to allocate a role to all customers, rather than the authenticated role that ubercart issues new users too (although it might be drupal that does that?).

I dont seem to be able to do this through conditional actions which would be a natural place really...

Thanks for the help, its hugely appreciated!

Any further / other ideas?

splash112@drupal.org's picture
Offline
Joined: 04/01/2008
Juice: 413
Re: Yes, it's installed.. any other ideas?

It's kind of old, but this might help you:

<?php
$myuser
= module_invoke('user', 'load', $user_uid);
$myuserroles = $myuser->roles[];
$myuserroles[]  = 'newrole';
module_invoke('user', 'save', $myuser, array('roles' => $myuserroles));
?>

from http://drupal.org/node/28379#comment-48796

If you could use this (or similar code) at CA "Customer completes checkout", it might just do the trick.

webmaker's picture
Offline
Joined: 04/22/2009
Juice: 76
Re: Re: Yes, it's installed.. any other ideas?

First I get an error regarding to line 2... it seems that it has to be $myuserroles = $myuser->roles;

But even when I changed this the following error appears:

user warning: Duplicate entry '' for key 2 query: INSERT INTO users (created) VALUES (1296661808) in /var/www/vhosts/beocos.de/httpdocs/modules/user/user.module on line 329.
cj-a-min's picture
Offline
Joined: 11/29/2011
Juice: 9
Its a Big Secret

No one tells you that

$uid = $user->uid;

Needs to be in the form of UC tokens, which is

$uid = [order-uid];

Explanation:
The first code tries to insert uid 0, into users_roles table, which of course already exist (authenticated user), which means you'll get sql duplicate errors and the code will not work. The later code will get the newly created uid and thus insert this into users_roles, which will work.

The final code is like this for UC 6.x-2.4.

<?php
  $role_name
= 'yourcustomrole';
 
$rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", $role_name));
 
$uid = [order-uid];
 
db_query("INSERT INTO {users_roles} (uid, rid) VALUES ('%d','%d')", $uid, $rid);
 
watchdog('user', 'Added user to yourcustomrole role');
?>

This code can be placed in admin/store/settings/checkout/edit/messages in Checkout completion for new users:

Not one person has touched on this since 2009 and its going on 2012, see:

http://www.ubercart.org/issue/13192/new_user_creation_checkout_and_autom...
http://www.ubercart.org/comment/54046
http://stackoverflow.com/questions/3533415/how-to-assign-role-with-php-i...
http://stackoverflow.com/questions/3532597/how-can-i-grant-a-specific-ro...
http://drupal.org/node/1050202
http://www.ubercart.org/forum/support/11420/why_ubercart_let_roles_array...

So yes its a big secret. Hopefully this helps everyone looking for an answer.