function commerce_usps_rate in Commerce USPS 7.2
Same name and namespace in other branches
- 7 commerce_usps.module \commerce_usps_rate()
Returns a base price array for a shipping service calculated for the order.
Parameters
array $service: An array describing the shipping service.
object $order: The order object.
Return value
array The service rates returned from USPS
1 string reference to 'commerce_usps_rate'
File
- ./
commerce_usps.module, line 78 - Defines the USPS shipping method and services for Drupal Commerce.
Code
function commerce_usps_rate($service, $order) {
// Attempt to recover cached shipping rates.
$rates = commerce_shipping_rates_cache_get('usps', $order, variable_get('commerce_usps_rates_timeout', 0));
// If no cached rates were found or they have expired.
if (!is_array($rates) && commerce_usps_validate_order($order)) {
// Load files required for building requests.
require_once dirname(__FILE__) . '/includes/commerce_usps.xml.inc';
$shipping_address = commerce_usps_get_order_shipping_address($order);
// Determine which type of rate request to submit.
if ($shipping_address['country'] == 'US') {
$rates = commerce_usps_rate_v4_request($order, $shipping_address);
}
else {
$rates = commerce_usps_intl_rate_v2_request($order, $shipping_address);
}
commerce_shipping_rates_cache_set('usps', $order, (array) $rates);
}
// Return the rate for the requested service or FALSE if not found.
return isset($rates[$service['name']]) ? $rates[$service['name']] : FALSE;
}