4

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.

  • 1
    Any reason you can't copy the query from uc_order_history() and just use it manually? It doesn't look like there's a purpose-built function for what you need – Clive Oct 21 '13 at 13:06
  • Thanks for the suggestion. Yes, I've ended up by writing a custom function. – kenorb Oct 21 '13 at 13:39
3

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.

  • Wow, I'm surprised there is no function available for this. Lame. – rooby Oct 21 '13 at 14:42
  • So am I, it's a joke you can just do uc_orders_load_by_uid() or something... – leexonline Jan 13 '14 at 22:42
3

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);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.