class TaxRateResolver in Commerce Product Tax 8
Returns the tax rate configured on the product variation.
Hierarchy
- class \Drupal\commerce_product_tax\Resolver\TaxRateResolver implements TaxRateResolverInterface, TaxTypeAwareInterface uses TaxTypeAwareTrait
Expanded class hierarchy of TaxRateResolver
1 string reference to 'TaxRateResolver'
1 service uses TaxRateResolver
File
- src/
Resolver/ TaxRateResolver.php, line 16
Namespace
Drupal\commerce_product_tax\ResolverView source
class TaxRateResolver implements TaxRateResolverInterface, TaxTypeAwareInterface {
use TaxTypeAwareTrait;
/**
* {@inheritdoc}
*/
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;
}
}
}
}
}
/**
* Gets the tax field names attached to the purchasable entity.
*
* @param \Drupal\commerce\PurchasableEntityInterface $entity
* The purchasable entity.
*
* @return array
* An array of tax field names.
*/
protected function getTaxFieldNames(PurchasableEntityInterface $entity) {
$field_names = [];
foreach ($entity
->getFieldDefinitions() as $field) {
if ($field
->getType() == 'commerce_tax_rate') {
$field_names[] = $field
->getName();
}
}
return $field_names;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TaxRateResolver:: |
protected | function | Gets the tax field names attached to the purchasable entity. | |
TaxRateResolver:: |
public | function |
Resolves the tax rate for the given tax zone. Overrides TaxRateResolverInterface:: |
|
TaxRateResolverInterface:: |
constant | |||
TaxTypeAwareTrait:: |
protected | property | The tax type. | |
TaxTypeAwareTrait:: |
public | function |