You are here

function uc_roles_order in Ubercart 5

Implementation of hook_order().

File

uc_roles/uc_roles.module, line 316
Grants roles upon accepted payment of products

Code

function uc_roles_order($op, $order, $status) {
  global $user;
  switch ($op) {
    case 'update':

      // Only process role grants when the order is being updated to the
      // correct status, the status is actually being changed, and a valid user
      // has been assigned to the order.
      if ($status == variable_get('uc_roles_default_order_status', 'completed') && $order->order_status != $status && $order->uid > 0 && ($order_user = user_load(array(
        'uid' => $order->uid,
      ))) !== FALSE) {
        foreach ($order->products as $product) {
          $roles = db_query("SELECT * FROM {uc_roles_products} WHERE nid = %d", $product->nid);
          while ($role = db_fetch_object($roles)) {

            // Grant (or renew) role upon successful completion of payment
            if ($role->model == $product->model || empty($role->model)) {
              $existing_role = db_fetch_object(db_query("SELECT * FROM {uc_roles_expirations} WHERE uid = %d AND rid = %d", $order_user->uid, $role->rid));
              if (!is_null($existing_role->expiration)) {
                $op = 'renew';
                $comment = t('Customer user role %role renewed.', array(
                  '%role' => _get_role_name($role->rid),
                ));
              }
              else {
                $op = 'grant';
                $comment = t('Customer granted user role %role.', array(
                  '%role' => _get_role_name($role->rid),
                ));
              }
              $quantity = $role->by_quantity ? $product->qty : 1;
              _role_action($op, $order_user, $role->rid, _get_expiration_date($role->duration * $quantity, $role->granularity, $existing_role->expiration));
              $new_expiration = db_fetch_object(db_query("SELECT * FROM {uc_roles_expirations} WHERE uid = %d AND rid = %d", $order_user->uid, $role->rid));
              uc_order_comment_save($order->order_id, $user->uid, $comment);
              _role_email_user($op, $order_user, $new_expiration, $order);
              $order_user = user_load(array(
                'uid' => $order->uid,
              ));
            }
          }
        }
      }
      break;
  }
}