Try our conversational search powered by Generative AI!

Interface ILineItemCalculator

Calculates the extended price for a ILineItem

Namespace: EPiServer.Commerce.Order
Assembly: Mediachase.Commerce.dll
Version: 10.8.0
Syntax
public interface 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);
}
}

Methods

GetDiscountedPrice(ILineItem, Currency)

Gets the line item discounted price.

Declaration
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 the lineitem

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

Represents a line item in the system, the actual item that is bought.

Currency currency

The currency to be used in the calculations

Returns
Type Description
Money

The extended price

Examples