HomeDev GuideAPI Reference
Dev Guide

# Shipping calculator

Describes ShippingCalculator in Optimizely Customized Commerce 13, which calculates the shipping cost, shipping taxes, and total amount of line items in shipment.

• Shipping cost â€“ CalculatesÂ the shipment's shipping cost.

``````public void GetShippingCost(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
{
var shippingCost = shippingCalculator.GetShippingCost(shipment, market, currency);
Debug.WriteLine("Shipping cost for shipment '{0}': {1}", shipment.ShipmentId, shippingCost);
}
``````
• Discounted shipping amount â€“ Calculates shipping cost including shipping discount.

``````public void GetDiscountedShippingAmount(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
{
var shippingDiscountedAmount = shippingCalculator.GetDiscountedShippingAmount(shipment, market, currency);
Debug.WriteLine("Shipping discounted amount for shipment '{0}': {1}", shipment.ShipmentId, shippingDiscountedAmount);
}
``````
• Shipping items total â€“ Calculates the subtotal for all line items in the shipment. It is the sum of the discounted price off all line items in the shipment excluding gift items.

``````public void GetShippingItemsTotal(IShipment shipment, Currency currency, IShippingCalculator shippingCalculator)
{
var shipmentSubtotal = shippingCalculator.GetShippingItemsTotal(shipment, currency);
Debug.WriteLine("Subtotal for shipment '{0}': {1}", shipment.ShipmentId, shipmentSubtotal);
}
``````
• Shipping return items total â€“ Calculates the subtotal for all return line items in the shipment. It is the sum of discounted price of all return line items in the shipment excluding gift items.

``````public void GetShippingReturnItemsTotal(IShipment shipment, Currency currency, IShippingCalculator shippingCalculator)
{
var shippingItemTotal = shippingCalculator.GetShippingReturnItemsTotal(shipment, currency);
Debug.WriteLine("Total prices of all return line items for shipment '{0}': {1}", shipment.ShipmentId, shippingItemTotal);
}
``````
• Shipping totals â€“ Calculates the shipping totals.

``````public void GetShippingTotals(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
{
var shippingTotals = shippingCalculator.GetShippingTotals(shipment, market, currency);
Debug.WriteLine("Subtotal for shipment '{0}': {1}", shipment.ShipmentId, shippingTotals.ItemsTotal);
Debug.WriteLine("Shipping cost for shipment '{0}': {1}", shipment.ShipmentId, shippingTotals.ShippingCost);
Debug.WriteLine("Shipping tax for shipment '{0}': {1}", shipment.ShipmentId, shippingTotals.ShippingTax);
}
``````
• Shipping tax â€“ Calculates a shipment's shipping tax.

``````public void GetShippingTax(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
{
var shippingTax = shippingCalculator.GetShippingTax(shipment, market, currency);
Debug.WriteLine("Shipping tax for shipment '{0}': {1}", shipment.ShipmentId, shippingTax);
}
``````
• Sales tax â€“ Calculates a shipment's sales tax, based on shipping address.

``````public void GetSalesTax(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
{
var salesTax = shippingCalculator.GetSalesTax(shipment, market, currency);
Debug.WriteLine("Sales tax for shipment '{0}': {1}", shipment.ShipmentId, salesTax);
}
``````
• Return shipping tax â€“ Calculates a shipment's return shipping tax.

``````public void GetReturnShippingTax(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
{
var returnShippingTax = shippingCalculator.GetReturnShippingTax(shipment, market, currency);
Debug.WriteLine("Shipping tax for return shipment '{0}': {1}", shipment.ShipmentId, returnShippingTax);
}
``````
• Return sales tax â€“ Calculates the sales tax for a return shipment that contains return line items.

``````public void GetReturnShippingTax(IShipment shipment, IMarket market, Currency currency, IShippingCalculator shippingCalculator)
{
var returnShippingTax = shippingCalculator.GetReturnShippingTax(shipment, market, currency);
Debug.WriteLine("Shipping tax for return shipment '{0}': {1}", shipment.ShipmentId, returnShippingTax);
}
``````

## Change the default calculation

By inheriting from the default implementation of the interface,Â DefaultShippingCalculator, you can override calculation methods. To change the calculation of shipping cost, override one or severalÂ CalculateShippingCostÂ methods. To change the calculation of shipping items' total, override theÂ CalculateShippingItemsTotal method. And override other methods to change the tax calculation.

``````public class ShippingCalculatorOverridingDefault : DefaultShippingCalculator
{
public ShippingCalculatorOverridingDefault(ILineItemCalculator lineItemCalculator,
IReturnLineItemCalculator returnLineItemCalculator,
ITaxCalculator taxCalculator)
: base(lineItemCalculator, returnLineItemCalculator, taxCalculator)
{ }
protected override ShippingMethodDto GetShippingMethods()
{
return new ShippingMethodDto();
}
protected override Money CalculateShippingCost(IShipment shipment, IMarket market, Currency currency)
{
return new Money(0m, currency);
}
protected override bool CanBeConverted(Money moneyFrom, Currency currencyTo)
{
return true;
}
protected override Money CalculateShippingItemsTotal(IShipment shipment, Currency currency)
{
return new Money(0, currency);
}
protected override Money CalculateShippingReturnItemsTotal(IShipment shipment, Currency currency)
{
return new Money(0, currency);
}
protected override Money CalculateShippingTax(IShipment shipment, IMarket market, Currency currency)
{
return new Money(0, currency);
}
protected override Money CalculateReturnShippingTax(IShipment shipment, IMarket market, Currency currency)
{
return new Money(0, currency);
}
}
``````

## Change the default validation

The default implementation validates that all return values are not negative after the calculation. To change the behavior, override the appropriate method: ValidateShipmentCostForShipment, ValidateShippingItemTotal, ValidateShipping Tax, or ValidateSalesTax.

``````public class ShippingCalculatorOverridingDefault : DefaultShippingCalculator
{
public ShippingCalculatorOverridingDefault(
ILineItemCalculator lineItemCalculator,
IReturnLineItemCalculator returnLineItemCalculator,
ITaxCalculator taxCalculator)
: base(lineItemCalculator, returnLineItemCalculator, taxCalculator)
{ }

protected override void ValidateShippingCostForShipment(Money money)
{
if (money.Amount <= 0)
{
throw new ValidationException("Shipping cost must be greater than 0");
}
}

protected override void ValidateShippingItemTotal(Money money)
{
if (money.Amount <= 0)
{
throw new ValidationException("Shipping item total must be greater than 0");
}
}

protected override void ValidateShippingTax(Money money)
{
if (money.Amount <= 0)
{
throw new ValidationException("Shipping tax 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");
}
}
}
``````