--- profile_role.module.orig	2010-01-18 21:18:01.000000000 -0800
+++ profile_role.module	2010-09-06 00:30:42.000000000 -0700
@@ -71,12 +71,28 @@ function profile_role_access_category($u
  * Restrict the user registration form.
  */
 function profile_role_form_user_register_alter(&$form, $form_state) {
+  $roles = array();
+  
   if ($form['#action'] == '/user/register') {
-    // No way to determine what role user will be, so leave form alone.
-    return;
+    // try to determine what role this user is purchasing and pull it out of the cart
+    if (module_exists('uc_roles')) {
+      $cart = uc_cart_get_contents();
+      foreach ($cart AS $item) {
+        $nids[]   = $item->nid;
+        $models[] = $item->model;
+      }
+      $result = db_query("SELECT rid, model FROM {uc_roles_products} WHERE model IN ('%s') AND nid IN (%d)", 
+        implode("', '", $models), implode(',', $nids));
+      while ($row = db_fetch_object($result)) {
+        $roles[$row->rid] = $row->model;
+      }
+    } else {
+      // No way to determine what role user will be, so leave form alone.
+      return;
+    }
   }
-
-  $roles = isset($form_state['post']['roles']) ? $form_state['post']['roles'] : array();
+  
+  $roles = isset($form_state['post']['roles']) ? $form_state['post']['roles'] : $roles;
   $category_roles = profile_role_get_roles();
   $result = db_query('SELECT DISTINCT(category)
                       FROM {profile_fields}');
