HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

Line item calculator

This topic describes LineItemCalculator, which calculates a line item's extended price, discounted price, line item price and sales tax.

  • 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)
          {
            return lineItemCalculator.GetSalesTax(lineitems, market, shippingAddress, currency);
          }

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);
          }
    
        protected override Money CalculateSalesTax(ILineItem lineItem, IMarket market, Currency currency, IOrderAddress shippingAddress)
          {
            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");
              }
          }
      }

What’s Next
Did this page help you?