Drupal 6.26 with Ubercart 6.x-2.9 UC Conditional actions
Ubercart customers are automatically assigned the "authenticated user" role upon making a purchase. Of course, this is a default role and cannot be changed.
The goal: We want to add a new "customer" role to these customers automatically. This way they will appear as "customers" in the users list. It comes in handy when checking who is a customer and who is not. It turns out that there is no easy check mark or select box to achieve this.
The module UC Roles which may appear to be an alternative has somewhat different functions and is briefly described after Conditional actions.
UC Conditional actions
This is already part of Ubercart core. After a bit of research and testing the Ubercart core "Roles" module it is now clear that UC Conditional actions can do this specific job alone.
General info on Conditional actions, but not really actionable:
http://www.ubercart.org/docs/user/7657/configuring_conditional_actions
Using Conditional Actions to assign a user role at checkout, actionable:
http://www.ubercart.org/comment/54046 <= with thanks to the author
That looks complicated and it put me off at first, looking for other solutions. But after spending a long time looking and testing, I finally went back to this, and it did finally work. Note again, this is for assigning every new user who has just purchased an item an additional role like "customer".
I created the role first and manually assigned earlier customers to it. For new customers:
Adding the customer role automatically
This involves php insertion and it is potentially risky. First make a database backup to revert to previous state, if that becomes necessary. I experimented and tried this on a test installation - where I was able to make a few greenhorn mistakes - before implementing this on the live site (recommended). If anything goes wrong "it wasn't me".
Go to Store admin > Conditional actions > Overview. It makes sense to study how "predicates" are created, what "conditions" look like and what the "actions" look like. Don't change anything, unless you know what you are doing.
Be warned that the "Save changes" button for the conditions may lead you to believe that some changes are not permanent until you click that button. Yet I found that some types of changes appear to stick completely on their own (without Saving changes).
1. Make a new "predicate" (hm, predicate?). Give the predicate a name similar to "Assign 'customer' role when a purchase is made". Select a trigger, I chose: Customer completes checkout. Alternatively the trigger could be: Payment gets entered for an order. Then give it a class name, for example "role". Enable it, Save.
2. Go to conditions. You are inside a conditions group with operand marked AND. That's ok because there will be only one single condition. Create that one single condition: In the drop-down box of available conditions choose under Order User: Check the role of the user. Then follow the instructions in the original post's attached image file to a T. The Negate condition is enabled, the operator is OR, and mark all roles that are not anonymous and authenticated: finally meaning, do not do this for any of the marked roles. Save changes.
3. Go to actions. Add an action. Choose Custom PHP. In the php entry box enter the following. It is modified from the OP mentioned above, I removed the php start and end tags according to Drupal instructions and removed the beginning '>' which I could not place (while I'm not a programmer):
if($account) {
$uid = $account->uid;
$role_name = 'customer';
$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 authenticated user');
}
Two things you can/must change here, first is the name of the 'customer' role for $role_name. Change it for any other role name that you are using for this (make the role first). Second is the 'uc ca added role to Ubercart created authenticated user' for the watchdog entry at the bottom. Change it to anything you want. Make sure there are no unwanted changes and that the punctuations remain intact. Save changes.
So, that's basically it.
Of course, you'll want to test it. If you are using this for Ubercart, you'll need to go through a complete purchase process. You can bypass your standard payment methods by temporarily enabling the Ubercart optional Payment methods pack - it contains an "other" payment method that appears in checkout. Just don't forget to disable/uninstall that module after testing.
For this specific purpose, it is not needed to enable the UC Roles core (optional) module. But I had researched and tested it anyway:
UC Roles
Ubercart's optional core module "roles" can change customer's roles upon purchase of specific, individual products/services. If needed, membership durations can be attached to this. This is very useful if that is what you want to do. The instructions are detailed and step-by-step.
http://www.ubercart.org/docs/user/3366/selling_site_access_role_promotions
But watch out. When enabling the "roles" module, the first thing it does is enable email notifications to users when a role change occurs. You might not want to have that enabled immediately. If the site is live, you better check this: Store admin > Conditional Actions > Overview > by class. Also, if you have disabled these notifications and and then uninstall the module, they will stick in the CA overview. Clicking reset will get rid of them for good.
Also, in the user profile the Roles module adds a selection box and entry for duration of validity - I assume for admin action only.
This module does not directly address my need for globally adding a role based on any purchase. Actually it could work, but I'd have to go into every product and set it there. That would be overdone and quite cumbersome. Plus there would be unnecessary active features that could lead to mistakes in administering the shop.
Related
To identify Ubercart users by role or some other flag (not actionable for me)
http://www.ubercart.org/comment/49744
How to Create Memberships with Drupal Ubercart (detailed)
http://www.leveltendesign.com/blog/rachel/how-create-memberships-drupal-...