An order with two order items. One is taxed using the Standard rate (French VAT, for example), the other is taxed using the Reduced rate. The order summary will show two rows both labelled "VAT".
The TaxOrderProcessor needs to detect this situation (count the occurrence of adjustments with the same label but different source IDs), and when detected, add the used percentage, so that we get something like:
VAT - 19%
VAT (19%)
19% VAT
We need to decide which one is preferable.
My current idea for getting the used percentage is to have LocalTaxTypeBase append it to the source ID.
| Comment | File | Size | Author |
|---|---|---|---|
| #61 | 2881056-patch-61.patch | 9.11 KB | grevil |
| #52 | 2024-11-19 12_10_46-Warenkorb _ Bilderrahmen-passt.de_.png | 8.3 KB | grevil |
| #15 | 2881056-distinguish_taxes.patch | 2.2 KB | finex |
| #10 | distinguish_taxes_in_order_summary-2881056-10.patch | 2.14 KB | lowfidelity |
| #8 | allow_use_rate_label.patch | 794 bytes | fastangel |
Issue fork commerce-2881056
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
bojanz commentedNote that adjustments now have a getPercentage() getter that is potentially useful.
Comment #3
bojanz commented#2917222: The order total summary doesn't have access to adjustment percentages made a Twig-level workaround possible.
Comment #4
lowfidelityIn my case I needed to display "VAT 10 %" and "VAT 20 %" above the total amount. In order to display the different tax rates on the order overview page including their percentage, I copied commerce-order-total-summary.html.twig from the commerce/modules/order/ dir to my template folder and changed the loop that prints the tax rates like this:
I'm using a custom tax rate resolver based on the example posted by @Megachriz to apply different tax rates.
Comment #5
megachriz@paper boy
I just noticed this issue on a commerce site in development. Thanks for your code example! You were using a code example posted by me, now I'm using a code example posted by you! :D
@bojanz
Would it make sense to add a setting to the tax type for if and how you want to display the percentage of a tax or do you recommend people to add it in theirselves in templates?
Comment #6
megachriz@paper boy
I adjusted your code a bit as I was getting a percentage shown for shipping costs as well. This resulted into "Shipping 0%".
In my case I put the percentage to show before the label.
Comment #7
megachrizI had to make a similar adjustment in the commerce-order-receipt.html.twig template:
Comment #8
fastangel commentedI did this with a simple patch to use the label rate instead zone label. Here is my patch.
Comment #9
bojanz commented@fastangel
That won't work for any country that uses VAT. Nobody wants to see "Standard" as the label for the tax.
Comment #10
lowfidelityThanks for your additions, @MegaChriz! I created a patch from #4, #6 and #7. Tested it on Drupal 8.5.4 and Commerce 2.x-dev.
Comment #11
fastangel commentedGood point bojanz I used with custom no with rest of types.
Thanks
Comment #12
lowfidelityCan confirm, the patch from #10 works on Drupal Commerce 8.x-2.8 and current DEV. Any reason why the patch didn't make it into 8.x-2.8 release?
Comment #14
finex commentedHi, the patch looks fine.
Would be possible to do the same for discounts: instead of a generic "discount" label it could be printed the discount name.EDIT: nevermind, there is already an issue about this bug: https://www.drupal.org/project/commerce/issues/2770731 :-)
Comment #15
finex commentedUpdated patch for the latest 8.x-2.x-dev release
Comment #16
pstewart commentedI've run into this problem and had a slightly different approach - rather than changing the template to conditionally display the percentage, I incorporated it directly into the label at the point the adjustment is created. I've attached a patch which does this, however I'm leaning towards the template patch from #15 being a better solution as it has greater flexibility to override how the adjustment is presented through the theme layer.
Comment #18
pstewart commentedA slight mod to #15 for whitespace consistency with subtotal and total lines in the order summary
Comment #19
matthiasm11 commentedThe approach in #15/#18 looks better to me, compared to the one in #16, due to flexibility. Thanks!
Comment #20
jsacksick commented-
Comment #21
mindhunter75 commented#10 worked for me! Thank you very much!
Comment #22
jsacksick commentedThe approach in #16 looks like the right one to me, although I'd probably just output the zone display label and the percentage (i.e no rate label). We'd still need to figure out what should be the default output, perhaps the following instead?
Which would output VAT 17% for example.
Comment #23
megachrizThis patch adjusts the label of tax adjustments in preprocess functions, using the suggestion from #22.
Comment #24
jsacksick commentedthat isn't my suggestion :). My suggestion is to update LocalTaxTypeBase (where we add the adjustment, similarly to what's done in #16.
Comment #25
megachriz@jsacksick
I'm not too sure about the approach in #16. That makes the tax label "to be set in stone". And since the translation happens on creation, on multilingual sites you might get different displays of the tax labels - it then depends on which language was active when the user placed the order. The approach in #23 makes the tax label to be displayed in a more consistent way, since then the translation happens upon viewing the order (in Dutch the percentage before the tax display label is more common).
Comment #26
pstewart commentedI agree with @MegaChriz, I'm currently using #18 in production to solve this problem.
Comment #27
finex commentedUpdated patch 15/18 for new Drupal Commerce
Comment #28
pstewart commentedComment #29
pstewart commentedHidden my original files as #18 now superceded by #27 re-roll and #16 generally agreed to be an inadequate approach. I'm now using #27 following upgrade to 2.24, but I've also taken a look at 23 and think this preprocess approach has equal merit, perhaps a little better for keeping the logic out of the template and in the preprocess function.
Comment #30
lubwn commentedPatch #23 worked for me flawlessly. Thank you!
Comment #32
attisanTook the idea from patch #23 (@MegaChriz) and extended it to be themable. this way the default behavior remains unchanged but the template can be overridden to accommodate for displaying percentages, or whatever needed.
Comment #35
attisansadly though, my patch does not fix cases where tax adjustments are shown in other modules (I'm specifically looking at commerce_invoice).
Comment #36
attisanChanged MR from hooking into specific theme hooks to using
hook_preprocess()so 3rd party usage of order totals are covered too.Comment #37
anybodyJust ran into the same requirement! Even if only one tax is used, it would be super helpful to show the vat rate used for information and easier review.
We'll have a look at this soon to push things forward.
Comment #40
grevil commentedMain branch was way too far behind. Created a new 3.0.x branch and manually applied changes by @attisan.
Comment #42
grevil commentedThe new twig template is quite different from the original one. I'll adjust this accordingly.
Comment #43
grevil commentedAdjusted accordingly.
Comment #44
anybodyI've hidden the patches to focus on the MR now and get this fixed! :)
Comment #45
jsacksick commentedI disagree with this approach... I think perhaps it'd be better to introduce an event from the OrderTotalSummary service, and have the Tax module react to it. From there we could alter the adjustment label to include the tax percentage.
Comment #46
anybodyThanks @jsacksick we'll implement that as separate MR tomorrow. Having the VAT rate shown is definitely a big win!
Comment #49
grevil commentedAlright, all done. Ready for review!
Comment #50
anybodyThanks @grevil! LGTM and seems to do what @jsacksick suggested?
I only had a little docs comment, everything else seems fine to me.
Could you post a screenshot with the result?
Comment #51
anybodyComment #52
grevil commentedHere is a screenshot (although in german and using a custom theme, but anyway):

EDIT: Mwst. = VAT
Comment #53
jsacksick commentedHm... I think we need to fix the naming as well...
Maybe OrderSummaryBuildTotalsEvent? (a bit wordy though). But just BUILD_TOTAL could confuse people and they'd be under the impression that the event is dispatched when the order total is recalculated.
The event constant should probably be
OrderEvents::ORDER_SUMMARY_BUILD_TOTALS.Also, the alteration should probably only be done for VAT adjustments... So probably makes sense to do this only if the adjustment is included? (as VAT is always included).
Also, the way it is currently done makes the label untranslatable...
It should be something like:
Or maybe we keep the original label just like you did if we can't easily get the zone display label.
Also, just so you know, I'd like to wait for feedback from other people that previously contributed here before moving forward with this.
Comment #54
grevil commentedThis is already the case, as we are checking the adjustment type in
modifyAdjustmentsLabels. Or am I misunderstanding you here?Not quite sure what the zone display label is here?
Comment #55
jsacksick commented@grevil: Checking the adjustment type isn't sufficient, there is also the Sales tax scenario and other taxes as well.
Comment #56
grevil commented@jsacksick but wouldn't it be always preferable to have the tax percentage displayed on adjustments of type "tax"?
Comment #57
anybodyLooks like we need some more fine-graded number formatting:
e.g. 0.09975 * 100
https://git.drupalcode.org/project/commerce/-/blob/3.0.x/modules/tax/src...
So that the number is formatted correctly.
Comment #58
grevil commentedAlright, should be fixed now using the commerce price calculator (as @jsacksick already mentioned in #53 anyway).
Back to needs review!
Comment #59
jsacksick commented@grevil: Perhaps we need to allow resetting the whole array? And not just adjustments? I mean, can't really think of a usecase altering the subtotal or total... But I guess it doesn't hurt?
Comment #60
grevil commentedAlright, done!
I implemented it, so that the subtotal and total is only overwritten for the "buildTotals" array, meaning that
$order->getSubtotalPrice() and $order->getTotalPrice()will have the unmodified value set.Comment #61
grevil commentedStatic patch for the time being.
Comment #62
anybodyThanks @grevil! Works great and as expected so far!!
Comment #63
jsacksick commented@grevil: My suggestion from #59 was to pass the whole array to the subscriber, so it can easily be read / altered. Eventually, additional data could be injected for use in a custom template as well.
So perhaps a
getTotals()and asetTotals()helper as opposed to getters / setters for the subtotal & total.In any event, as mentioned on Slack, I'd like to get feedback from the community on this considering 3-4 different approaches have been previously implemented here.
Comment #64
grevil commentedAlright, we now pass the whole array!
Comment #65
anybodyComment #66
jsacksick commented@anybody: you mean RTBCED by your company :), as mentioned, waiting for other community members to chime in here, especially the ones that contributed patches previously. But from a pure code point of view, this looks good now (as I wanted it to look at least).
Comment #67
anybody@jsacksick yes, but there's no option for that. Waiting longer without active people here won't help much I think? The world moves on and Drupal needs to do the same...
But yes of course always important to have as much feedback as possible. Having this RTBC'd doesn't mean will be accepted immediately by maintainers. Thanks :)
Comment #68
jsacksick commentedThere is 2 remaining problems with the MR though. The event dispatcher has a NULL default value. We shouldn't make it optional, or we should conditionally dispatch the event.
Also, I think the label alteration makes the label untranslatable.
$adjustment['label'] = $originalLabel . ' (' . $taxPercentage . '%)';So perhaps a translatable string here with a context? commerce_tax?
t('@label (@tax_percentage%'));or something similar.Comment #69
anybodyDefinitely +1 thanks for the feedback!
Comment #70
megachrizI see that on a site where I had this issue I'm still using the solution from #7.
I do plan to try to update that site to as much D11 compatible releases as possible later this month, so perhaps I can test the changes from this issue along with it. But no promises, I noticed that when I tried to do
composer updatenow, quite a few patches could no longer be applied. It depends on how much time it will take to update these if there will be any time left to look at this issue.Comment #71
grevil commentedBack to NW based on #68
Comment #72
grevil commentedAlright, adjusted accordingly, thanks for the feedback @jsacksick!
Comment #73
grevil commentedI don't think anyone will chime in here anymore. Can we get this fixed?
Comment #74
jsacksick commentedI have no problem with the new event subscriber etc... But the label change should have been introduced prior to tagging 3.0.0 as I'm pretty sure the change introduced will not please anyone... Merging this will cause the adjustment label to change instantly on live sites as well...
Also, there are test failures, and we have no new tests testing the new label... So not 100% convinced we can just go ahead and commit this change as is.
We might need to merge the 3.x changes and kick off the tests to see where things stand.
Comment #75
jsacksick commentedI made some changes / improvements to the patch.
Comment #76
jsacksick commentedActually, now having second thoughts on the approach... Perhaps we should simply add a tax type setting... "Append the tax rate to the adjustment label"... This would remove the need for the subscriber and can be easily toggled within the UI.
Also, this can be added to the LocalTaxTypeBase class only so it doesn't affect tax type plugins using external APIS like Avatax.
Hmmm... Actually, I think we did it this way so that the alteration is done runtime rather than affecting the adjustment that is stored in DB. This way the adjustment label would always be on the current site language...
Comment #77
grevil commentedCan't remember exactly anymore. But we could still add a new setting (off by default) which prevents the subscriber to do anything, until activated!
EDIT: Or maybe there is something like an conditional subscriber in drupal / commerce? Unsure about that.
Comment #78
grevil commented@jsacksick, so what are your final thoughts on this? I think the current approach is pretty good!
Comment #79
jsacksick commentedOk, so let's do the following:
Then we adjust the following logic to check for the setting:
Also, let's change modifyAdjustmentsLabels() to alterAdjustmentsLabels() (nitpicking here but...)
To also check for the setting.
We could maybe set the default value to TRUE, and people can opt out if they're annoyed.
Comment #80
grevil commentedAlright, on it!
Comment #81
grevil commentedDone! Works great and as expected, please review!
Comment #82
jsacksick commentedTo make the code more efficient, let's pass the following to shouldAlterAdjustment().
And then update shouldAlterAdjustment() to:
We're also missing adjustments to the schema.yml.
Comment #83
jsacksick commentedComment #84
grevil commentedAll done! Please review!
Comment #85
jsacksick commentedLet's update the
OrderTotalSummaryTestto test this behavior and then we are good to go. We have no tests ensuring this functionality works so there is a risk in breaking this in the future without noticing. So back to "Needs work", for the tests.Comment #86
jsacksick commentedTests were written, if these are green, I'll merge this.
Comment #88
jsacksick commentedComment #89
grevil commentedGreat stuff! Thank you @jsacksick!