You are here

function uc_select_address in Ubercart 7.3

Same name and namespace in other branches
  1. 8.4 uc_store/uc_store.module \uc_select_address()
  2. 5 uc_store/uc_store.module \uc_select_address()
  3. 6.2 uc_store/uc_store.module \uc_select_address()

Creates an address select box based on a user's previous orders.

Parameters

$uid: The user's ID to search for in the orders table.

$type: Choose either 'shipping' or 'billing'.

1 call to uc_select_address()
uc_order_address_book_form in uc_order/uc_order.admin.inc
Presents previously entered addresses as selectable options.

File

uc_store/uc_store.module, line 1456
Contains global Ubercart functions and store administration functionality.

Code

function uc_select_address($uid, $type = 'billing', $onchange = '', $title = NULL) {
  $addresses = uc_get_addresses($uid, $type);
  if (!is_array($addresses) || count($addresses) == 0) {
    return NULL;
  }
  $options = array(
    '0' => t('Select one...'),
  );
  foreach ($addresses as $key => $address) {
    $option = $address['street1'];

    // Check if the address is a duplicate (i.e. same address, but sent to
    // different person).
    if (isset($addresses[$key - 1]) && $option == $addresses[$key - 1]['street1'] || isset($addresses[$key + 1]) && $option == $addresses[$key + 1]['street1']) {
      $option .= ' - ' . $address['first_name'] . ' ' . $address['last_name'];
    }
    $options[drupal_json_encode($address)] = check_plain($option);
  }
  $select = array(
    '#type' => 'select',
    '#title' => is_null($title) ? t('Address book') : $title,
    '#options' => $options,
    '#attributes' => array(
      'onchange' => $onchange,
    ),
  );
  return $select;
}