You are here

function commerce_shipping_recalculate_services_submit in Commerce Shipping 7.2

Submit callback for recalculating shipping services.

1 string reference to 'commerce_shipping_recalculate_services_submit'
commerce_shipping_form_commerce_checkout_form_alter in ./commerce_shipping.module
Implements hook_form_FORM_ID_alter().

File

includes/commerce_shipping.checkout_pane.inc, line 211
Callback functions for the shipping module's checkout panes.

Code

function commerce_shipping_recalculate_services_submit($form, &$form_state) {
  $order = $form_state['order'];
  $rebuild = FALSE;

  // If recalculation shouldn't happen, halt the process in this submit handler
  // as opposed to doing it in the validate step which would result in an empty
  // validation error message.
  if (empty($form_state['recalculate'])) {
    return;
  }

  // If any customer profile pane exists on the form, process its input before
  // recalculating shipping rates. This may mean submitting the checkout pane
  // itself, but it may also involve copying the necessary data from another
  // customer profile checkout pane.
  foreach (commerce_customer_profile_types() as $type => $profile_type) {
    $pane_id = 'customer_profile_' . $type;

    // If this pane is actually present in the form...
    if (!empty($form[$pane_id])) {

      // If the profile copy button has been checked for this customer profile...
      if (!empty($form_state['values'][$pane_id]['commerce_customer_profile_copy'])) {
        $source_id = 'customer_profile_' . variable_get('commerce_' . $pane_id . '_profile_copy_source', '');
        $info = array(
          'commerce_customer_profile',
          $type,
          $pane_id,
        );

        // Try getting the source profile data from validated input in the form
        // state's values array if it is present on the form.
        if (isset($form_state['values'][$source_id])) {
          commerce_customer_profile_copy_fields($info, $form_state['input'][$pane_id], $form_state['input'][$source_id], $form_state);
          commerce_customer_profile_copy_fields($info, $form_state['values'][$pane_id], $form_state['values'][$source_id], $form_state);
        }
        else {

          // Otherwise, attempt to get source profile from the order object.
          $wrapper = entity_metadata_wrapper('commerce_order', $order);
          $profile = NULL;

          // Look first for a profile coming from a matching customer profile
          // reference field.
          if ($source_field_name = variable_get('commerce_' . $source_id . '_field', '')) {
            $profile = $wrapper->{$source_field_name}
              ->value();
          }
          elseif (!empty($form_state['order']->data['profiles'][$source_id])) {

            // Otherwise fallback to the customer profile referenced
            // in the order data array.
            $profile = commerce_customer_profile_load($form_state['order']->data['profiles'][$source_id]);
          }

          // Assuming we found a source profile, use that to copy data into the
          // form state for the current profile.
          if (!empty($profile)) {
            commerce_customer_profile_copy_fields($info, $form_state['input'][$pane_id], $profile, $form_state);
            commerce_customer_profile_copy_fields($info, $form_state['values'][$pane_id], $profile, $form_state);
          }
        }

        // Unset any cached addressfield data for this customer profile.
        foreach (field_read_fields(array(
          'type' => 'addressfield',
        )) as $field) {
          if (isset($form_state['input']['customer_profile_' . $type][$field['field_name']])) {
            foreach ($form_state['input']['customer_profile_' . $type][$field['field_name']][LANGUAGE_NONE][0] as $key => &$value) {
              if ($key == 'country') {
                continue;
              }
              $value = '';
            }
          }
        }
      }

      // Now that we have data in the form state array for the copied address,
      // go ahead and validate the data through the checkout pane's validation
      // handler.
      $checkout_pane = commerce_checkout_pane_load($pane_id);

      // Submit the pane and save the customer profiles.
      if ($callback = commerce_checkout_pane_callback($checkout_pane, 'checkout_form_submit')) {
        $callback($form, $form_state, $checkout_pane, $order);
        $rebuild = TRUE;
      }
    }
  }

  // Save the order and rebuild the form to reflect the updated customer data.
  if ($rebuild) {

    // Avoid overwriting an already updated order.
    $stored_order = entity_load_unchanged('commerce_order', $order->order_id);
    if ($stored_order->changed <= $order->changed) {
      commerce_order_save($order);
    }
    $form_state['rebuild'] = TRUE;
  }
}