function commerce_fedex_service_rate_order in Commerce FedEx 7
Returns an array of shipping method rates obtained from FedEx servers.
Parameters
array $shipping_service: The shipping service that is being requested by commerce shipping.
object $order: The commerce order object for the order that we're requesting rates for.
Return value
array The rate values for the requested shipping service.
1 string reference to 'commerce_fedex_service_rate_order'
File
- ./
commerce_fedex.module, line 96 - Defines the FedEx shipping method and services for Drupal Commerce.
Code
function commerce_fedex_service_rate_order($shipping_service, $order) {
// First attempt to recover cached shipping rates for the current order.
$rates = commerce_shipping_rates_cache_get('fedex', $order, 0);
// The request for each FedEx service is the same because they're collected
// all at once. If the first request failed, then they will all fail. If the
// rates variable is not an array then continue with the request.
if (!is_array($rates)) {
// Include the file that handles SOAP requests.
module_load_include('inc', 'commerce_fedex', 'includes/commerce_fedex_soap_client');
// Create the rate request to submit to FedEx for collecting shipping rates.
$request = commerce_fedex_create_rate_request($order);
if (!empty($request)) {
// Submit the soap request and gather available FedEx shipping rates.
$response = commerce_fedex_submit_soap_request($request, 'getRates');
// Make sure the response was successful and contains rate reply details.
if (!empty($response) && !empty($response->RateReplyDetails)) {
// Set the variables are regex to determine which rates should be used.
$rate_option = strtoupper(variable_get('commerce_fedex_rate_service_type', 'list'));
$rate_match = '/^PAYOR_' . $rate_option . '/';
// If only one rate reply comes back, then RateReplyDetails won't be an array.
// However, we expect one. Stick the rate reply into an array to make the code
// below happy.
if (!is_array($response->RateReplyDetails)) {
$response->RateReplyDetails = array(
$response->RateReplyDetails,
);
}
// Loop through the rate details object from FedEx.
foreach ($response->RateReplyDetails as $rate) {
// Set amount and currency code to NULL.
$amount = NULL;
$currency_code = NULL;
// When requesting ACCOUNT rates, GROUND_HOME_DELIVERY will return an
// object instead an array of objects.
if (is_object($rate->RatedShipmentDetails)) {
if (preg_match($rate_match, $rate->RatedShipmentDetails->ShipmentRateDetail->RateType)) {
$amount = $rate->RatedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Amount;
$currency_code = $rate->RatedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Currency;
}
}
else {
// Loop through the array of rate option objects.
foreach ($rate->RatedShipmentDetails as $shipment_details) {
if (preg_match($rate_match, $shipment_details->ShipmentRateDetail->RateType)) {
$amount = $shipment_details->ShipmentRateDetail->TotalNetCharge->Amount;
$currency_code = $shipment_details->ShipmentRateDetail->TotalNetCharge->Currency;
}
}
}
if (!empty($amount) && !empty($currency_code)) {
$name = check_plain($rate->ServiceType);
// Create the shipping service array to return to commerce_shipping.
$rates[strtolower($name)] = array(
'amount' => commerce_currency_decimal_to_amount($amount, $currency_code),
'currency_code' => $currency_code,
'data' => array(),
);
}
}
}
if (!empty($rates)) {
// Cache the calculated rates for subsequent requests.
commerce_shipping_rates_cache_set('fedex', $order, $rates);
}
else {
// If request fails, cache an empty array to prevent multiple failed
// requests since all additional requests will fail as well.
commerce_shipping_rates_cache_set('fedex', $order, array());
}
}
}
// Return the rate for the requested service or FALSE if not found.
return isset($rates[$shipping_service['name']]) ? $rates[$shipping_service['name']] : FALSE;
}