You are here

function hook_uc_addresses_select_addresses_alter in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 uc_addresses.api.php \hook_uc_addresses_select_addresses_alter()

This hook allows you to alter the addresses that the user can choose from at checkout or when editing the order, depending on the context $context.

You will get an array of UcAddressesAddress instances where some of them may be saved in the user's address book and others come from other sources (such as previous orders). Which addresses you get depends on what addresses are delivered by modules that implement hook_uc_addresses_select_addresses() and if the user has any saved addresses in his/her address book.

You can find out from which module the address came by checking $address->module. That property is only available in this context, normally UcAddressesAddress instances don't have that property set.

This hook will only be invoked if the hook hook_uc_addresses_select_addresses() resulted in any addresses, so you have always at least one address in the addresses array.

Parameters

array $addresses: An array of UcAddressesAddress instances.

int $uid: The user ID to select addresses for.

string $context: The context in which the addresses are used:

  • checkout_form
  • order_form

string $type: The type of address to select addresses for (shipping or billing).

Return value

void

1 invocation of hook_uc_addresses_select_addresses_alter()
uc_addresses_get_select_addresses in ./uc_addresses.module
Returns an array of addresses to be used for the selecting address widget.

File

./uc_addresses.api.php, line 484
These hooks are invoked by the Ubercart Addresses module. @todo more documentation needed for hook_uc_addresses_field_handlers(). @todo Document the rest of the API.

Code

function hook_uc_addresses_select_addresses_alter(&$addresses, $uid, $context, $type) {

  // Example 1: Don't let the user choose from addresses in Canada.
  foreach ($addresses as $index => $address) {
    if ($address
      ->getField('country') == 124) {

      // The addresses' country is Canada (124). Remove from the addresses array.
      unset($addresses[$index]);
    }
  }

  // Example 2: Don't let the user choose the default billing address if it
  // should select an address for shipping.
  if ($type == 'shipping') {
    foreach ($addresses as $index => $address) {
      if ($address
        ->isDefault('billing') && !$address
        ->isDefault('shipping')) {

        // The address is the default billing address (and not the default
        // shipping address). Remove from the addresses array.
        unset($addresses[$index]);
      }
    }
  }

  // Example 3: At checkout, let the user select from his/her address book only
  // (thus only saved addresses are allowed and not addresses from other sources).
  if ($context == 'checkout_form') {
    foreach ($addresses as $index => $address) {
      if ($address
        ->isNew()) {

        // The address is new which means it's not saved in the address book.
        // Remove from the addresses array.
        unset($addresses[$index]);
      }
    }
  }
}