8 replies [Last post]
emilyfix's picture
Offline
Joined: 07/08/2008
Juice: 10
Was this information Helpful?

I have a website that has a course enrolment feature that has been provided by Ubercart. If one person signs up (purchases) multiple people to attend the course, I was wondering if it was possible to automate the ability to assign roles/accounts for each individual who plans to attend. The person who makes the purchase has to be logged in or registered in order to purchase, and their role is assigned automatically thanks to the uc_roles, but I need to assign multiple roles if multiple quantities of the product are purchased. Is this possible?

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4110
Re: Using Ubercart to register multiple users

You can easily do this with a custom module... you'd need to hook into hook_order(), and then find the qty of each of the uc_cart_get_contents() array. So ..

<?php
$global $user
;
$items = uc_cart_get_contents();
foreach (
$items AS $item) {
  if (
$item->qty == 3 && $item->model=='Special SKU') {
   
// Assign this user a new role, since they purchased 3 of this Special item.
   
user_multiple_role_edit(array($user->uid), 'add_role', 5);// The '5' needs to be the rid of your role.
    // Clear the cache to update their permissions
   
cache_clear_all($user->uid, 'cache_menu', TRUE);
  }
}
?>

This would go inside a hook_order, like..

<?php
/**
* Implement hook_order()
*/
function MYMODULE_order($op, $order, $status) {
  switch(
$op) {
   case
'update':
     if (
$status == 'Complete') {
      
/*
        *  Add the above function here...
        */
    
}
     break;
  }
}
?>

I use this same kind of code for a registration form, that is part of a custom module I've written, so it is tested and works. Hope it gets you started.

--
Help directly fund development: Donate via PayPal!

emilyfix's picture
Offline
Joined: 07/08/2008
Juice: 10
Ok...

From what I can tell, the PHP you have provided (thank you!) will change the roll of the individual purchasing the courses, but doesn't mention anything about signing up the other 2 who will be attending the course. So although this guy has purchased 3 courses, he will only be attending one along with two other people. Is it maybe easier to create email text field(s) in a checkout pane according to the quantity of courses purchased (qty = 3, fields = 2, etc.)? Am I explaining myself well here?

Thanks again Smiling

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4110
Re: Ok...

Ah yes, I forgot that part. Well the thing is you'd have to find a way to get their username and user id from the system. It seems like any way you slice it, there will still be the hand of an administrator taking a look and adding the additional names manually - unless you can figure out a way for the person registering to get those other values in there.

If you did have the uid of the other 2 people, you can simple duplicate the role function I called earlier, and substitute $user->uid (the current user's uid) with a single number that is the user id of one of the other people. (One uid per function).

hope that helps...

EDIT: I think a solution might be as you suggested, adding some email fields to the form. When User A submits it, the emails are sent to Persons B and C (using their email addresses from the form fields) asking them to confirm the registration. You'd then need to have a "secondary" form of sorts, that just takes the information submitted by User A, and asks them to confirm it. (Or you could have the module search for users whose email addresses they belong to, and load the $user object that way). (They'd need to be logged in, so that their user ids are passed into the system, and the role is added to their account.) To me that sounds like a fairly viable option. Smiling

--
Help directly fund development: Donate via PayPal!

Daniorama's picture
Offline
Joined: 09/15/2008
Juice: 46
Re: Re: Ok...

Hello, that seems a good solution, but how could I make that if some or none of the users (maybe even the buyer) is not yet registered on the web? Could you write the code regarding your suggestions? Thank you!

Daniorama's picture
Offline
Joined: 09/15/2008
Juice: 46
Re: Re: Re: Ok...

Sorry, I just forgot I would like to know if the same thing could be used to buy "gifts". Maybe if we could avoid giving the role to the buyer...

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4110
Re: Re: Re: Re: Ok...

I'm not sure what you mean by the "gifts" part - could you elaborate on that some more?

Regarding registering users who are not on the system yet, that is doable using user_save($account, $fields) added into your hook_order function within your custom module. You'd need to setup some fields in the product where the customer could add some email addresses, and then in the hook_order, loop through each value provided and run a user_save on all of them. You could even send them an email from there.

For an example of how this is done, check out the Gift Certificate contrib, and do a search for user_save. It'll give you the idea of how to create a user programatically. Running through the emails from a product is another thing, but easily accomplished.

Easiest way (so that you don't have to limit the # of fields) would be to have a product called "Registration" and include one text field per person - in other words, "Name", "Email Address", "Age", etc. That way you can (in hook_order) loop through each product, and find each product's attribute fields, and then perform actions against those.

Sorry I can't elaborate more, too busy, but if you still need help let me know and perhaps I can throw together a more complete example.

--
Help directly fund development: Donate via PayPal!

Daniorama's picture
Offline
Joined: 09/15/2008
Juice: 46
Re: Re: Re: Re: Re: Ok...

Hey, sorry for not answering sooner, I'm just too used to Drupal "recent posts" link and I didn't check this topic (I'm also not sure why I didn't get a notification of your answer)

I'm not sure if my current Drupal level is enough to not make a whole mess Laughing out loud I'll explain a bit more what I'm doing right now because maybe it could help to understand it:
I have an event and the sign up is divided in 2 steps. First they have to buy a ticket (using Ubercart) that assign a role that enables them to fill a Webform with the detail (like seat, if they are vegetarian and things like that...)
Ideally every user will buy its own ticket but there are many people who come together (groups or families) and I would like to have a easy system for them. Right now I've found 4 different solutions:
1-Trying to make a custom module as you explain, but I think it would be a bit too complex for me right now. But I could try it if you give me a bit guidance.
2-The buyer could submit so many form submissions as ticket buyed. I'm not sure if that would be easy as the number of submissions is set in the webform, maybe I could create a second Webform just for groups and try to find a way to join the number of tickets with the number of possible submissions.
3-Another option could be with Webform invite (but it's not ready for Drupal6)
4-There is also a contrib Module called Ubercart Webform Productize.

With the gift thing I mean letting the user buy things for other people and not to him/herself, normally that's easy for phisical things (just changing the shipping address), but file downloads or roles are not so easy. If with this system the buyer can include another user Email to receive the registration, role or file download without receiving him/herself then that will work nicely. This way the buyer has still the product order but only the other user recieve the "goodies" (it would be a kind of "virtual shipping address") The gift issue is related to this topic (because here is maybe the best solution to achieve it) but maybe it would be better another topic about it.

I'm sorry if this post is a bit "offtopic" about the issue. Maybe it would be better to just concentrate in the first option, but if anyone has ideas or experience with any of the others, please tell me. Thank you very much

jakonta's picture
Offline
Joined: 03/02/2011
Juice: 14
Similar idea

After posting here I realized that my issue was less complex than I originally thought, so I created a new thread at http://www.ubercart.org/forum/development/21234/getting_item_quantity_ho... - Once I finish this I would like to release it as a module so anyone finding this via search might want to check out the link in this post.