You are here

function commerce_addressbook_commerce_order_presave in Commerce Addressbook 7

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

Implements hook_commerce_order_presave().

File

./commerce_addressbook.module, line 381
:

Code

function commerce_addressbook_commerce_order_presave($order) {
  global $user;
  $uid = $user->uid;

  // If enabled, prefill the customer profiles on checkout form automatically with the latest saved customer profile
  if (variable_get('commerce_addressbook_auto_prefill') == TRUE && empty($order->order_id)) {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $profile_bundles = commerce_addressbook_get_bundles_with_field('commerce_addressbook_saved_profiles');
    $saved_addresses = commerce_addressbook_get_saved_addresses($uid);
    if ($profile_bundles) {
      foreach ($profile_bundles as $entity_type => $bundles) {
        foreach ($bundles as $bundlename => $fieldname) {
          if (isset($saved_addresses[$entity_type][$bundlename])) {
            $property = 'commerce_customer_' . $bundlename;
            $default_value = key($saved_addresses[$entity_type][$bundlename]);
            $order_wrapper->{$property} = $default_value;
          }
        }
      }
    }
  }
}