Shipping calculator
Describes ShippingCalculator, which calculates the shipping cost, shipping taxes, and total amount of line items in shipment in Optimizely Commerce Connect.
shippingCost
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);
}shippingDiscountedAmount
Calculates the shipping cost after applying the 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);
}shipmentSubtotal
Calculates the subtotal for all line items in the shipment, which is the sum of the discounted prices of all non-gift line items in the shipment.
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);
}shippingItemTotal
Calculates the subtotal for all return line items in the shipment, which is the sum of the discounted prices of all non-gift return line items in the shipment.
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);
}shippingTax
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);
}salesTax
Calculates a shipment's sales tax based on the 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);
}returnShippingTax
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);
}returnSalesTax
Calculates the sales tax for a return shipment that contains return line items.
public void GetReturnSalesTax(IShipment shipment, IMarket market, Currency currency, ISalesTaxCalculator salesTaxCalculator) {
var returnSalesTax = salesTaxCalculator.GetReturnSalesTax(shipment, market, currency);
Debug.WriteLine("Sales tax for return shipment '{0}': {1}", shipment.ShipmentId, returnSalesTax);
}shippingTotals
Calculates all shipping totals for the shipment (subtotal, shipping cost, and shipping tax).
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);
}Change the default calculation
Inherit from DefaultShippingCalculator, the default implementation of IShippingCalculator, and override one or more of its methods to change the calculation behavior. For example:
- To change the shipping cost calculation, override one or more of the
CalculateShippingCostoverloads. - To change the shipping items total, override
CalculateShippingItemsTotal. - To change tax calculations, override the corresponding
CalculateShippingTax,CalculateReturnShippingTax, orCalculateSalesTaxmethod.
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 each return value is non-negative after calculation. To change the behavior, override the appropriate method: ValidateShippingCostForShipment, ValidateShippingItemTotal, ValidateShippingTax, 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 not be negative.");
}
}
protected override void ValidateShippingItemTotal(Money money) {
if (money.Amount < 0) {
throw new ValidationException("Shipping item total must not be negative.");
}
}
protected override void ValidateShippingTax(Money money) {
if (money.Amount < 0) {
throw new ValidationException("Shipping tax must not be negative.");
}
}
protected override void ValidateSalesTax(Money money) {
if (money.Amount < 0) {
throw new ValidationException("Sales tax must not be negative.");
}
}
}Updated 21 days ago
