You are here

function uc_quote_assemble_quotes in Ubercart 8.4

Same name and namespace in other branches
  1. 7.3 shipping/uc_quote/uc_quote.pages.inc \uc_quote_assemble_quotes()

Pulls the get_quote_from_* triggers and assembles their returned data.

2 calls to uc_quote_assemble_quotes()
uc_cart_pane_quotes in shipping/uc_quote/uc_quote.module
Cart pane callback.
uc_quote_build_quote_form in shipping/uc_quote/uc_quote.module
Calculates and returns the shipping quote selection form.

File

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

Code

function uc_quote_assemble_quotes($order) {
  $shipping_types = [];
  foreach ($order->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'];
    }
  }
  $manager = \Drupal::service('plugin.manager.uc_quote.method');
  $methods = ShippingQuoteMethod::loadMultiple();
  uasort($methods, 'Drupal\\uc_quote\\Entity\\ShippingQuoteMethod::sort');

  /* @todo Implement shipping quote types
    foreach ($methods as $id => $method) {
      if (!isset($method['quote']) || ($method['quote']['type'] != 'order' && $method['quote']['type'] != $shipping_type)) {
        unset($methods[$id]);
      }
    }
    */
  $quotes = [];
  foreach ($methods as $method) {
    if ($method
      ->status()) {
      $plugin = $manager
        ->createInstance($method
        ->getPluginId(), $method
        ->getPluginConfiguration());
      foreach ($plugin
        ->getQuotes($order) as $accessorial => $quote) {
        $quotes[$method
          ->id()][$accessorial] = [
          'rate' => $quote,
          'format' => uc_currency_format($quote),
          'option_label' => $method
            ->label(),
        ];
      }
    }
  }
  return $quotes;
}