You are here

function uc_weightquote_quote in Ubercart 6.2

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

Standard callback to return a shipping rate via the weight quote method.

Parameters

$products: The order's products.

$details: Other order details including a shipping address.

Return value

A JSON object containing the shipping quote for the order.

1 string reference to 'uc_weightquote_quote'
uc_weightquote_shipping_method in shipping/uc_weightquote/uc_weightquote.module
Implements hook_shipping_method().

File

shipping/uc_weightquote/uc_weightquote.module, line 206
Shipping quote module that defines a weight-based shipping rate for each product.

Code

function uc_weightquote_quote($products, $details, $method) {
  $method = explode('_', $method['id']);
  $mid = $method[1];
  $context = array(
    'revision' => 'altered',
    'type' => 'amount',
  );
  if ($method = db_fetch_object(db_query("SELECT * FROM {uc_weightquote_methods} WHERE mid = %d", $mid))) {
    $context['extras']['weightquote_method'] = $method;

    // Start at the base rate.
    $rate = uc_price($method->base_rate, $context);
    foreach ($products as $product) {
      $context['subject']['order_product'] = $product;
      if (empty($product->weightquote) || is_null($product->weightquote[$mid])) {
        $price_info = array(
          'price' => $method->product_rate,
          'qty' => $product->qty,
        );

        // Add the method's default product rate.
        $product_rate = uc_price($price_info, $context);
      }
      else {
        $price_info = array(
          'price' => $product->weightquote[$mid],
          'qty' => $product->qty,
        );

        // Add the product-specific rate.
        $product_rate = uc_price($price_info, $context);
      }
      $rate += $product_rate * $product->weight * uc_weight_conversion($product->weight_units, variable_get('uc_weight_unit', 'lb'));
    }
    unset($context['subject']['order_product']);
    $context['revision'] = 'formatted';
    $formatted = uc_price($rate, $context);
    $quotes[] = array(
      'rate' => $rate,
      'format' => $formatted,
      'option_label' => check_plain($method->label),
    );
  }
  return $quotes;
}