Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save travis-bradbury/fd922f69485716802e4c59d558e8bae3 to your computer and use it in GitHub Desktop.

Select an option

Save travis-bradbury/fd922f69485716802e4c59d558e8bae3 to your computer and use it in GitHub Desktop.
A Drupal Commerce 2.x tax rate resolver example
mymodule.services.yml
Define a tax resolver plugin with a priority higher than the default tax resolver.
```
services:
mymodule.tax_resolver:
class: Drupal\mymodule\Resolver\TaxResolver
tags:
- { name: commerce_tax.tax_rate_resolver, priority: 100 }
```
mymodule/src/Resolver/TaxResolver.php
Return TaxRateResolverInterface::NO_APPLICABLE_TAX_RATE when you want something exempt.
Return null to allow other resolvers to run.
```
namespace Drupal\mymodule\Resolver
// Some use statements.
class TaxResolver implements TaxRateResolverInterface {
/**
* {@inheritdoc}
*/
public function resolve(TaxZone $zone, OrderItemInterface $order_item, ProfileInterface $customer_profile) {
$bundle = $order_item->getPurchasedEntity()->getProduct()->bundle();
// The default bundle is exempt from all tax.
if ($bundle === 'default') {
return self::NO_APPLICABLE_TAX_RATE;
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment