Hide menu Last updated: Oct 24 2016
Area: Episerver Commerce Applies to versions: 10 and higher

Calculating orders

The following calculation services are available to calculate order totals in different levels.

  • DefaultLineItemCalculator. Calculates extended price.
  • DefaultShippingCalculator. Calculates shipment cost, and total amount for items in shipment.
  • DefaultTaxCalculator. Calculates taxes.
  • DefaultOrderFormCalculator. Calculates totals on an order form.
  • DefaultOrderGroupCalculator. Calculates totals on an order.

Shipment required for correct calculation

When you create an order (cart, purchase order, or payment plan) through the IOrderRepository, a shipment is created. Order calculators calculate only line items that belong to a shipment. This is a changed behavior from the way it worked with workflow activities. 

Calculate all

The IOrderGroupTotalsCalculator makes calculations for the IOrderGroup, its IOrderForms, IShipments, and ILineItems.

  • The order group.
  • The order group's order forms.
  • Shipments on the order forms.
  • Line items on the shipments.

The method calls all calculators to calculate the values.

Line item calculator

The LineItemCalculator calculates a line item's extended price and discounted price.

  • Extended price. Includes order-level discount amount (spread over all line items in the shipment) and any line item discount amount.
public void GetExtendedPrice(ILineItem lineItem, ILineItemCalculator lineItemCalculator)
{
    var extendedPrice = lineItemCalculator.GetExtendedPrice(lineItem, Currency.USD);
    Debug.WriteLine("Extended price for '{0}': {1}", lineItem.Code, extendedPrice);
}

  • Discounted price. Only calculates the price with the "line item discount amount," that is, the discount for a specific item.
public void GetDiscountedPrice(ILineItem lineItem, ILineItemCalculator lineItemCalculator)
{
    var extendedPrice = lineItemCalculator.GetDiscountedPrice(lineItem, Currency.USD);
    Debug.WriteLine("Discounted price for '{0}': {1}", lineItem.Code, extendedPrice);
}

Change the default calculation of extended price

By inheriting from the default implementation of the interface, DefaultLineItemCalculator, you can override the extended price calculation. Just override the CalculateExtendedPrice method.

Change the validation for extended price

The default implementation validates that the extended price is not negative after the calculation. To change the behavior, override the ValidateExtendedPrice method.

Shipping calculator

The ShippingCalculator calculates the shipping cost of an order, order form, and shipment. It also calculates the total amount of line items in the shipment.

Change the default calculation of shipping cost and shipping item total

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

Change the validation

The default implementation validates that the shipment cost is not negative after the calculation. To change the behavior, override the method ValidateShipmentCostForOrderValidateShipmentCostForOrderForm, ValidateShipmentCostForShipment, or ValidateShippingItemTotal, depending on which validation you should override.

Tax calculator

The TaxCalculator calculates the tax cost for the shipment, order, and order form.

Change the default calculation of tax total

By inheriting from the default implementation of the interface, DefaultTaxCalculator, you can override the calculation of tax total. Just override one or several CalculateTaxTotal methods.

Change the validation

The default implementation validates that the tax is not negative after the calculation. To change the behavior, override the ValidateTaxTotalForOrder method or the ValidateTaxTotalForOrderForm method, depending on which validation you should override.

Order form calculator

The OrderFormCalculator calculates the handling total, sub total, and total for an order form.

Change the default calculations

By inheriting from the default implementation of the interface, DefaultOrderFormCalculator, you can override the calculations. Just override one or several of the CalculateHandlingTotalCalculateSubtotal, or CalculateTotal methods.

Change the validation

The default implementation validates that the total is not negative after the calculation. To change the behavior, override the ValidateHandlingTotalValidateSubtotal, or ValidateTotal method, depending on which validation you should override.

Order calculator

The OrderGroupCalculator calculates an order's handling total, sub total, and total.

Change the default calculations

By inheriting from the default implementation of the interface, DefaultOrderGroupCalculator, you override the calculations. Just override one or several CalculateHandlingTotal, CalculateSubtotal, or CalculateTotal methods.

Change the validation

The default implementation validates that the total is not negative after the calculation. To change the behavior, override the ValidateHandlingTotal, ValidateSubtotal, or ValidateTotal method, depending on which validation you should override.

Comments