ShippingExtension reference
Sana.Extensions.Shipping.ShippingExtension
is an abstract class that you should implement
when you develop a new shipping provider extension for Sana Commerce.
There is also generic class ShippingExtension<TMethodSettings>
inherited from ShippingExtension
which provides shipping extensions with possibility to have configurable shipping methods.
It takes type inherited from ShippingMethodSettings
as a generic type parameter.
Properties
BasketTotalsRequired
Gets the value indicating whether calculated basket totals are required to calculate shipping rates in this extension.
If your extension depends on correct basket total amounts, e.g. when shipping cost must be
zero when basket total amount is more than a certain number, implement this property and return
true
to trigger necessary calculations in Sana:
public override bool BasketTotalsRequired => true;
Otherwise, if your extension does not depend on basket total amount, e.g. when shipping cost
depends only on weight of items in the basket, implement this property and return false
to avoid unnecessary calculations. In such cases basket totals that are available to your
extension through ShippingContext will be zero:
public override bool BasketTotalsRequired => false;
WeightRequired
Gets the value indicating whether package weight is required to calculate shipping rates in this extension.
If your extension depends on weight of items in the basket, implement this property and return
true
to trigger necessary weight calculations in Sana:
public override bool WeightRequired => true;
Otherwise, if your extension does not depend on package weight, implement this property and
return false
to avoid unnecessary weight calculations for items in the basket. In such case
weights of items that are available to your extension through ShippingContext
will be null:
public override bool WeightRequired => false;
Methods
CalculateShippingRates
Calculates shipping rates for specified shipping methods.
Implement the actual logic of shipping rate calculation in this method. During checkout process Sana will call this method, pass in the instance of ShippingContext reference where all the data needed for your calculations will be available and you should return calculated shipping rates as a result.
public override IList<ShippingCost> CalculateShippingRates(ShippingContext shippingContext)
{
var calculatedRates = new List<ShippingCost>();
foreach (var method in shippingContext.Methods)
{
var rate = new ShippingCost
{
Cost = /* calculate actual cost here */,
ShippingMethodId = method.Id,
};
calculatedRates.Add(rate);
}
return calculatedRates;
}
GetPickupLocations
Gets pickup locations for shipping method.
This method is called only when some shipping rates returned from CalculateShippingRates
method have pickup locations. It is indicated by HasPickupLocation
property value on the ShippingRate class. Sana passes the instance of ShippingPickupLocationsContext reference
where all the data needed for pickup locations retrieving is available.
public override IList<PickupLocation> GetPickupLocations(ShippingPickupLocationsContext context)
{
var pickupLocations = /* receive pickup locations from shipping provider*/;
return pickupLocations.Select(l => new AddressAutocompleteSuggestion
(
Id = l.Id,
Address = l.Address,
Cost = l.Cost,
Distance = l.Distance
)).ToList();
}