You are here

function uc_tax_apply_item_tax in Ubercart 8.4

Calculates taxable amount for a single product.

2 calls to uc_tax_apply_item_tax()
uc_tax_apply_tax in uc_tax/uc_tax.module
Applies taxes to an order.
uc_tax_get_included_tax in uc_tax/uc_tax.module
Calculates the taxes that should be included in a product's display price.

File

uc_tax/uc_tax.module, line 442
Ubercart Tax module.

Code

function uc_tax_apply_item_tax($item, $tax) {

  // @todo The $item parameter can be many different objects, refactor this!
  $nid = $item instanceof NodeInterface ? $item
    ->id() : $item->nid->target_id;

  // Determine the product type.
  if (is_array($item->data) && isset($item->data['type'])) {

    // Saved in the order product data array.
    $type = $item->data['type'];
  }
  elseif (empty($nid)) {

    // "Blank-line" product.
    $type = 'blank-line';
  }
  elseif ($node = Node::load($nid)) {

    // Use type of current node, if it exists.
    $type = $node
      ->getType();
  }
  else {

    // Default to generic product.
    $type = 'product';
  }

  // Determine whether this is a shippable product.
  if (is_array($item->data) && isset($item->data['shippable'])) {

    // Saved in the order product data array.
    $shippable = $item->data['shippable'];
  }
  elseif (empty($nid)) {

    // "Blank line" product.
    $shippable = $item->weight > 0;
  }
  elseif ($node = Node::load($nid)) {

    // Use current node.
    $shippable = $node->shippable->value;
  }
  else {

    // Use default for this node type.
    $settings = NodeType::load($type)
      ->getModuleSettings('uc_product');
    $shippable = isset($settings['shippable']) ? $settings['shippable'] : TRUE;
  }

  // Tax products if they are of a taxed type and if it is shippable if
  // the tax only applies to shippable products.
  if (in_array($type, $tax->taxed_product_types) && ($tax->shippable == 0 || $shippable == 1)) {
    return is_object($item->price) ? $item->price->value : $item->price;
  }
  else {
    return FALSE;
  }
}