You are here

function _uc_quote_assemble_quotes in Ubercart 6.2

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

Pulls the get_quote_from_* triggers and assembles their returned data.

1 call to _uc_quote_assemble_quotes()
uc_quote_request_quotes in shipping/uc_quote/uc_quote.pages.inc
Callback to return the shipping quote(s) of the appropriate quoting method(s).

File

shipping/uc_quote/uc_quote.pages.inc, line 78
Menu callbacks for shipping quotes requested through AJAX.

Code

function _uc_quote_assemble_quotes($order) {
  global $user;
  $account = user_load($order->uid);
  if (!$account) {
    $account = $user;
  }
  $products = $order->products;
  $shipping_types = array();
  foreach ($products 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 = '';

  // Use the most prominent shipping type (highest weight).
  // In theory, you can ship lighter products with heavier by the same
  // method, but not vice versa.
  $type_weight = -1000;

  // arbitrary low 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');
  foreach ($methods as $id => $method) {
    if ($method['quote']['type'] != 'order' && $method['quote']['type'] != $shipping_type) {
      unset($methods[$id]);
    }
  }
  $context = array(
    'revision' => 'formatted',
    'type' => 'line_item',
    'subject' => array(
      'order' => $order,
    ),
  );

  //drupal_set_message('<pre>'. print_r($products, TRUE) .'</pre>');
  $quote_data = array();
  $arguments = array(
    'order' => array(
      '#entity' => 'uc_order',
      '#title' => t('Order'),
      '#data' => $order,
    ),
    'method' => array(
      '#entity' => 'quote_method',
      '#title' => t('Quote method'),
    ),
    'account' => array(
      '#entity' => 'user',
      '#title' => t('User'),
      '#data' => $account,
    ),
  );
  foreach ($methods as $method) {
    $arguments['method']['#data'] = $method;
    $predicates = ca_load_trigger_predicates('get_quote_from_' . $method['id']);
    $predicate = array_shift($predicates);
    if ($predicate && ca_evaluate_conditions($predicate, $arguments)) {
      $data = uc_quote_action_get_quote($order, $method);
      foreach ($data as &$quote) {
        if (isset($quote['rate']) && !isset($quote['format'])) {
          $context['subject']['line_item'] = array(
            'type' => 'shipping',
            'name' => $quote['option_label'],
            'amount' => $quote['rate'],
            'weight' => 1,
          );
          $quote['format'] = uc_price($quote['rate'], $context);
        }
      }
      $quote_data[$method['id']] = $data;
    }
  }
  return $quote_data;
}