medusa
medusa copied to clipboard
Context customer object is undefined in getTaxLines
Bug report
Context customer object is undefined in getTaxLines on retrieving.
Describe the bug
I have a custom AbstractTaxService that checks if the customer is a B2B user. If they are, it sets all item rates to 0%; otherwise, it stays with the default rates. I check this using the context argument of the getTaxLines function. The problem arises when I add a product to the cart – it normally returns the values of context.customer object. However, when I try to retrieve my cart, it returns undefined, resetting all taxes to the default rate. I'm unsure if I'm doing something wrong or if it might be a bug.
System information
Medusa version (including plugins): 1.20.0 Node.js version: 20.10.0 Database: Postgres 14 Operating system: Ubuntu 22.04 Browser (if relevant): Brave
Steps to reproduce the behavior
Just retrieve cart with custom AbstractTaxService.
Expected behavior
On retrieving cart it should returns valid context.customer field in getTaxLines function.
Code snippets
async getTaxLines(
itemLines: ItemTaxCalculationLine[],
shippingLines: ShippingTaxCalculationLine[],
context: TaxCalculationContext
): Promise<ProviderTaxLine[]> {
let is_b2b = false
if (context.customer) {
is_b2b = context.customer.metadata?.company !== undefined
}
console.log('for b2b', is_b2b) // is_b2b only true on adding line item, on retrieving it's always false
let taxLines: ProviderTaxLine[] = itemLines.flatMap((l) => {
return l.rates.map((r) => ({
rate: is_b2b ? 0 : r.rate || 0,
name: r.name,
code: is_b2b ? 'zero_tax' : 'default_tax',
item_id: l.item.id,
}))
})
taxLines = taxLines.concat(
shippingLines.flatMap((l) => {
return l.rates.map((r) => ({
rate: r.rate || 0,
name: r.name,
code: r.code,
shipping_method_id: l.shipping_method.id,
}))
})
)
return taxLines
}