Class DefaultLineItemCalculator

Line item calculator.

Inheritance
System.Object
DefaultLineItemCalculator
Implements
Inherited Members
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
Namespace: EPiServer.Commerce.Order.Calculator
Assembly: EPiServer.Business.Commerce.dll
Version: 10.8.0
Syntax
public class DefaultLineItemCalculator : ILineItemCalculator
Examples
    /// <summary>
/// Sample implementation of <see cref="ILineItemCalculator"/>.
/// </summary>
public class LineItemCalculatorSample : ILineItemCalculator
{
/// <summary>
/// The tax calculator needed for calculating sales tax of an <see cref="ILineItem"/>.
/// </summary>
private readonly ITaxCalculator _taxCalculator;

public LineItemCalculatorSample(ITaxCalculator taxCalculator)
{
_taxCalculator = taxCalculator;
}

public Money GetExtendedPrice(ILineItem lineItem, Currency currency)
{
//The extended price of a line item is the price that includes all discounts applied on the line item, including entry level and order level discounts.
//Of course you might have different calculation here.
var rawExtendedPrice = lineItem.PlacedPrice * lineItem.Quantity - lineItem.GetDiscountTotal(currency).Amount;

// If the total discount amount is greater than the line item price, it means that with the discounts, customers don't need to pay for the item.
// It also means that the extended price of the line item is zero.
return new Money(Math.Max(0, rawExtendedPrice), currency);
}

public Money GetDiscountedPrice(ILineItem lineItem, Currency currency)
{
//The discounted price of a line item is the price that includes all entry level discounts applied on the line item. That does not take order level discounts into account.
//Of course you might have different calculation here.
var rawDiscountedPrice = lineItem.PlacedPrice * lineItem.Quantity - lineItem.GetEntryDiscount();

// If the discount amount is greater than the line item price, it means that with the discount, customers don't need to pay for the item.
// It also means that the discounted price of the line item is zero.
return new Money(Math.Max(0, rawDiscountedPrice), currency);
}

/// <summary>
/// Gets the extended and the discounted prices of an <see cref="ILineItem"/>.
/// These prices are wrapped in a <see cref="LineItemPrices"/> object.
/// </summary>
/// <param name="lineItem">The given line item.</param>
/// <param name="currency">The currency.</param>
public LineItemPrices GetLineItemPrices(ILineItem lineItem, Currency currency)
{
var extendedPrice = GetExtendedPrice(lineItem, currency);
var discountedPrice = GetDiscountedPrice(lineItem, currency);

return new LineItemPrices(extendedPrice, discountedPrice);
}

public Money GetSalesTax(ILineItem lineItem, IMarket market, Currency currency, IOrderAddress shippingAddress)
{
return _taxCalculator.GetSalesTax(lineItem, market, shippingAddress, GetExtendedPrice(lineItem, currency));
}

public Money GetSalesTax(IEnumerable<ILineItem> lineitems, IMarket market, Currency currency,
IOrderAddress shippingAddress)
{
return _taxCalculator.GetSalesTax(lineitems, market, shippingAddress, currency);
}
}

Constructors

DefaultLineItemCalculator()

Declaration
public DefaultLineItemCalculator()

Methods

CalculateExtendedPrice(ILineItem, Currency)

Calculate the extended price for a line item.

Declaration
protected virtual Money CalculateExtendedPrice(ILineItem lineItem, Currency currency)
Parameters
Type Name Description
ILineItem lineItem

The line item to calculate the extended price for.

Currency currency

The currency to be used in the calculations

Returns
Type Description
Money

The extended price

Remarks

Extended price formula: PlacedPrice * Quantity - LineItemDiscountAmount - OrderLevelDiscountAmount

Examples

GetDiscountedPrice(ILineItem, Currency)

Gets the line item discount price.

Declaration
public Money GetDiscountedPrice(ILineItem lineItem, Currency currency)
Parameters
Type Name Description
ILineItem lineItem

The line item.

Currency currency

The currency.

Returns
Type Description
Money

The line item discounted price.

GetExtendedPrice(ILineItem, Currency)

Gets the extended price for a line item.

Declaration
public Money GetExtendedPrice(ILineItem lineItem, Currency currency)
Parameters
Type Name Description
ILineItem lineItem

The line item to calculate the extended price for.

Currency currency

The currency to be used in the calculations

Returns
Type Description
Money

The extended price

Remarks

Extended price formula: PlacedPrice * Quantity - LineItemDiscountAmount - OrderLevelDiscountAmount

Examples
Exceptions
Type Condition
System.Exception

Thrown when calculation fails

ValidateExtendedPrice(Money)

Validates the extended price

Declaration
protected virtual void ValidateExtendedPrice(Money money)
Parameters
Type Name Description
Money money

The calculated value

Implements