You are here

function uc_addresses_order in Ubercart Addresses 6.2

Same name and namespace in other branches
  1. 5.2 uc_addresses.module \uc_addresses_order()
  2. 5 uc_addresses.module \uc_addresses_order()

Implementation of hook_order().

Return value

void

File

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

Code

function uc_addresses_order($op, &$order, $arg2) {
  switch ($op) {
    case 'load':
      $order->uc_addresses = array();
      $address_types = array(
        'delivery',
        'billing',
      );
      foreach ($address_types as $order_type) {
        $address_type = $order_type;
        if ($order_type == 'delivery') {
          $address_type = 'shipping';
        }

        // Check session first for temporary saved addresses.
        if (isset($_SESSION['uc_addresses_order'][$order->order_id][$address_type])) {
          $address = unserialize($_SESSION['uc_addresses_order'][$order->order_id][$address_type]);
        }
        else {

          // Construct new address.
          $address = UcAddressesAddressBook::get($order->uid)
            ->addAddress();

          // Loop through all address field definitions and set those that exists for the order.
          $fields_data = uc_addresses_get_address_fields();
          foreach ($fields_data as $fieldname => $fielddata) {
            $order_field_name = $order_type . '_' . $fieldname;
            if (isset($order->{$order_field_name})) {
              $address
                ->setField($fieldname, $order->{$order_field_name});
            }
          }
        }
        $order->uc_addresses[$address_type] = $address;
      }
      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('load', $order, $arg2);
      }
      break;
  }
}