HomeDev GuideAPI Reference
Dev Guide

Order group calculator

Describes OrderGroupCalculator, which calculates an order's total, subtotal, handling total, shipping subtotal, order discount total, order group total, and tax total in Optimizely Customized Commerce.

Total

Calculates the total for an order.

``````public void GetTotal(IOrderGroup orderGroup, IOrderGroupCalculator orderGroupCalculator)
{
var total = orderGroupCalculator.GetTotal(orderGroup);
Debug.WriteLine("Total for order group '{0}': {1}", orderGroup.OrderLink.OrderGroupId, total);
}
``````

SubTotal

Calculates the subtotal of all order forms for an order.

``````public void GetSubTotal(IOrderGroup orderGroup, IOrderGroupCalculator orderGroupCalculator)
{
var subTotal = orderGroupCalculator.GetSubTotal(orderGroup);
Debug.WriteLine("Subtotal for order group '{0}': {1}", orderGroup.OrderLink.OrderGroupId, subTotal);
}
``````

HandlingTotal

Calculates an order's handling total.

``````public void GetHandlingTotal(IOrderGroup orderGroup, IOrderGroupCalculator orderGroupCalculator)
{
var handlingTotal = orderGroupCalculator.GetHandlingTotal(orderGroup);
Debug.WriteLine("Handling total for order group '{0}': {1}", orderGroup.OrderLink.OrderGroupId, handlingTotal);
}
``````

ShippingSubTotal

CalculatesÂ an order's shipping subtotal.

``````public void GetShippingSubTotal(IOrderGroup orderGroup, IOrderGroupCalculator orderGroupCalculator)
{
var shippingSubTotal = orderGroupCalculator.GetShippingSubTotal(orderGroup);
Debug.WriteLine("Shipping subtotal for order group '{0}': {1}", orderGroup.OrderLink.OrderGroupId, shippingSubTotal);
}
``````

OrderDiscountTotal

Calculates the order group discount amount. It is the total of order-level discounts of all line items in all shipments in all order forms of an order group.

``````public void GetOrderDiscountTotal(IOrderGroup orderGroup, IOrderGroupCalculator orderGroupCalculator)
{
var orderDiscountTotal = orderGroupCalculator.GetOrderDiscountTotal(orderGroup);
Debug.WriteLine("Order discount total for order group '{0}': {1}", orderGroup.OrderLink.OrderGroupId, orderDiscountTotal);
}
``````

OrderGroupTotals

Calculates order group totals.

``````public void GetOrderGroupTotals(IOrderGroup orderGroup, IOrderGroupCalculator orderGroupCalculator)
{
var orderGroupTotals = orderGroupCalculator.GetOrderGroupTotals(orderGroup);

Debug.WriteLine("Handling total for order group '{0}': {1}", orderGroup.OrderLink.OrderGroupId, orderGroupTotals.HandlingTotal);
Debug.WriteLine("Shipping subtotal for order group '{0}': {1}", orderGroup.OrderLink.OrderGroupId, orderGroupTotals.ShippingTotal);
Debug.WriteLine("Tax total for order group '{0}': {1}", orderGroup.OrderLink.OrderGroupId, orderGroupTotals.TaxTotal);
Debug.WriteLine("Subtotal for order group '{0}': {1}", orderGroup.OrderLink.OrderGroupId, orderGroupTotals.SubTotal);
Debug.WriteLine("Total for order group '{0}': {1}", orderGroup.OrderLink.OrderGroupId, orderGroupTotals.Total);
}
``````

TaxTotal

Calculates the tax total for the order.

``````public void GetTaxTotal(IOrderGroup orderGroup, IOrderGroupCalculator orderGroupCalculator)
{
var taxTotal = orderGroupCalculator.GetTaxTotal(orderGroup);
Debug.WriteLine("Tax total for order group '{0}': {1}", orderGroup.OrderLink.OrderGroupId, taxTotal);
}
``````

Change the default calculation

By inheriting from the default implementation of the interface,Â DefaultOrderGroupCalculator,Â you override the calculations. Just override one or more methods inÂ DefaultOrderGroupCalculator.

``````public class OrderGroupCalculatorOverridingDefault : DefaultOrderGroupCalculator
{
public OrderGroupCalculatorOverridingDefault(
IOrderFormCalculator orderFormCalculator,
IReturnOrderFormCalculator returnOrderFormCalculator,
IMarketService marketService)
: base(orderFormCalculator, returnOrderFormCalculator, marketService)
{ }

protected override Money CalculateTotal(IOrderGroup orderGroup)
{
return new Money(0, orderGroup.Currency);
}

protected override Money CalculateSubTotal(IOrderGroup orderGroup)
{
return new Money(0, orderGroup.Currency);
}

protected override Money CalculateHandlingTotal(IOrderGroup orderGroup)
{
return new Money(0, orderGroup.Currency);
}

protected override Money CalculateShippingSubTotal(IOrderGroup orderGroup)
{
return new Money(0, orderGroup.Currency);
}

protected override Money CalculateTaxTotal(IOrderGroup orderGroup)
{
return new Money(0, orderGroup.Currency);
}
}
``````

Â Change the default validation

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

``````public class OrderGroupCalculatorOverridingDefault : DefaultOrderGroupCalculator
{
public OrderGroupCalculatorOverridingDefault(
IOrderFormCalculator orderFormCalculator,
IReturnOrderFormCalculator returnOrderFormCalculator,
IMarketService marketService)
: base(orderFormCalculator, returnOrderFormCalculator, marketService)
{ }

protected override void ValidateTotal(Money money)
{
if (money.Amount <= 0)
{
throw new ValidationException("Total must be greater than 0");
}
}

protected override void ValidateSubTotal(Money money)
{
if (money.Amount <= 0)
{
throw new ValidationException("Subtotal must be greater than 0");
}
}

protected override void ValidateHandlingTotal(Money money)
{
if (money.Amount <= 0)
{
throw new ValidationException("Handling total must be greater than 0");
}
}

protected override void ValidateShippingSubTotal(Money money)
{
if (money.Amount <= 0)
{
throw new ValidationException("Shipping subtotal must be greater than 0");
}
}

protected override void ValidateTaxTotal(Money money)
{
if (money.Amount <= 0)
{
throw new ValidationException("Tax total must be greater than 0");
}
}
}
``````