November Happy Hour will be moved to Thursday December 5th.

Interface IOrderGroupCalculator

The calculator calculates totals on the order group.

Namespace: EPiServer.Commerce.Order
Assembly: Mediachase.Commerce.dll
Version: 12.17.2
Syntax
public interface IOrderGroupCalculator
Examples
    /// <summary>
/// Sample implementation of <see cref="IOrderGroupCalculator"/>.
/// </summary>
public class OrderGroupCalculatorSample : IOrderGroupCalculator
{
private readonly IOrderFormCalculator _orderFormCalculator;
private readonly IReturnOrderFormCalculator _returnOrderFormCalculator;
private readonly IMarketService _marketService;

public OrderGroupCalculatorSample(
IOrderFormCalculator orderFormCalculator,
IReturnOrderFormCalculator returnOrderFormCalculator,
IMarketService marketService)
{
_orderFormCalculator = orderFormCalculator;
_returnOrderFormCalculator = returnOrderFormCalculator;
_marketService = marketService;
}

public Money GetTotal(IOrderGroup orderGroup)
{
var currency = orderGroup.Currency;
var result = GetSubTotal(orderGroup) +
    GetHandlingTotal(orderGroup) +
    GetShippingSubTotal(orderGroup) -
    GetOrderDiscountTotal(orderGroup) -
    orderGroup.GetShippingDiscountTotal();

if (!orderGroup.PricesIncludeTax)
{
    result += GetTaxTotal(orderGroup);
}

return result;
}

public Money GetSubTotal(IOrderGroup orderGroup)
{
var currency = orderGroup.Currency;

//The subtotal of an order group is the total of the subtotal amount of all order forms in the order group.
var amount = orderGroup.Forms.Sum(form => _orderFormCalculator.GetSubTotal(form, currency).Amount);

return new Money(amount, currency);
}

public Money GetHandlingTotal(IOrderGroup orderGroup)
{
var currency = orderGroup.Currency;

//The handling total of an order group is the total of handling total amount of all order forms in the order group.
var amount = orderGroup.Forms.Sum(form => _orderFormCalculator.GetHandlingTotal(form, currency).Amount);

return new Money(amount, currency);
}

public Money GetShippingSubTotal(IOrderGroup orderGroup)
{
var currency = orderGroup.Currency;
var market = _marketService.GetMarket(orderGroup.MarketId);

//The shipping subtotal of an order group is the total of shipping sub total amount of all order forms in the order group.
//i.e. it's the total of shipping cost of all shipments in the order group.
var shippingSubTotal = orderGroup.Forms.Sum(form => _orderFormCalculator.GetShippingSubTotal(form, market, currency).Amount);

return new Money(shippingSubTotal, currency);
}

[Obsolete("Use the overload without Currency instead.")]
public Money GetOrderDiscountTotal(IOrderGroup orderGroup, Currency currency)
{
return GetOrderDiscountTotal(orderGroup);
}

public Money GetOrderDiscountTotal(IOrderGroup orderGroup)
{
//The order discount total of an order group is the total of all order level discounts applied on all line items in the order group.
var orderDiscountTotal = orderGroup.GetAllLineItems().Sum(lineItem =>
{
    var orderDiscountAmount = 0m;

    var lineItemDiscountAmount = lineItem as ILineItemDiscountAmount;
    if (lineItemDiscountAmount != null)
    {
        orderDiscountAmount = lineItemDiscountAmount.OrderAmount;
    }

    return orderDiscountAmount;
});

return new Money(orderDiscountTotal, orderGroup.Currency);
}

public OrderGroupTotals GetOrderGroupTotals(IOrderGroup orderGroup)
{
var market = _marketService.GetMarket(orderGroup.MarketId);
var currency = orderGroup.Currency;

var subTotal = GetSubTotal(orderGroup);
var total = GetTotal(orderGroup);
var shippingTotal = GetShippingSubTotal(orderGroup);
var taxTotal = GetTaxTotal(orderGroup);
var handlingTotal = GetHandlingTotal(orderGroup);

var formTotalsDictionary = orderGroup.Forms.ToDictionary(form => form, form => _orderFormCalculator.GetOrderFormTotals(form, market, currency));
return new OrderGroupTotals(subTotal, total, shippingTotal, taxTotal, handlingTotal, formTotalsDictionary);
}

public Money GetTaxTotal(IOrderGroup orderGroup)
{
var currency = orderGroup.Currency;
var market = _marketService.GetMarket(orderGroup.MarketId);

//The tax total of an order group is the total of tax amount of all order forms in the order group.
var taxTotal = orderGroup.Forms.Sum(form => _orderFormCalculator.GetTaxTotal(form, market, currency).Amount);

return new Money(taxTotal, currency);
}
}

Methods

GetHandlingTotal(IOrderGroup)

Gets the handling total of an IOrderGroup.

Declaration
Money GetHandlingTotal(IOrderGroup orderGroup)
Parameters
Type Name Description
IOrderGroup orderGroup

The order group.

Returns
Type Description
Money

The handling total for the order group.

Examples
        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);
}

GetOrderDiscountTotal(IOrderGroup)

Gets the total of all order level discounts applied on an IOrderGroup.

Declaration
Money GetOrderDiscountTotal(IOrderGroup orderGroup)
Parameters
Type Name Description
IOrderGroup orderGroup

The order group.

Returns
Type Description
Money

The order level discount total of the order.

Remarks

There're three types of discount can be applied to an order: order level, shipment level (or shipping discount) and line item level discount. This only returns order level discount.

Examples
        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);
}

GetOrderDiscountTotal(IOrderGroup, Currency)

Gets the order discount price.

Declaration
[Obsolete("This method is no longer used, use the overload without Currency instead. Will remain at least until May 2019.")]
Money GetOrderDiscountTotal(IOrderGroup orderGroup, Currency currency)
Parameters
Type Name Description
IOrderGroup orderGroup

The order group.

Currency currency

The currency.

Returns
Type Description
Money

The order discount price.

GetOrderGroupTotals(IOrderGroup)

Gets the order group totals of an IOrderGroup.

Declaration
OrderGroupTotals GetOrderGroupTotals(IOrderGroup orderGroup)
Parameters
Type Name Description
IOrderGroup orderGroup

The given order group.

Returns
Type Description
OrderGroupTotals

An order group totals.

Examples
        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);
}

GetShippingSubTotal(IOrderGroup)

Gets the total of shipping cost of all shipments in an IOrderGroup.

Declaration
Money GetShippingSubTotal(IOrderGroup orderGroup)
Parameters
Type Name Description
IOrderGroup orderGroup

The given order group.

Returns
Type Description
Money

The shipping subtotal of the order group.

Examples
        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);
}

GetSubTotal(IOrderGroup)

Gets the subtotal of an IOrderGroup.

Declaration
Money GetSubTotal(IOrderGroup orderGroup)
Parameters
Type Name Description
IOrderGroup orderGroup

The given order group.

Returns
Type Description
Money

The subtotal of the order group.

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

GetTaxTotal(IOrderGroup)

Gets the tax total of an IOrderGroup.

Declaration
Money GetTaxTotal(IOrderGroup orderGroup)
Parameters
Type Name Description
IOrderGroup orderGroup

The given order group.

Returns
Type Description
Money

The tax total of the order group.

Examples
        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);
}

GetTotal(IOrderGroup)

Gets the total of an IOrderGroup.

Declaration
Money GetTotal(IOrderGroup orderGroup)
Parameters
Type Name Description
IOrderGroup orderGroup

The given order group.

Returns
Type Description
Money

The total of the order group.

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