4

I need to assign a kind of "premium status" to members that purchase an amount equal or greater than 100 € via PHP.

Conditional Actions are already set up (user = anonymous/authenticated AND total amount = equal/greater than 100 AND user =! premium) but I'm missing the PHP part to actually say "then grant him the premium membership".

How can I achieve this?

EDIT: is the below code correct?

if ($account) {
  $uid = $account->uid;
  $role_name = 'authenticated user';
  $rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", $role_name));
  db_query("INSERT INTO {users_roles} (uid, rid) VALUES(%d, %d)", $uid, $rid);
  watchdog('user', 'uc ca added role to Ubercart created user');
}
apaderno
  • 28,547
  • 16
  • 75
  • 90
dag729
  • 199
  • 4
  • 10

4 Answers4

6

You can do this with user_load() and user_save():

$uid = 1; // UID of user to add role to
$role_name = 'test role'; // Name of role to add

// Get RID of role
$rid = db_result(db_query("SELECT r.rid FROM {role} r WHERE r.name = '%s'", $role_name));

// Load user object
$account = user_load(array('uid' => 1));

// Save the user object with the new roles.
if ($account !== FALSE && !isset($account->roles[$rid])) {
  $roles = $account->roles + array($rid => $role_name);
  user_save($account, array('roles' => $roles));
}

If you wanted to do this in bulk for multiple users, there's user_multiple_role_edit():

$uids = array(1, 2, 3, 4); // UIDs of users to add role to
$role_name = 'test role'; // Name of role to add

// Get RID of role
$rid = db_result(db_query("SELECT r.rid FROM {role} r WHERE r.name = '%s'", $role_name));

// Add the role to UIDs
user_multiple_role_edit($uids, 'add_role', $rid);

Edit

If you wanted to do this for the current user (like part of the check you mentioned in a comment), you can do:

// Check for value over 100.00
if ($total_value > 100) {
  global $user; // Retrieve user object for currently logged in user.

  $role_name = 'test role'; // Name of role to add

  // Get RID of role
  $rid = db_result(db_query("SELECT r.rid FROM {role} r WHERE r.name = '%s'", $role_name));

  // Save the user object with the new role.
  if (!isset($user->roles[$rid])) {
    $roles = $user->roles + array($rid => $role_name);
    user_save($user, array('roles' => $roles));
  }
}
// Other code here.
  • I want the role to change on the event of a total purchase equal/greather than 100€ but, if I understand your code, that's a static solution (I'll have to use it everytime I want to change roles by hand). – dag729 Aug 20 '10 at 19:18
  • 1
    No, just put this code in wherever you're checking the total purchase. (i.e. `if ($purchase > 100) { // insert code above here }`, see also edit)This is permanent, though: the user will always have this role once granted unless you revoke it later on. The other solution is temporary, and will not last between pages. –  Aug 20 '10 at 19:24
  • 1
    Slightly unfortunate acronim they use, it took me a while to figure out that your comment Get RID of role didnt mean delete the role :) – Toby Allen Dec 01 '11 at 20:17
3

Just though I'd update the snippet for Drupal 7. Otherwise it works fine :)

$rid = db_query("SELECT r.rid FROM {role} r WHERE r.name = :rname", 
                array(':rname' => $role_name))->fetchField();
rix
  • 10,104
  • 14
  • 65
  • 92
2

I'd rather use user_role_load_by_name function.

Bulat
  • 6,869
  • 1
  • 29
  • 52
0

for drupal 7:

     global $user; // Retrieve user object for currently logged in user.

  $role_name = 'test role'; // Name of role to add

  // Get RID of role
  $rid = db_query("SELECT r.rid FROM {role} r WHERE r.name = :rname", array(':rname' => $role_name))->fetchField();

  // Save the user object with the new role.
  if (!isset($user->roles[$rid])) {
    $roles = $user->roles + array($rid => $role_name);
    user_save($user, array('roles' => $roles));
  }

thanks rix.

PersianMan
  • 2,779
  • 2
  • 17
  • 20