You are here

function _uc_usps_package_products in Ubercart 5

Same name and namespace in other branches
  1. 6.2 shipping/uc_usps/uc_usps.module \_uc_usps_package_products()
  2. 7.3 shipping/uc_usps/uc_usps.module \_uc_usps_package_products()
2 calls to _uc_usps_package_products()
uc_usps_intl_quote in shipping/uc_usps/uc_usps.module
Callback for retrieving USPS shipping quote to other countries.
uc_usps_quote in shipping/uc_usps/uc_usps.module
Callback for retrieving USPS shipping quote.

File

shipping/uc_usps/uc_usps.module, line 541
Shipping quote method module that receives quotes from the United States Postal Service via XML web service.

Code

function _uc_usps_package_products($products, &$addresses) {
  $last_key = 0;
  $packages = array();
  if (variable_get('uc_usps_all_in_one', true) && count($products) > 1) {
    foreach ($products as $product) {
      if ($product->nid) {
        $address = (array) uc_quote_get_default_shipping_address($product->nid);
        $key = array_search($address, $addresses);
        if ($key === false) {
          $addresses[++$last_key] = $address;
          $key = $last_key;
          $packages[$key][0] = new stdClass();
        }
      }
      $packages[$key][0]->price += $product->price * $product->qty;
      $packages[$key][0]->weight += $product->weight * $product->qty * uc_weight_conversion($product->weight_units, 'lb');
    }
    foreach ($packages as $key => $package) {
      $packages[$key][0]->pounds = floor($package[0]->weight);
      $packages[$key][0]->ounces = 16 * ($package[0]->weight - $packages[$key][0]->pounds);
      $packages[$key][0]->container = 'RECTANGULAR';
      $packages[$key][0]->size = 'REGULAR';

      // Packages are "machinable" if heavier than 6oz. and less than 35lbs.
      $packages[$key][0]->machinable = ($packages[$key][0]->pounds == 0 ? $packages[$key][0]->ounces >= 6 : true) && $packages[$key][0]->pounds <= 35 && ($packages[$key][0]->pounds == 35 ? $packages[$key][0]->ounces == 0 : true);
      $packages[$key][0]->qty = 1;
    }
  }
  else {
    foreach ($products as $product) {
      if ($product->nid) {
        $address = (array) uc_quote_get_default_shipping_address($product->nid);
        $key = array_search($address, $addresses);
        if ($key === false) {
          $addresses[++$last_key] = $address;
          $key = $last_key;
        }
      }
      if (!$product->pkg_qty) {
        $product->pkg_qty = 1;
      }
      $num_of_pkgs = (int) ($product->qty / $product->pkg_qty);
      if ($num_of_pkgs) {
        $package = drupal_clone($product);
        $package->description = $product->model;
        $weight = $product->weight * $product->pkg_qty;
        switch ($product->weight_units) {
          case 'g':
            $weight = $weight / 1000;
          case 'kg':
            $weight = $weight * 2.2;
          case 'lb':
            $package->pounds = floor($weight);
            $package->ounces = 16 * ($weight - $package->pounds);
            break;
          case 'oz':
            $package->pounds = floor($weight / 16);
            $package->ounces = $weight - $package->pounds * 16;
            break;
        }
        $package->container = $product->usps['container'];
        $length_conversion = uc_length_conversion($product->length_units, 'in');
        $package->length = max($product->length, $product->width) * $length_conversion;
        $package->width = min($product->length, $product->width) * $length_conversion;
        $package->height = $product->height * $length_conversion;
        if ($package->length < $package->width) {
          list($package->length, $package->width) = array(
            $package->width,
            $package->length,
          );
        }
        $package->girth = 2 * $package->width + 2 * $package->height;
        $package->size = $package->length + $package->girth;
        if ($package->size <= 84) {
          $package->size = 'REGULAR';
        }
        else {
          if ($package->size <= 108) {
            $package->size = 'LARGE';
          }
          else {
            if ($package->size <= 130) {
              $package->size = 'OVERSIZE';
            }
            else {
              $package->size = 'GI-HUGE-IC';

              // Too big for the U.S. Postal service.
            }
          }
        }
        $package->machinable = $package->length >= 6 && $package->length <= 34 && $package->width >= 0.25 && $package->width <= 17 && $package->height >= 3.5 && $package->height <= 17 && ($package->pounds == 0 ? $package->ounces >= 6 : true) && $package->pounds <= 35 && ($package->pounds == 35 ? $package->ounces == 0 : true);
        $package->price = $product->price * $product->pkg_qty;
        $package->qty = $num_of_pkgs;
        $packages[$key][] = $package;
      }
      $remaining_qty = $product->qty % $product->pkg_qty;
      if ($remaining_qty) {
        $package = drupal_clone($product);
        $package->description = $product->model;
        $weight = $product->weight * $remaining_qty;
        switch ($product->weight_units) {
          case 'g':
            $weight = $weight / 1000;
          case 'kg':
            $weight = $weight * 2.2;
          case 'lb':
            $package->pounds = floor($weight);
            $package->ounces = 16 * ($weight - $package->pounds);
            break;
          case 'oz':
            $package->pounds = floor($weight / 16);
            $package->ounces = $weight - $package->pounds * 16;
            break;
        }
        $package->container = $product->usps['container'];
        $package->length = max($product->length, $product->width) * $length_conversion;
        $package->width = min($product->length, $product->width) * $length_conversion;
        $package->height = $product->height * $length_conversion;
        $package->girth = 2 * $package->width + 2 * $package->height;
        $package->size = $package->length + $package->girth;
        if ($package->size <= 84) {
          $package->size = 'REGULAR';
        }
        else {
          if ($package->size <= 108) {
            $package->size = 'LARGE';
          }
          else {
            if ($package->size <= 130) {
              $package->size = 'OVERSIZE';
            }
            else {
              $package->size = 'GI-HUGE-IC';

              // Too big for the U.S. Postal service.
            }
          }
        }
        $package->machinable = $package->length >= 6 && $package->length <= 34 && $package->width >= 0.25 && $package->width <= 17 && $package->height >= 3.5 && $package->height >= 17 && ($package->pounds == 0 ? $package->ounces >= 6 : true) && $package->pounds <= 35 && ($package->pounds == 35 ? $package->ounces == 0 : true);
        $package->price = $product->price * $remaining_qty;
        $package->qty = 1;
        $packages[$key][] = $package;
      }
    }
  }
  return $packages;
}