Tax not being applied to individual items but applied as line item
Opened this issue · 2 comments
Description
I have a VAT tax rule setup. The rule is set up to be applied to line items (not the cart total).
When the order details are submitted to SagePay, each item has a blank tax column and the VAT is added as a separate line item:
This isn't ideal as not all products will have tax applied to them and it also doesn't make sense from an accounting perspective.
Steps to reproduce
- Make an order with tax applied to items
- Items don't have tax applied to them in SagePage
Additional info
- Craft version: 3.7.13
- PHP version: 7.4.12
- Plugins & versions: Commerce 3.4.3, SagePay 2.1.1
I feel like this should be an option/default behaviour when you have tax rules set to apply to individual items.
Essentially, I've had to hack around doing the following to get the data to go into SagePay:
Using the EVENT_AFTER_CREATE_ITEM_BAG
event I'm having to completely rebuild the ItemBag
by populating it with new Omnipay Items, specifically using the extended version from Omnipay\SagePay\Extend\Item
so I can add VAT.
Essentially I'm doing the following:
- Looping over all the adjustments to find the shipping tax and storing it
- Looping over all the adjustments again to build up a new array with new
Item
s. For the shipping adjustment, I am usingsetVat
e.g.$newItem->setVat($shippingTax);
. I'm also at this point discarding any tax adjustments - Looping over all the order LineItems, storing their tax value from their adjustments and then creating a new Item and adding it to an array
- Merge the two arrays containing the LineItems and adjustments
- Using items->replace() to replace the ItemBag
This gives me the desired output:
Now, this is working. At the moment it's very much just some hacky code to test the theory so I won't post anything just yet. But I am wondering if there's a more sensible/robust way to achieve this?
For clarity, I should add I am using the legacy basket format (via the plugin settings) but I believe this happens later on and shouldn't affect this.