function uc_select_address in Ubercart 8.4
Same name and namespace in other branches
- 5 uc_store/uc_store.module \uc_select_address()
- 6.2 uc_store/uc_store.module \uc_select_address()
- 7.3 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.
string $type: Choose either 'shipping' or 'billing'.
1 call to uc_select_address()
- AddressBookForm::buildForm in uc_order/
src/ Form/ AddressBookForm.php - Form constructor.
File
- uc_store/
uc_store.module, line 471 - 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 = [
'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[Json::encode($address)] = $option;
}
$select = [
'#type' => 'select',
'#title' => is_null($title) ? t('Address book') : $title,
'#options' => $options,
'#attributes' => [
'onchange' => $onchange,
],
];
return $select;
}