Created
April 16, 2019 21:56
-
-
Save travis-bradbury/fd922f69485716802e4c59d558e8bae3 to your computer and use it in GitHub Desktop.
A Drupal Commerce 2.x tax rate resolver example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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