You are here

function commerce_avatax_address_suggestion_apply in Drupal Commerce Connector for AvaTax 7.5

Submit handler for the continue button of the checkout form. Apply the address suggestion if selected in the Jquery Dialog.

1 string reference to 'commerce_avatax_address_suggestion_apply'
commerce_avatax_form_alter in ./commerce_avatax.module
Implements hook_form_alter().

File

./commerce_avatax.module, line 857
AvaTax service integration from Avalara, Inc.

Code

function commerce_avatax_address_suggestion_apply($form, &$form_state) {

  // Check if we need to apply the selected address suggestion.
  if (!isset($form_state['commerce_avatax']) || empty($form_state['values']['use_suggested_address'])) {
    return;
  }
  $settings = $form_state['commerce_avatax'];
  $profile = commerce_customer_profile_load($settings['customer_profile_to_update']);

  // If the profile could not be loaded, or if no address suggestion was found.
  if (!is_object($profile) || !isset($settings['address_validation_result']['suggestions'])) {
    return;
  }
  $address_suggestion = reset($settings['address_validation_result']['suggestions']);
  $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
  foreach ($address_suggestion as $key => $value) {
    $profile_wrapper->commerce_customer_address->{$key} = $value;
  }
  $profile_wrapper
    ->save();

  // If the profile ID has been updated, we need to update the order's
  // reference.
  if ($settings['customer_profile_to_update'] != $profile->profile_id) {
    $pane_id = 'customer_profile_' . $profile->type;
    if ($field_name = variable_get('commerce_' . $pane_id . '_field', '')) {
      $order = commerce_order_load($form_state['order']->order_id);
      $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
      $order_wrapper->{$field_name} = $profile->profile_id;
      commerce_order_save($order);
    }
  }
}