vendure-ecommerce/vendure

Include Address based tax zone strategy in core

martijnvdbrug opened this issue · 0 comments

Is your feature request related to a problem? Please describe.
We have implemented a custom TaxZoneStrategy that determines the taxzone based on the orders shipping (or billing) address. Most of our clients tell us they already expect Vendure to work like this, because 'Why else doe we have tax zones'.

We can easily implement this in each project, but since it's such a common use case, it might make sense to have Vendure core export this strategy?

Describe the solution you'd like

  1. As a developer I would like to be able to set the AddressBasedTaxZoneStrategy in my project:
import { AddressBasedTaxZoneStrategy } from '@vendure/core';

// vendure-config.ts
  taxOptions: {
    taxZoneStrategy: new ShippingBasedTaxZoneStrategy(),
  },

Describe alternatives you've considered
Package it in a plugin.

Additional context
The strategy we use:

import {
  Channel,
  Logger,
  Order,
  RequestContext,
  Zone
} from '@vendure/core';
import { TaxZoneStrategy } from '@vendure/core/dist/config/tax/tax-zone-strategy';

const loggerCtx = 'AddressBasedTaxZoneStrategy';

export class AddressBasedTaxZoneStrategy implements TaxZoneStrategy {

  determineTaxZone(
    ctx: RequestContext,
    zones: Zone[],
    channel: Channel,
    order?: Order
  ): Zone {
    const countryCode = order?.billingAddress?.countryCode ?? order?.shippingAddress?.countryCode;
    if (order && countryCode) {
      const zone = zones.find((zone) =>
        zone.members?.find((member) => member.code === countryCode)
      );
      if (zone) {
        return zone;
      }
      Logger.warn(
        `No tax zone found for country ${countryCode}. Returning default ${channel.defaultTaxZone.name} for order ${order.code}`,
        loggerCtx
      );
    }
    return channel.defaultTaxZone;
  }
}