You are here

function hook_calculate_tax in Ubercart 5

Same name and namespace in other branches
  1. 6.2 docs/hooks.php \hook_calculate_tax()

Calculate tax line items for an order.

Parameters

$order: An order object or an order id.

Return value

An array of tax line items keyed by a module-specific id.

2 functions implement hook_calculate_tax()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

uc_cybersource_calculate_tax in payment/uc_cybersource/uc_cybersource.module
Calculates taxes for an order using CyberSource's tax service.
uc_taxes_calculate_tax in uc_taxes/uc_taxes.module
Calculate the amount and types of taxes that apply to an order.
3 invocations of hook_calculate_tax()
uc_line_item_tax in uc_taxes/uc_taxes.module
Handle the tax line item.
uc_taxes_calculate in uc_taxes/uc_taxes.module
Calculates the taxes for an order based on enabled tax modules.
uc_taxes_javascript in uc_taxes/uc_taxes.module
AJAX callback for order preview.

File

docs/hooks.php, line 83
These are the hooks that are invoked by the Übercart core.

Code

function hook_calculate_tax($order) {
  global $user;
  if (is_numeric($order)) {
    $order = uc_order_load($order);
    $account = user_load(array(
      'uid' => $order->uid,
    ));
  }
  else {
    if ((int) $order->uid) {
      $account = user_load(array(
        'uid' => intval($order->uid),
      ));
    }
    else {
      $account = $user;
    }
  }
  if (!is_object($order)) {
    return array();
  }
  if (empty($order->delivery_postal_code)) {
    $order->delivery_postal_code = $order->billing_postal_code;
  }
  if (empty($order->delivery_zone)) {
    $order->delivery_zone = $order->billing_zone;
  }
  if (empty($order->delivery_country)) {
    $order->delivery_country = $order->billing_country;
  }
  if (is_array($order->line_items)) {
    foreach ($order->line_items as $i => $line) {
      if (substr($line['type'], 0, 4) == 'tax_' && substr($line['type'], 5) != 'subtotal') {
        unset($order->line_items[$i]);
      }
    }
  }
  $_SESSION['taxes'] = array();
  $taxes = uc_taxes_get_rates();
  foreach ($taxes as $tax) {

    // Gotta pass a fake line_item entity for the data to be saved to $_SESSION.
    workflow_ng_invoke_event('calculate_tax_' . $tax->id, $order, $tax, $account, array());

    //$order->line_items[] = array('type' => 'tax', 'amount' => $_SESSION['taxes'][$tax->id]['amount']);
  }
  $order->taxes = $_SESSION['taxes'];
  unset($_SESSION['taxes']);

  //array_unshift($order->taxes, array('id' => 'subtotal', 'name' => t('Subtotal excluding taxes'), 'amount' => $amount, 'weight' => -10));
  return $order->taxes;
}