You are here

function commerce_shipping_service_rate_order in Commerce Shipping 7.2

Adds a shipping rate to the given order object for the specified service.

Parameters

string $service: The machine-name of the shipping service to rate.

object $order: The order for which the shipping service should be rated.

1 call to commerce_shipping_service_rate_order()
commerce_shipping_rate_apply in ./commerce_shipping.rules.inc
Action: Apply a shipping rate to an order.
1 string reference to 'commerce_shipping_service_rate_order'
commerce_shipping_default_rules_configuration in ./commerce_shipping.rules_defaults.inc
Implements hook_default_rules_configuration().

File

./commerce_shipping.module, line 571
Defines a system for calculating shipping costs associated with an order.

Code

function commerce_shipping_service_rate_order($service, $order) {

  // Load the full shipping service info array.
  $shipping_service = commerce_shipping_service_load($service);

  // If the service specifies a rate callback...
  if ($callback = commerce_shipping_service_callback($shipping_service, 'rate')) {

    // Get the base rate price for the shipping service.
    $price = $callback($shipping_service, $order);

    // If we got a base price...
    if ($price) {

      // Create a calculated shipping line item out of it.
      $line_item = commerce_shipping_service_rate_calculate($service, $price, $order->order_id);
      $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

      // Add the rate to the order as long as it doesn't have
      // a NULL price amount.
      if (!is_null($line_item_wrapper->commerce_unit_price->amount
        ->value())) {

        // Include a weight property on the line item object from the shipping
        // service for sorting rates.
        $line_item->weight = empty($shipping_service['weight']) ? 0 : $shipping_service['weight'];
        $order->shipping_rates[$service] = $line_item;
      }
    }
  }
}