Interface ILineItemCalculator
Calculates the extended price for a ILineItem.
Namespace: EPiServer.Commerce.Order
Assembly: Mediachase.Commerce.dll
Version: 13.30.0Syntax
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 discounted price of an ILineItem.
Declaration
Money GetDiscountedPrice(ILineItem lineItem, Currency currency)
Parameters
Type | Name | Description |
---|---|---|
ILineItem | lineItem | The line item. |
Currency | currency | The currency to be used in the calculations. |
Returns
Type | Description |
---|---|
Money | The discounted price of the line item. |
Examples
public void GetDiscountedPrice(ILineItem lineItem, Currency currency, ILineItemCalculator lineItemCalculator)
{
var discountedPrice = lineItemCalculator.GetDiscountedPrice(lineItem, currency);
Debug.WriteLine("Discounted price for '{0}': {1}", lineItem.Code, discountedPrice);
}
GetExtendedPrice(ILineItem, Currency)
Gets the extended price of an ILineItem.
Declaration
Money GetExtendedPrice(ILineItem lineItem, Currency currency)
Parameters
Type | Name | Description |
---|---|---|
ILineItem | lineItem | The line item. |
Currency | currency | The currency to be used in the calculations. |
Returns
Type | Description |
---|---|
Money | The rounded extended price of the line item. |
Examples
public void GetExtendedPrice(ILineItem lineItem, Currency currency, ILineItemCalculator lineItemCalculator)
{
var extendedPrice = lineItemCalculator.GetExtendedPrice(lineItem, currency);
Debug.WriteLine("Extended price for '{0}': {1}", lineItem.Code, extendedPrice);
}
GetLineItemPrices(ILineItem, Currency)
Gets the extended and discounted prices of an ILineItem.
Declaration
LineItemPrices GetLineItemPrices(ILineItem lineItem, Currency currency)
Parameters
Type | Name | Description |
---|---|---|
ILineItem | lineItem | The line item. |
Currency | currency | The currency to be used in the calculations. |
Returns
Type | Description |
---|---|
LineItemPrices | The prices for the line item. |
Examples
public void GetLineItemPrices(ILineItem lineItem, Currency currency, ILineItemCalculator lineItemCalculator)
{
var lineItemdPrices = lineItemCalculator.GetLineItemPrices(lineItem, currency);
Debug.WriteLine("Extended price for '{0}': {1}", lineItem.Code, lineItemdPrices.ExtendedPrice);
Debug.WriteLine("Discounted price for '{0}': {1}", lineItem.Code, lineItemdPrices.DiscountedPrice);
}
GetSalesTax(ILineItem, IMarket, Currency, IOrderAddress)
Gets the sales tax of an ILineItem.
Declaration
Money GetSalesTax(ILineItem lineItem, IMarket market, Currency currency, IOrderAddress shippingAddress)
Parameters
Type | Name | Description |
---|---|---|
ILineItem | lineItem | The line item. |
IMarket | market | The market to be used in the calculation. |
Currency | currency | The currency to be used in the calculations. |
IOrderAddress | shippingAddress | The shipping address to be used in the calculations. |
Returns
Type | Description |
---|---|
Money | The sales tax of the line item. |
Examples
public void GetSalesTax(ILineItem lineItem, IMarket market, Currency currency, IOrderAddress shippingAddress, ILineItemCalculator lineItemCalculator)
{
var salesTax = lineItemCalculator.GetSalesTax(lineItem, market, currency, shippingAddress);
Debug.WriteLine("Sales tax for '{0}': {1}", lineItem.Code, salesTax);
}
GetSalesTax(IEnumerable<ILineItem>, IMarket, Currency, IOrderAddress)
Gets the sales tax of a collection of ILineItems.
Declaration
Money GetSalesTax(IEnumerable<ILineItem> lineItems, IMarket market, Currency currency, IOrderAddress shippingAddress)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<ILineItem> | lineItems | The line items. |
IMarket | market | The market to be used in the calculation. |
Currency | currency | The currency to be used in the calculations. |
IOrderAddress | shippingAddress | The shipping address to be used in the calculations. |
Returns
Type | Description |
---|---|
Money | The sales tax for the line item. |
Examples
public void GetSalesTax(IEnumerable<ILineItem> lineItems, IMarket market, Currency currency, IOrderAddress shippingAddress, ILineItemCalculator lineItemCalculator)
{
var salesTax = lineItemCalculator.GetSalesTax(lineItems, market, currency, shippingAddress);
}