You are here

function uc_addresses_uc_checkout_complete in Ubercart Addresses 6.2

Same name and namespace in other branches
  1. 6 uc_addresses.module \uc_addresses_uc_checkout_complete()
  2. 7 uc_addresses.module \uc_addresses_uc_checkout_complete()

Implementation of hook_uc_checkout_complete().

Saves any new addresses to the address book.

Parameters

object $order: The Ubercart order.

object $account: The user who ordered.

Return value

void

File

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

Code

function uc_addresses_uc_checkout_complete($order, $account) {
  if (empty($account->uid)) {

    // No account has been created. This can happen when anonymous checkout is
    // enabled and the "Customer information" pane is disabled.
    return;
  }
  if (isset($order->uc_addresses)) {
    $saved_addresses = array();
    foreach ($order->uc_addresses as $address_type => $address) {
      if (!empty($address->save_address)) {

        // Check if the user already has similar looking addresses.
        if (UcAddressesAddressBook::get($account->uid)
          ->compareAddress($address)) {

          // Don't save the address.
        }
        else {
          if (!$address
            ->isOwned()) {
            $address
              ->setOwner($account->uid);
          }

          // Save address.
          $address
            ->save();

          // We keep track of which addresses are saved, so we can find
          // out later which one will become the default shipping address
          // and which one the default billing address.
          $saved_addresses[$address_type] = $address;

          // Make sure we have all types of default addresses in the
          // saved addresses array.
          foreach (uc_addresses_address_types() as $save_address_type) {
            if (!isset($saved_addresses[$save_address_type])) {
              $saved_addresses[$save_address_type] = $address;
            }
          }
        }
      }
    }

    // Find out which address will become the default shipping address
    // and which one the default billing address.
    foreach ($saved_addresses as $address_type => $address) {

      // Check if addresses may set as this default type.
      if (!variable_get('uc_addresses_use_default_' . $address_type, TRUE)) {

        // Don't set this default type.
        continue;
      }
      if (!$address
        ->getAddressBook()
        ->getDefaultAddress($address_type)) {

        // If there is no such address, then make this address the default.
        $address
          ->setAsDefault($address_type);
        $address
          ->save();
      }
    }
  }

  // Remove any temporary addresses from session.
  unset($_SESSION['uc_addresses_order']);
}