You are here

function uc_addresses_get_addresses in Ubercart Addresses 7

Loads a customer's previously given addresses from the uc_orders table.

Similar to uc_get_adddresses() in uc_store.module.

Parameters

int $uid: The user to select addresses for.

string $type: The type of address to look for: "delivery" or "billing".

Return value

array An array of address arrays.

1 call to uc_addresses_get_addresses()
uc_addresses_uc_addresses_select_addresses in ./uc_addresses.uc_addresses_fields.inc
Implements hook_uc_addresses_select_addresses().

File

./uc_addresses.module, line 2044
Adds user profile address support to Ubercart.

Code

function uc_addresses_get_addresses($uid, $type = 'billing') {
  if ($uid == 0) {
    return NULL;
  }
  $query = db_select('uc_orders', 'o')
    ->distinct();
  $query
    ->addTag('uc_get_addresses');
  $query
    ->addTag('address_type:' . $type);
  $alias = array();
  $fields = uc_addresses_get_address_fields();
  $schema = drupal_get_schema('uc_orders');
  foreach ($fields as $field_name => $field) {
    $order_field_name = $type . '_' . $field_name;
    if (isset($schema['fields'][$order_field_name])) {
      $alias[$field_name] = $query
        ->addField('o', $order_field_name, $field_name);
    }
  }
  $query
    ->condition('uid', $uid)
    ->condition('order_status', uc_order_status_list('general', TRUE), 'IN');
  $result = $query
    ->execute();
  $addresses = array();
  while ($address = $result
    ->fetchAssoc()) {
    if (!empty($address['street1']) || !empty($address['postal_code'])) {
      $addresses[] = $address;
    }
  }
  return $addresses;
}