HomeDev GuideAPI Reference

# Line item calculator

Describes LineItemCalculator, which calculates a line item's extended price, discounted price, line item price, and sales tax in Optimizely Customized Commerce 13.

• Extended price â€“ Calculates the price including order-level discount amount (spreadÂ over all line items in the shipment) and any line item discount amount.

``````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);
}
``````
• Discounted price â€“ Only calculates the price with the "line item discount amount," that is, the discount for a specific line item.

``````public void GetDiscountedPrice(ILineItem lineItem, Currency currency, ILineItemCalculator lineItemCalculator)
{
var discoutedPrice = lineItemCalculator.GetDiscountedPrice(lineItem, currency);
Debug.WriteLine("Discounted price for '{0}': {1}", lineItem.Code, discountedPrice);
}
``````
• Line item price â€“ Calculates the line item price, which includesÂ a line item's extended price and discounted price.

``````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);
}
``````
• Sales tax â€“ Calculates a line item's sales tax.

``````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);
}
``````
• Sales tax â€“ Calculates line items' sales tax.

``````public Money GetSalesTax(IEnumerable<ILineItem> lineitems, IMarket market, Currency currency, IOrderAddress shippingAddress, ILineItemCalculator lineItemCalculator)
{
}
``````

## Change the default calculation

By inheriting from the default implementation of the interface, DefaultLineItemCalculator,Â you canÂ override the extended price and sales tax calculations. To change the calculation of a line item's extended price, override theÂ CalculateExtendedPrice method. To change the calculation of line item's sales tax, override the CalculateSalesTax method.

``````public class LineItemCalculatorSample : DefaultLineItemCalculator
{
protected override Money CalculateExtendedPrice(ILineItem lineItem, Currency currency)
{
return new Money(0, currency);
}

{
return new Money(0, currency);
}
}
``````

## Change the default validation

The default implementation validates that the extended price and sales tax are not negative after the calculation. To change the behavior, override the ValidateExtendedPrice and ValidateSalesTax methods.

``````public class LineItemCalculatorSample : DefaultLineItemCalculator
{
protected override void ValidateExtendedPrice(Money money)
{
if (money.Amount <= 0)
{
throw new ValidationException("Extended price must be greater than 0");
}
}

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