You are here

function uc_cart_pane_quotes in Ubercart 6.2

Same name and namespace in other branches
  1. 8.4 shipping/uc_quote/uc_quote.module \uc_cart_pane_quotes()
  2. 5 shipping/uc_quote/uc_quote.module \uc_cart_pane_quotes()
  3. 7.3 shipping/uc_quote/uc_quote.module \uc_cart_pane_quotes()

Cart pane callback.

See also

theme_uc_cart_pane_quotes()

1 string reference to 'uc_cart_pane_quotes'
uc_quote_cart_pane in shipping/uc_quote/uc_quote.module
Implements hook_cart_pane().

File

shipping/uc_quote/uc_quote.module, line 689
The controller module for fulfillment modules that process physical goods.

Code

function uc_cart_pane_quotes($form_state, $items) {
  global $user;

  // Get all quote types necessary to fulfill order.
  $shipping_types = array();
  foreach ($items as $product) {
    $shipping_types[] = uc_product_get_shipping_type($product);
  }
  $shipping_types = array_unique($shipping_types);
  $all_types = uc_quote_get_shipping_types();
  $shipping_type = '';
  $type_weight = 1000;

  // arbitrary large number
  foreach ($shipping_types as $type) {
    if ($all_types[$type]['weight'] < $type_weight) {
      $shipping_type = $all_types[$type]['id'];
      $type_weight = $all_types[$type]['weight'];
    }
  }
  $methods = array_filter(module_invoke_all('shipping_method'), '_uc_quote_method_enabled');
  uasort($methods, '_uc_quote_type_sort');
  $method_choices = array();
  foreach ($methods as $method) {
    if ($method['quote']['type'] == 'order' || $method['quote']['type'] == $shipping_type) {
      $method_choices[$method['id']] = $method['title'];
    }
  }
  $form['delivery_country'] = uc_country_select(uc_get_field_name('country'), uc_store_default_country(), NULL, 'name', TRUE);
  $country_id = isset($_POST['delivery_country']) ? intval($_POST['delivery_country']) : uc_store_default_country();
  $form['delivery_zone'] = uc_zone_select(uc_get_field_name('zone'), NULL, NULL, $country_id, 'name', TRUE);
  $form['delivery_postal_code'] = uc_textfield(uc_get_field_name('postal_code'), '', TRUE, NULL, 10, 10);
  $form['quote_method'] = array(
    '#type' => 'hidden',
    '#value' => key($method_choices),
  );
  $form['get_quote'] = array(
    '#type' => 'button',
    '#value' => t('Calculate'),
  );
  $form['page'] = array(
    '#type' => 'hidden',
    '#value' => 'cart',
  );
  $form['uid'] = array(
    '#type' => 'hidden',
    '#value' => $user->uid,
  );
  $form['quote'] = array(
    '#type' => 'markup',
    '#value' => '<div id="quote"></div>',
  );
  drupal_add_js(array(
    'uc_quote' => array(
      'progress_msg' => t('Receiving quotes:'),
      'err_msg' => check_markup(variable_get('uc_quote_err_msg', t("There were problems getting a shipping quote. Please verify the delivery and product information and try again.\nIf this does not resolve the issue, please call in to complete your order.")), variable_get('uc_quote_msg_format', FILTER_FORMAT_DEFAULT), FALSE),
    ),
    'ucURL' => array(
      'shippingQuotes' => url('cart/checkout/shipping/quote'),
    ),
  ), 'setting');
  drupal_add_js('misc/progress.js');
  drupal_add_js(drupal_get_path('module', 'uc_quote') . '/uc_quote.js');
  $prod_string = '';
  foreach ($items as $item) {
    $prod_string .= '|' . $item->nid;
    $prod_string .= '^' . $item->title;
    $prod_string .= '^' . $item->model;
    $prod_string .= '^' . $item->qty;
    $prod_string .= '^' . $item->cost;
    $prod_string .= '^' . $item->price;
    $prod_string .= '^' . $item->weight;
    $prod_string .= '^' . serialize($item->data);
  }
  $prod_string = substr($prod_string, 1);

  // If a previous quote gets loaded, make sure it gets saved again.
  // Also, make sure the previously checked option is checked by default.
  drupal_add_js('$(function() {
    setQuoteCallbacks("' . drupal_urlencode($prod_string) . '");
    $("#uc-cart-pane-quotes").submit(function() {
      quoteCallback("' . drupal_urlencode($prod_string) . '");
      return false;
    });
  })', 'inline');
  return $form;
}