Loading...

Last updated: Apr 16 2018

Area: Episerver Commerce Applies to versions: 12 and higher
Other versions:

Shipping calculator

The ShippingCalculator calculates the shipping cost, shipping taxes, and total amount of line items in shipment.

  • Shipping cost. Calculates the shipment's shipping cost.
    public void GetShippingCost(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
    {
        var shippingCost = shippingCalculator.GetShippingCost(shipment, market, currency);
        Debug.WriteLine("Shipping cost for shipment '{0}': {1}", shipment.ShipmentId, shippingCost);
    }
  • Discounted shipping amount. Calculates shipping cost including shipping discount.
    public void GetDiscountedShippingAmount(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
    {
        var shippingDiscountedAmount = shippingCalculator.GetDiscountedShippingAmount(shipment, market, currency);
        Debug.WriteLine("Shipping discounted amount for shipment '{0}': {1}", shipment.ShipmentId, shippingDiscountedAmount);
    }
  • Shipping items total. Calculates the subtotal for all line items in the shipment. It is the sum of the discounted price off all line items in the shipment excluding gift items.
    public void GetShippingItemsTotal(IShipment shipment, Currency currency, IShippingCalculator shippingCalculator)
    {
        var shipmentSubtotal = shippingCalculator.GetShippingItemsTotal(shipment, currency);
        Debug.WriteLine("Subtotal for shipment '{0}': {1}", shipment.ShipmentId, shipmentSubtotal);
    }
  • Shipping return items total. Calculates the subtotal for all return line items in the shipment. It is the sum of discounted price of all return line items in the shipment excluding gift items.
    public void GetShippingReturnItemsTotal(IShipment shipment, Currency currency, IShippingCalculator shippingCalculator)
    {
        var shippingItemTotal = shippingCalculator.GetShippingReturnItemsTotal(shipment, currency);
        Debug.WriteLine("Total prices of all return line items for shipment '{0}': {1}", shipment.ShipmentId, shippingItemTotal);
    }
  • Shipping totals. Calculates the shipping totals.
    public void GetShippingTotals(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
    {
        var shippingTotals = shippingCalculator.GetShippingTotals(shipment, market, currency);
        Debug.WriteLine("Subtotal for shipment '{0}': {1}", shipment.ShipmentId, shippingTotals.ItemsTotal);
        Debug.WriteLine("Shipping cost for shipment '{0}': {1}", shipment.ShipmentId, shippingTotals.ShippingCost);
        Debug.WriteLine("Shipping tax for shipment '{0}': {1}", shipment.ShipmentId, shippingTotals.ShippingTax);
    }
  • Shipping tax. Calculates a shipment's shipping tax.
    public void GetShippingTax(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
    {
        var shippingTax = shippingCalculator.GetShippingTax(shipment, market, currency);
        Debug.WriteLine("Shipping tax for shipment '{0}': {1}", shipment.ShipmentId, shippingTax);
    }
  • Sales tax. Calculates a shipment's sales tax, based on shipping address.
    public void GetSalesTax(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
    {
        var salesTax = shippingCalculator.GetSalesTax(shipment, market, currency);
        Debug.WriteLine("Sales tax for shipment '{0}': {1}", shipment.ShipmentId, salesTax);
    }
  • Return shipping tax. Calculates a shipment's return shipping tax.
    public void GetReturnShippingTax(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
    {
        var returnShippingTax = shippingCalculator.GetReturnShippingTax(shipment, market, currency);
        Debug.WriteLine("Shipping tax for return shipment '{0}': {1}", shipment.ShipmentId, returnShippingTax);
    }
  • Return sales tax. Calculates the sales tax for a return shipment that contains return line items.
    public void GetReturnShippingTax(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
    {
        var returnShippingTax = shippingCalculator.GetReturnShippingTax(shipment, market, currency);
        Debug.WriteLine("Shipping tax for return shipment '{0}': {1}", shipment.ShipmentId, returnShippingTax);
    }

Changing the default calculation

By inheriting from the default implementation of the interface, DefaultShippingCalculator, you can override calculation methods. To change the calculation of shipping cost, override one or several CalculateShippingCost methods. To change the calculation of shipping items' total, override the CalculateShippingItemsTotal method. And override other methods to change the tax calculation.

Changing the default validation

The default implementation validates that all return values are not negative after the calculation. To change the behavior, override the appropriate method: ValidateShipmentCostForShipment, ValidateShippingItemTotal, ValidateShipping Tax, or ValidateSalesTax.


Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/