You are here

function _commerce_fedex_get_package_items in Commerce FedEx 7

Internal function to determine shippable line items from the order.

Parameters

object $order: The commerce order object for the order that we're requesting rates for.

Return value

array The list of packages and dimensions for this order that should be submitted to FedEx.

1 call to _commerce_fedex_get_package_items()
commerce_fedex_create_rate_request in includes/commerce_fedex_soap_client.inc
Function to create FedEx rate request array.

File

includes/commerce_fedex_soap_client.inc, line 293
Handles the SOAP request/response to FedEx Web Services servers.

Code

function _commerce_fedex_get_package_items($order) {

  // Get the weight and volume of the shippable items.
  $weight = commerce_physical_order_weight($order, 'lb');
  $volume = commerce_physical_order_volume($order, 'in');

  // Determine the default package volume from the FedEx settings.
  $default_package_volume = variable_get('commerce_fedex_default_package_size_length', '0') * variable_get('commerce_fedex_default_package_size_width', '0') * variable_get('commerce_fedex_default_package_size_height', '0');

  // Calculate the number of packages that should be created based on the
  // size of products and the default package volume.
  $number_of_packages = ceil($volume['volume'] / $default_package_volume);

  // Check the FedEx settings to see if we should request insurance.
  $insurance_value = array();
  if (variable_get('commerce_fedex_insurance')) {
    $insurance_value = _commerce_fedex_get_insurance_value($order);
  }

  // Loop through the number of caluclated package to create the return array.
  for ($i = 1; $i <= $number_of_packages; $i++) {
    $package_line_items[$i - 1] = array(
      'SequenceNumber' => $i,
      'GroupPackageCount' => 1,
      'Weight' => array(
        'Value' => round(max(array(
          0.1,
          $weight['weight'] / $number_of_packages,
        )), 2),
        'Units' => 'LB',
      ),
      'Dimensions' => array(
        'Length' => variable_get('commerce_fedex_default_package_size_length'),
        'Width' => variable_get('commerce_fedex_default_package_size_width'),
        'Height' => variable_get('commerce_fedex_default_package_size_height'),
        'Units' => 'IN',
      ),
    );

    // If an isurance value has been returned, add insurance value to request.
    if (!empty($insurance_value['amount']) && $insurance_value['amount'] > 0) {
      $package_line_items[$i - 1]['InsuredValue']['Currency'] = $insurance_value['currency_code'];
      $package_line_items[$i - 1]['InsuredValue']['Amount'] = round($insurance_value['amount'] / $number_of_packages, 2);
    }
  }

  // Make sure that we've found shippable items in the order.
  if (!empty($package_line_items)) {
    return $package_line_items;
  }
  else {
    return FALSE;
  }
}