Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nick Hoang 51 posts 180 karma points
    Mar 23, 2023 @ 16:46
    Nick Hoang
    0

    Order tax rate is not accurate

    Hi Matt,

    I'm using Vendr v1.8.6 to build an eCommerce website with multiple warehouses, the warehouses have different tax rate and currency so I would like to dynamic generate tax rate of the orders.

    I've implemented a custom OrderCalculator like this:

    public class CustomOrderCalculator : IOrderCalculator
    {        
        public OrderCalculation CalculateOrder(OrderReadOnly order)
        {
            OrderCalculation calculation = new OrderCalculation(order.CurrencyId);
    
            if (order.Properties["taxrate"] != null && decimal.TryParse(order.Properties["taxrate"].Value, out decimal taxRate))
            {
                if (taxRate > 0)
                {                    
                    calculation.TaxRate = new TaxRate(taxRate);                    
                }
            }
            Pipeline.Invoke<CalculateOrderPipeline, OrderCalculationPipelineArgs, OrderCalculation>((IUnitOfWork uow) => new OrderCalculationPipelineArgs(uow, calculation, order));
            return calculation;            
        }
    }
    

    (The 'taxrate' will be add to the order as a propety)

    But it didn't work as I expected, the order's tax rate didn't change, still getting from the standard tax class that I defined before.

    Could you correct me if I'm doing wrong?

    Thanks,Nick

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Mar 23, 2023 @ 17:02
    Matt Brailsford
    100

    I think the problem is that you invoke the calculation pipeline which has a task in to to calculate the tax rate so it's just overriding your tax rate.

    You might just be better off leaving the order calculator as the default and instead replace the CalculateOrderTaxRateTask in the pipeline.

    public class MyCalculateOrderTaxRateTask : PipelineTaskWithTypedArgsBase<OrderCalculationPipelineArgs, OrderCalculation>
    {
        public override PipelineResult<OrderCalculation> Execute(OrderCalculationPipelineArgs args)
        {
            if (CONDITION) {
                args.Calculation.TaxRate = {YOUR CALC HERE};
            }
    
            return Ok(args.Model);
        }
    }
    

    then in your startup script

    builder.WithCalculateOrderTaxRatesPipeline()
        .InsertAfter<CalculateOrderTaxRateTask, MyCalculateOrderTaxRateTask>();
    

    NB I can't fully remember if this is the API for v1 but this is the general idea

  • Nick Hoang 51 posts 180 karma points
    Mar 24, 2023 @ 01:04
    Nick Hoang
    0

    Thanks Matt, It works as a charm

    Cheers, Nick

Please Sign in or register to post replies

Write your reply to:

Draft