You are here

public function CommerceShippingExample::calculate_quote in Commerce Shipping 7

Calculate quote callback: the bulk of the shipping method is usually found here. This is where we do the actual calculations to figure out what the shipping costs should be. We can return a single price or for more control an array of arrays containing:

  • label
  • quantity
  • amount
  • currency code

Only the amount is needed as the rest have default values.

Overrides CommerceShippingQuote::calculate_quote

File

modules/plugins/quotes/example_plugin/CommerceShippingExample.class.php, line 87

Class

CommerceShippingExample

Code

public function calculate_quote($currency_code, $form_values = array(), $order = NULL, $pane_form = NULL, $pane_values = NULL) {
  if (empty($order)) {
    $order = $this->order;
  }
  $settings = $this->settings;
  $shipping_line_items = array();
  $shipping_line_items[] = array(
    'amount' => commerce_currency_decimal_to_amount($settings['shipping_price'], $currency_code),
    'currency_code' => $currency_code,
    'label' => t('Normal shipping'),
  );
  if (!empty($form_values['express'])) {
    $shipping_line_items[] = array(
      'amount' => commerce_currency_decimal_to_amount($settings['shipping_price'], $currency_code),
      'currency_code' => $currency_code,
      'label' => t('Express fee'),
    );
  }
  return $shipping_line_items;
}