HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Order form calculator

Describes OrderFormCalculator, which calculates the following for an order form: total, subtotal, handling total, shipping subtotal, order discount total, discount total, order form totals, and tax total in Optimizely Customized Commerce.

Total

Calculates the total for an order form.

public void GetTotal(IOrderForm orderForm, IMarket market, Currency currency, IOrderFormCalculator orderFormCalculator)
          {
            var total = orderFormCalculator.GetTotal(orderForm, market, currency);
            Debug.WriteLine("Total for order form '{0}': {1}", orderForm.OrderFormId, total);
          }

SubTotal

Calculates the total of all shipments in an order form.

public void GetSubTotal(IOrderForm orderForm, Currency currency, IOrderFormCalculator orderFormCalculator)
          {
            var subTotal = orderFormCalculator.GetSubTotal(orderForm, currency);
            Debug.WriteLine("Subtotal for order form '{0}': {1}", orderForm.OrderFormId, subTotal);
          }

HandlingTotal

Calculates the handling total for an order form.

public void GetHandlingTotal(IOrderForm orderForm, Currency currency, IOrderFormCalculator orderFormCalculator)
          {
            var handlingTotal = orderFormCalculator.GetHandlingTotal(orderForm, currency);
            Debug.WriteLine("Handling total for order form '{0}': {1}", orderForm.OrderFormId, handlingTotal);
          }

ShippingSubtotal

Calculates the shipping subtotal for an order form.

public void GetShippingSubTotal(IOrderForm orderForm, IMarket market, Currency currency, IOrderFormCalculator orderFormCalculator)
          {
            var shippingSubTotal = orderFormCalculator.GetShippingSubTotal(orderForm, market, currency);
            Debug.WriteLine("Shipping subtotal for order form '{0}': {1}", orderForm.OrderFormId, shippingSubTotal);
          }

OrderDiscountTotal

Calculates the order form discount amount. It is the total of all order-level discounts of all line items in all shipments in an order form.

public void GetOrderDiscountTotal(IOrderForm orderForm, Currency currency, IOrderFormCalculator orderFormCalculator)
          {
            var orderDiscountTotal = orderFormCalculator.GetOrderDiscountTotal(orderForm, currency);
            Debug.WriteLine("Order discount total for order form '{0}': {1}", orderForm.OrderFormId, orderDiscountTotal);
          }

DiscountTotal

Calculates the order form discount total. It is the total of

  • order-level discount amounts
  • line item discount amounts for all line items in all shipments
  • shipment discount amounts of all shipments in an order form
public void GetDiscountTotal(IOrderForm orderForm, Currency currency, IOrderFormCalculator orderFormCalculator)
          {
            var discountTotal = orderFormCalculator.GetDiscountTotal(orderForm, currency);
            Debug.WriteLine("Discount total for order form '{0}': {1}", orderForm.OrderFormId, discountTotal);
          }

OrderFormTotals

Calculates the order form total.

public void GetOrderFormTotals(IOrderForm orderForm, IMarket market, Currency currency, IOrderFormCalculator orderFormCalculator)
          {
            var orderFormTotals = orderFormCalculator.GetOrderFormTotals(orderForm, market, currency);
        
            Debug.WriteLine("Handling total for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.HandlingTotal);
            Debug.WriteLine("Shipping subtotal for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.ShippingTotal);
            Debug.WriteLine("Tax total for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.TaxTotal);
            Debug.WriteLine("Subtotal for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.SubTotal);
            Debug.WriteLine("Discount total for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.DiscountTotal);
            Debug.WriteLine("Total for order form '{0}': {1}", orderForm.OrderFormId, orderFormTotals.Total);
          }

TaxTotal

Calculates an order form's tax total.

public void GetTaxTotal(IOrderForm orderForm, IMarket market, Currency currency, IOrderFormCalculator orderFormCalculator)
          {
            var taxTotal = orderFormCalculator.GetTaxTotal(orderForm, market, currency);
            Debug.WriteLine("Tax total for order form '{0}': {1}", orderForm.OrderFormId, taxTotal);
          }

Change the default calculation

By inheriting from the default implementation of the interface, DefaultOrderFormCalculator, you can override the calculations. Just override one or more methods in DefaultOrderFormCalculator.

public class OrderFormCalculatorOverridingDefault : DefaultOrderFormCalculator
          {
            public OrderFormCalculatorOverridingDefault(IShippingCalculator shippingCalculator, ITaxCalculator taxCalculator)
              : base(shippingCalculator, taxCalculator)
              { }
            protected override Money CalculateTotal(IOrderForm orderForm, IMarket market, Currency currency)
              {
                return new Money(0, currency);
              }
            protected override Money CalculateSubtotal(IOrderForm orderForm, Currency currency)
              {
                return new Money(0, currency);
              }
            protected override Money CalculateHandlingTotal(IOrderForm orderForm, Currency currency)
              {
                return new Money(0, currency);
              }
            protected override Money CalculateShippingSubTotal(IOrderForm orderForm, IMarket market, Currency currency)
              {
                return new Money(0, currency);
              }
            protected override Money CalculateTaxTotal(IOrderForm orderForm, IMarket market, Currency currency)
              {
                return new Money(0, currency);
              }
          }

Change the default validation

The default implementation validates that the total is not negative after the calculation. To change the behavior, override the appropriate method: ValidateTotal, ValidateSubtotal, ValidateHandlingTotal, ValidateShippingSubtotal, or ValidateTaxTotal.

public class OrderFormCalculatorOverridingDefault : DefaultOrderFormCalculator
      {
        public OrderFormCalculatorOverridingDefault(IShippingCalculator shippingCalculator, ITaxCalculator taxCalculator)
          : base(shippingCalculator, taxCalculator)
          { }
        protected override void ValidateTotal(Money money)
          {
            if (money.Amount <= 0)
              {
                throw new ValidationException("Order total must be greater than 0");
              }
          }
        protected override void ValidateSubtotal(Money money)
          {
        	if (money.Amount <= 0)
              {
        	    throw new ValidationException("Subtotal must be greater than 0");
        	  }
          }
        protected override void ValidateHandlingTotal(Money money)
          {
        	if (money.Amount <= 0)
              {
        	    throw new ValidationException("Handling total must be greater than 0");
        	  }
          }
        protected override void ValidateShippingSubTotal(Money money)
          {
        	if (money.Amount <= 0)
              {
        	    throw new ValidationException("Shipping subtotal must be greater than 0");
        	  }
          }
        protected override void ValidateTaxTotal(Money money)
          {
        	if (money.Amount <= 0)
           	  {
         	    throw new ValidationException("Tax total must be greater than 0");
        	  }
          }
      }