You are here

function uc_addresses_uc_order in Ubercart Addresses 7

Implements hook_uc_order().

Return value

void

File

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

Code

function uc_addresses_uc_order($op, &$order) {
  global $user;
  switch ($op) {
    case 'new':
      $order->uc_addresses = array();
      foreach (uc_addresses_address_types() as $address_type) {
        $address = NULL;

        // Check if address may be automatically filled in.
        if (variable_get('uc_addresses_default_' . $address_type . '_address', TRUE) && variable_get('uc_addresses_use_default_' . $address_type, TRUE)) {

          // Get default address of type $address_type.
          $address = UcAddressesAddressBook::get($user->uid)
            ->getDefaultAddress($address_type);
          if ($address) {
            $address = $address
              ->copyAddress(UcAddressesAddressBook::get(0));

            // Set order values.
            $order_address_type = $address_type;
            if ($order_address_type == 'shipping') {
              $order_address_type = 'delivery';
            }
            $address_values = $address
              ->getRawFieldData();
            foreach ($address_values as $fieldname => $value) {
              $order_fieldname = $order_address_type . '_' . $fieldname;
              $order->{$order_fieldname} = $value;
            }
          }
        }
        if (empty($address)) {

          // Construct new address.
          $order_address_type = $address_type;
          if ($address_type == 'shipping') {
            $order_address_type = 'delivery';
          }
          $address = uc_addresses_new_address_from_order($order, $order_address_type);
        }
        $address->address_type = $address_type;
        $order->uc_addresses[$address_type] = $address;
      }
      break;
    case 'load':
      uc_addresses_order_attach_addresses($order);
      break;
    case 'save':
      if (!isset($order->uc_addresses)) {

        // Attach address objects immediately after saving, so the order
        // doesn't require a reload to gain the address objects.
        uc_addresses_order_attach_addresses($order);
      }
      break;
  }
}