Q:

How to load Ubercart orders for certain user ID?

How to load Ubercart orders for certain user ID programatically?
There is only uc_order_history() which returns formatted order table and does not work in that case.

A:

Find the following function in Drupal 7:
/**
 * Custom function to return list of orders for certain user
 */
function foo_get_user_orders($user, $order_status = 'completed') {
  $query = db_select('uc_orders', 'uo');
  $query->addField('uo', 'order_id');
  $query->addField('uo', 'order_total');
  $query->addField('uo', 'order_status'); // optional: $query->addField('uo', 'product_count');
  $query->addJoin('LEFT', 'uc_order_products', 'p', 'p.order_id = uo.order_id');
  $query->addField('p', 'nid');
  $query->condition('order_status', $order_status);
  $query->condition('uid', $user->uid);
  $result = $query->execute()->fetchAll();
  return $result;
}

For Drupal 6, please install dbtng module and it should work as well.

A:

Here's a pretty simple function which will return all the order objects for a user (D7):
function my_module_uc_orders_by_user($uid = NULL, $status = 'completed') {

  global $user;
  $uid = is_numeric($uid) ? $uid : $user->uid;

  if ($results = db_query("SELECT order_id FROM {uc_orders} WHERE uid = :uid AND order_status = :status", array(':uid' => $uid, ':status' => $status))->fetchAll()) {
    $order_ids = array();
    foreach ($results AS $result) {
      $order_ids[] = $result->order_id;
    }

    return uc_order_load_multiple($order_ids);
  }
}

