You are here

function uc_checkout_pane_quotes in Ubercart 6.2

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

Shipping quote checkout pane callback.

Selects a quoting method based on the enabled methods' weight and the types of products in the cart. The "Get Quotes" button fires a callback that returns a form for the customer to select a rate based on their needs and preferences.

Adds a line item to the order that records the chosen shipping quote.

1 call to uc_checkout_pane_quotes()
uc_paypal_ec_review_form in payment/uc_paypal/uc_paypal.pages.inc
1 string reference to 'uc_checkout_pane_quotes'
uc_quote_checkout_pane in shipping/uc_quote/uc_quote.module
Defines the shipping quote checkout pane.

File

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

Code

function uc_checkout_pane_quotes($op, &$order, $arg2) {
  global $user;
  switch ($op) {
    case 'view':
      $description = check_markup(variable_get('uc_quote_pane_description', t('Shipping quotes are generated automatically when you enter your address and may be updated manually with the button below.')), variable_get('uc_quote_desc_format', FILTER_FORMAT_DEFAULT), FALSE);

      // Let Javascript know where we are.
      $contents['page'] = array(
        '#type' => 'hidden',
        '#value' => 'checkout',
      );
      $contents['quote_button'] = array(
        '#type' => 'button',
        '#value' => t('Click to calculate shipping'),
        '#weight' => 0,
      );
      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 address and product information and try again.\nIf this does not resolve the issue, please call @phone to complete your order.", array(
            '@phone' => variable_get('uc_store_phone', NULL),
          ))), 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');
      $default = NULL;
      if (!empty($order->quote['method'])) {
        $default = $order->quote['method'] . '---' . (!empty($order->quote['accessorials']) ? $order->quote['accessorials'] : 0);
      }
      $prod_string = '';
      foreach (uc_cart_get_contents() 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('
        Drupal.behaviors.getQuotes = function (context) {
          setQuoteCallbacks("' . drupal_urlencode($prod_string) . '", context);
        };
        $(function() {
        var quoteButton = $("input:radio[name=quote-option]").click(function() {
          var quoteButton = $(this);
          var label = quoteButton.parent("label").text().split(":", 2)[0];
          var rate = parseFloat($("input:hidden[name=\'rate[" + quoteButton.val() + "]\']").val());
          set_line_item("shipping", label, rate, 1, 1, false);
          if (window.getTax) {
            getTax();
          }
          else if (window.render_line_items) {
            render_line_items();
          }
        }).filter("[value=' . $default . ']").click();
        var quoteDiv = $("#quote");
        if (quoteDiv.length && $("#quote input[name=quote-form]").length == 0) {
          quoteDiv.append("<input type=\\"hidden\\" name=\\"quote-form\\" value=\\"" + Drupal.encodeURIComponent(quoteDiv.html()) + "\\" />");
        }
      });', 'inline');
      return array(
        'description' => $description,
        'contents' => $contents,
      );
    case 'process':
      if (!isset($_POST['quote-option'])) {
        if (variable_get('uc_quote_require_quote', TRUE)) {
          drupal_set_message(t('You must select a shipping option before continuing.'), 'error');
          return FALSE;
        }
        else {
          return TRUE;
        }
      }
      $details = array();
      foreach ($order as $key => $value) {
        if (strpos($key, 'delivery_') !== FALSE) {
          $details[substr($key, 9)] = $value;
        }
      }
      $products = array();
      foreach ($order->products as $id => $product) {
        $node = (array) node_load($product->nid);
        foreach ($node as $key => $value) {
          if (!isset($product->{$key})) {
            $product->{$key} = $value;
          }
        }
        $order->products[$id] = $product;
      }
      $quote_option = explode('---', $_POST['quote-option']);
      $order->quote['method'] = $quote_option[0];
      $order->quote['accessorials'] = $quote_option[1];
      $_SESSION['quote']['quote_form'] = rawurldecode($_POST['quote-form']);
      $methods = array_filter(module_invoke_all('shipping_method'), '_uc_quote_method_enabled');
      $method = $methods[$quote_option[0]];
      $quote_data = array();
      $arguments = array(
        'order' => array(
          '#entity' => 'uc_order',
          '#title' => t('Order'),
          '#data' => $order,
        ),
        'method' => array(
          '#entity' => 'quote_method',
          '#title' => t('Quote method'),
          '#data' => $method,
        ),
        'account' => array(
          '#entity' => 'user',
          '#title' => t('User'),
          '#data' => $user,
        ),
      );
      $predicates = ca_load_trigger_predicates('get_quote_from_' . $method['id']);
      $predicate = array_shift($predicates);
      if ($predicate && ca_evaluate_conditions($predicate, $arguments)) {
        $quote_data = uc_quote_action_get_quote($order, $method);
      }
      if (!isset($quote_data[$quote_option[1]])) {
        drupal_set_message(t('Invalid option selected. Recalculate shipping quotes to continue.'), 'error');
        return FALSE;
      }
      $label = $method['quote']['accessorials'][$quote_option[1]];
      $order->quote['rate'] = $quote_data[$quote_option[1]]['rate'];
      $result = db_query("SELECT line_item_id FROM {uc_order_line_items} WHERE order_id = %d AND type = 'shipping'", $order->order_id);
      if ($lid = db_result($result)) {
        uc_order_update_line_item($lid, $label, $order->quote['rate']);
      }
      else {
        uc_order_line_item_add($order->order_id, 'shipping', $label, $order->quote['rate']);
      }
      return TRUE;
    case 'review':
      $context = array(
        'revision' => 'themed',
        'type' => 'line_item',
        'subject' => array(
          'order' => $order,
        ),
      );
      $result = db_query("SELECT * FROM {uc_order_line_items} WHERE order_id = %d AND type = '%s'", $order->order_id, 'shipping');
      if ($line_item = db_fetch_array($result)) {
        $context['subject']['line_item'] = $line_item;
        $review[] = array(
          'title' => $line_item['title'],
          'data' => uc_price($line_item['amount'], $context),
        );
      }
      return $review;
  }
}