You are here

function commerce_shipping_service_rate_calculate in Commerce Shipping 7.2

Creates a shipping line item and passes it through Rules.

Creates a shipping line item with the specified initial price and passes it through Rules for additional calculation.

Parameters

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

array $price: A price array used to establish the base unit price for the shipping.

int $order_id: If available, the order to which the shipping line item will belong.

Return value

object The shipping line item with a calculated shipping rate.

1 call to commerce_shipping_service_rate_calculate()
commerce_shipping_service_rate_order in ./commerce_shipping.module
Adds a shipping rate to the given order object for the specified service.

File

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

Code

function commerce_shipping_service_rate_calculate($service, $price, $order_id = 0) {
  $shipping_service = commerce_shipping_service_load($service);

  // Create the new line item for the service rate.
  $line_item = commerce_shipping_line_item_new($service, $price, $order_id);

  // Set the price component of the unit price if it hasn't already been done.
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $data = $line_item_wrapper->commerce_unit_price->data
    ->value();
  if (empty($data['components'])) {
    $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add($line_item_wrapper->commerce_unit_price
      ->value(), $shipping_service['price_component'], $line_item_wrapper->commerce_unit_price
      ->value(), TRUE, FALSE);
  }
  rules_invoke_all('commerce_shipping_calculate_rate', $line_item);
  return $line_item;
}