1 ---
2 products:
3 - tax
4 ---
5
6 import { Table } from "docs-ui"
7
8 export const metadata = {
9 title: `Tax Lines in Cart Module`,
10 }
11
12 # {metadata.title}
13
14 In this document, you’ll learn about tax lines in a cart and how to retrieve tax lines with the Tax Module.
15
16 ## What are Tax Lines?
17
18 A tax line indicates the tax rate of a line item or a shipping method. The [LineItemTaxLine data model](/references/cart/models/LineItemTaxLine) represents a line item’s tax line, and the [ShippingMethodTaxLine data model](/references/cart/models/ShippingMethodTaxLine) represents a shipping method’s tax line.
19
20 ![A diagram showcasing the relation between other data models and the tax line models](https://res.cloudinary.com/dza7lstvk/image/upload/v1711534431/Medusa%20Resources/cart-tax-lines_oheaq6.jpg)
21
22 ---
23
24 ## Tax Inclusivity
25
26 By default, the tax amount is calculated by taking the tax rate from the line item or shipping method’s amount, and then adding them to the item/method’s subtotal.
27
28 However, line items and shipping methods have an `is_tax_inclusive` property that, when enabled, indicates that the item or method’s price already includes taxes.
29
30 So, instead of calculating the tax rate and adding it to the item/method’s subtotal, it’s calculated as part of the subtotal.
31
32 <Note>
33
34 The following diagram is a simplified showcase of how a subtotal is calculated from the taxes perspective.
35
36 </Note>
37
38 ![A diagram showing an example of calculating the subtotal of a line item using its taxes](https://res.cloudinary.com/dza7lstvk/image/upload/v1711535295/Medusa%20Resources/cart-tax-inclusive_shpr3t.jpg)
39
40 For example, if a line item's amount is `5000`, the tax rate is `10`, and tax inclusivity is enabled, the tax amount is 10% of `5000`, which is `500`, making the unit price of the line item `4500`.
41
42 ---
43
44 ## When Are Tax Lines Recalculated?
45
46 When you use the Cart Module in the Medusa application, Medusa recalculates a cart's tax lines automatically as the customer builds and updates their cart. So, in most cases, you don't need to manually recalculate them.
47
48 Whether Medusa recalculates the tax lines depends on the `automatic_taxes` setting of the cart's [region](../../region/page.mdx):
49
50 - If `automatic_taxes` is enabled, Medusa recalculates the affected tax lines automatically when the cart changes.
51 - If `automatic_taxes` is disabled, Medusa only calculates tax lines when you send a request to the [Calculate Cart Taxes API route](!api!/store/carts/calculate-taxes).
52
53 ### Tax Lines Require a Shipping Address
54
55 Regardless of the `automatic_taxes` setting, Medusa only calculates tax lines once the cart's shipping address has a country code. Until you set the shipping address's country, the cart's line items and shipping methods have no tax lines.
56
57 Medusa determines the tax rates from the country in the cart's shipping address, not from the list of countries in the cart's [region](../../region/page.mdx). So, a region with multiple countries doesn't have a default country for tax calculation. Medusa calculates each customer's taxes based on the country they set in their shipping address.
58
59 <Note>
60
61 If the cart's region has only one country, Medusa sets that country as the cart's shipping address country when creating the cart. So, Medusa can calculate tax lines without the customer setting their address. For a region with multiple countries, the customer must set their shipping address's country before Medusa calculates tax lines.
62
63 </Note>
64
65 ### Tax Recalculation by Cart Operation
66
67 The following table explains how each cart operation affects the cart's tax lines when `automatic_taxes` is enabled:
68
69 <Table>
70 <Table.Header>
71 <Table.Row>
72 <Table.HeaderCell>
73 Operation
74 </Table.HeaderCell>
75 <Table.HeaderCell>
76 Tax behavior
77 </Table.HeaderCell>
78 </Table.Row>
79 </Table.Header>
80 <Table.Body>
81 <Table.Row>
82 <Table.Cell>
83
84 Add a line item
85
86 </Table.Cell>
87 <Table.Cell>
88
89 Medusa calculates the tax lines of the new item.
90
91 </Table.Cell>
92 </Table.Row>
93 <Table.Row>
94 <Table.Cell>
95
96 Add a shipping method
97
98 </Table.Cell>
99 <Table.Cell>
100
101 Medusa calculates the tax lines of the new shipping method.
102
103 </Table.Cell>
104 </Table.Row>
105 <Table.Row>
106 <Table.Cell>
107
108 Update the cart's region or locale
109
110 </Table.Cell>
111 <Table.Cell>
112
113 Medusa recalculates the tax lines of all the cart's items and shipping methods.
114
115 </Table.Cell>
116 </Table.Row>
117 <Table.Row>
118 <Table.Cell>
119
120 Update a line item's quantity
121
122 </Table.Cell>
123 <Table.Cell>
124
125 Medusa doesn't recalculate tax lines. Instead, it derives the new tax amounts from the item's existing tax rates.
126
127 </Table.Cell>
128 </Table.Row>
129 <Table.Row>
130 <Table.Cell>
131
132 Remove a line item
133
134 </Table.Cell>
135 <Table.Cell>
136
137 Medusa doesn't recalculate the tax lines of the cart's other items.
138
139 </Table.Cell>
140 </Table.Row>
141 </Table.Body>
142 </Table>
143
144 <Note>
145
146 To force Medusa to recalculate a cart's tax lines, even when `automatic_taxes` is disabled, send a request to the [Calculate Cart Taxes API route](!api!/store/carts/calculate-taxes). This also triggers a fresh calculation from your third-party tax provider, if you use one.
147
148 </Note>
149
150 ---
151
152 ## Retrieve Tax Lines
153
154 When using the Cart and Tax modules together, you can use the `getTaxLines` method of the Tax Module’s main service. It retrieves the tax lines for a cart’s line items and shipping methods.
155
156 ```ts
157 // retrieve the cart
158 const cart = await cartModuleService.retrieveCart("cart_123", {
159 relations: [
160 "items.tax_lines",
161 "shipping_methods.tax_lines",
162 "shipping_address",
163 ],
164 })
165
166 // retrieve the tax lines
167 const taxLines = await taxModuleService.getTaxLines(
168 [
169 ...(cart.items as TaxableItemDTO[]),
170 ...(cart.shipping_methods as TaxableShippingDTO[]),
171 ],
172 {
173 address: {
174 ...cart.shipping_address,
175 country_code:
176 cart.shipping_address.country_code || "us",
177 },
178 }
179 )
180 ```
181
182 Then, use the returned tax lines to set the line items and shipping methods’ tax lines:
183
184 ```ts
185 // set line item tax lines
186 await cartModuleService.setLineItemTaxLines(
187 cart.id,
188 taxLines.filter((line) => "line_item_id" in line)
189 )
190
191 // set shipping method tax lines
192 await cartModuleService.setLineItemTaxLines(
193 cart.id,
194 taxLines.filter((line) => "shipping_line_id" in line)
195 )
196 ```
197
198 ---
199
200 ## Customize Tax Context with Hooks
201
202 The tax line workflows (`updateTaxLinesWorkflow`, `upsertTaxLinesWorkflow`, and `updateOrderTaxLinesWorkflow`) expose a `setTaxLineContext` hook that allows you to add custom context to tax calculations.
203
204 <Note>
205
206 This hook is available since Medusa [v2.16.0](https://github.com/medusajs/medusa/releases/tag/v2.16.0).
207
208 </Note>
209
210 This is useful when you need to pass additional information to your tax provider that isn't included in the default cart or order data.
211
212 For example, create the file `src/workflows/hooks/tax-context.ts` with the following content:
213
214 ```ts title="src/workflows/hooks/tax-context.ts"
215 import { updateTaxLinesWorkflow } from "@medusajs/medusa/core-flows"
216 import { Modules } from "@medusajs/framework/utils"
217
218 updateTaxLinesWorkflow.hooks.setTaxLineContext(
219 async ({ cart, items, shipping_methods }, { container }) => {
220 // Add custom logic to determine tax context
221 const customerModule = container.resolve(Modules.CUSTOMER)
222 const customer = await customerModule.retrieveCustomer(cart.customer_id)
223
224 return {
225 customer_type: customer.metadata?.type || "regular",
226 loyalty_tier: customer.metadata?.loyalty_tier || "bronze",
227 special_exemption: customer.metadata?.tax_exempt || false,
228 }
229 }
230 )
231 ```
232
233 The returned object is passed as `additional_context` to the [tax provider's `getTaxLines` method](/references/tax/ITaxProvider/methods/tax.ITaxProvider.getTaxLines), allowing you to implement custom tax logic based on this context.
234
235 <Note>
236
237 Learn more about workflow hooks in the [Workflow Hooks guide](!docs!/learn/fundamentals/workflows/workflow-hooks).
238
239 </Note>