Calculate Taxes Manually in Checkout
In this document, you’ll learn how to manually calculate taxes during checkout if you have automatic tax calculation disabled in a region.
Overview
By default, taxes are automatically calculated by Medusa during checkout. This behavior can be disabled for a region using the Admin APIs or the Medusa admin to limit the requests being sent to a tax provider.
If you disable this behavior, you must manually trigger taxes calculation. When taxes are calculated, this means that requests will be sent to the tax provider to retrieve the tax rates.
How to Manually Calculate Taxes in Checkout
This section explores different ways you can calculate taxes based on your purpose.
Use Calculate Cart Taxes Endpoint
The Calculate Cart Taxes endpoint forces the calculation of taxes for a cart during checkout. This bypasses the option set in admin to not calculate taxes automatically, which results in sending requests to the tax provider.
This calculates and retrieves the taxes on the cart and each of the line items in that cart.
Use CartService's retrieve Method
The CartService
Copy to Clipboard class has a method retrieve
Copy to Clipboard that gets a cart by ID. In that method, taxes are calculated only if automatic taxes calculation is enabled for the region the cart belongs to.
You can, however, force calculating the taxes of the cart by passing in the third parameter an option containing the key force_taxes
Copy to Clipboard with its value set to true
Copy to Clipboard.
For example:
You can learn how to retrieve and use services in this documentation.
Use CartService's decorateTotals Method
Another way you can use the CartService
Copy to Clipboard to calculate taxes is using the method decorateTotals
Copy to Clipboard:
export default () => {
// ...
router.get("/store/line-taxes", async (req, res) => {
// example of retrieving cart
const cartService = req.scope.resolve("cartService")
const cart = await cartService.retrieve(cart_id)
// ...
// retrieve taxes of line items
const data = await decorateTotals(cart, {
force_taxes: true,
})
return res.status(200).json({ cart: data })
})
}
The decorateTotals
Copy to Clipboard method accepts the cart as a first parameter and an options object as a second parameter. If you set force_taxes
Copy to Clipboard to true
Copy to Clipboard in that object, the totals of the line items can be retrieved regardless of whether the automatic calculation is enabled in the line item’s region.
Use TotalsService
You can calculate and retrieve taxes of line items using the getLineItemTotals
Copy to Clipboard method available in the TotalService
Copy to Clipboard class. All you need to do is pass in the third argument to that method an options object with the key include_tax
Copy to Clipboard set to true:
You can learn how to retrieve and use services in this documentation.