You are here

public function TaxRateResolver::resolve in Commerce Product Tax 8

Resolves the tax rate for the given tax zone.

Parameters

\Drupal\commerce_tax\TaxZone $zone: The tax zone.

\Drupal\commerce_order\Entity\OrderItemInterface $order_item: The order item.

\Drupal\profile\Entity\ProfileInterface $customer_profile: The customer profile. Contains the address and tax number.

Return value

\Drupal\commerce_tax\TaxRate|string|null The tax rate, NO_APPLICABLE_TAX_RATE, or NULL.

Overrides TaxRateResolverInterface::resolve

File

src/Resolver/TaxRateResolver.php, line 23

Class

TaxRateResolver
Returns the tax rate configured on the product variation.

Namespace

Drupal\commerce_product_tax\Resolver

Code

public function resolve(TaxZone $zone, OrderItemInterface $order_item, ProfileInterface $customer_profile) {
  $purchased_entity = $order_item
    ->getPurchasedEntity();
  if (!$purchased_entity) {
    return NULL;
  }
  $tax_field_names = $this
    ->getTaxFieldNames($purchased_entity);
  foreach ($tax_field_names as $field_name) {
    $field_items = $purchased_entity
      ->get($field_name);
    if ($field_items
      ->isEmpty()) {
      continue;
    }
    $tax_type = $field_items
      ->getFieldDefinition()
      ->getSetting('tax_type');
    if ($tax_type != $this->taxType
      ->id()) {
      continue;
    }
    foreach ($field_items as $tax_rate) {
      list($zone_id, $rate_id) = explode('|', $tax_rate->value);
      if ($zone
        ->getId() != $zone_id) {
        continue;
      }
      if ($rate_id === static::NO_APPLICABLE_TAX_RATE) {
        return static::NO_APPLICABLE_TAX_RATE;
      }
      foreach ($zone
        ->getRates() as $rate) {
        if ($rate
          ->getId() == $rate_id) {
          return $rate;
        }
      }
    }
  }
}