You are here

function commerce_addressbook_commerce_order_presave in Commerce Addressbook 7.2

Same name and namespace in other branches
  1. 7 commerce_addressbook.module \commerce_addressbook_commerce_order_presave()

Implements hook_commerce_order_presave().

This hook sets the default profile as initial value on the corresponding checkout panes.

File

./commerce_addressbook.module, line 532
Defines addressbook functionality for customer profiles, allowing them to be reused and managed on a per-user basis.

Code

function commerce_addressbook_commerce_order_presave($order) {
  if ($order->uid) {
    foreach (commerce_checkout_panes() as $pane_id => $checkout_pane) {

      // Only for panes for which the address book is enabled.
      if (variable_get('commerce_' . $pane_id . '_addressbook', FALSE)) {
        $type = substr($checkout_pane['pane_id'], 17);

        // Removes 'customer_profile_'
        // Does this profile type have a default?
        $default_profile_id = commerce_addressbook_get_default_profile_id($order->uid, $type);
        if ($default_profile_id) {

          // Is this profile stored in a field or in the data association?
          if (($field_name = variable_get('commerce_' . $pane_id . '_field', '')) && empty($order->{$field_name})) {

            // Check to ensure the specified profile reference field exists on
            // the current order type.
            if (field_info_instance('commerce_order', $field_name, $order->type)) {
              $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
              $order_wrapper->{$field_name} = $default_profile_id;
            }
          }
          else {

            // Or store it in the association stored in the order's data array
            // if no field is set.
            $order->data['profiles'][$checkout_pane['pane_id']] = $default_profile_id;
          }
        }
      }
    }
  }
}