Skip to content

Commerce Shipping API Reference

advanced
📜Corecommerce

The Shipping API calculates shipping rates and integrates with carriers. Implement IShippingPlugin for custom shipping providers. IShippingManagerFacade manages shipping methods and rates.

Optimizely.Commerce.Order

InterfacePurpose
IShippingPluginCustom shipping rate calculation
IShippingManagerFacadeManage shipping methods and gateways
IShipmentRepresents a shipment within an order

Implement this interface to create custom shipping rate providers.

GetRate(IMarket, Guid, IShipment, ref string)

Section titled “GetRate(IMarket, Guid, IShipment, ref string)”

Calculates the shipping rate for a shipment.

Parameters

NameTypeDescription
marketIMarketCurrent market
shippingMethodIdGuidShipping method identifier
shipmentIShipmentShipment with line items and address
messageref stringOutput message for errors or warnings

Returns: ShippingRate — contains the calculated amount and currency

Implement a custom shipping plugin
csharp
public class FlatRateShippingPlugin : IShippingPlugin
{
  public ShippingRate GetRate(
      IMarket market,
      Guid shippingMethodId,
      IShipment shipment,
      ref string message)
  {
      var totalWeight = shipment.LineItems.Sum(li => li.Quantity);
      decimal rate = totalWeight > 10 ? 15.99m : 5.99m;

      return new ShippingRate(
          shippingMethodId,
          "Flat Rate Shipping",
          new Money(rate, market.DefaultCurrency));
  }
}
PropertyTypeDescription
IdGuidShipping method ID
NamestringDisplay name
MoneyMoneyRate amount and currency

GetShippingMethodsByMarket(MarketId, bool)

Section titled “GetShippingMethodsByMarket(MarketId, bool)”

Returns available shipping methods for a market.

Parameters

NameTypeDescription
marketIdMarketIdTarget market
returnInactiveboolInclude inactive methods

Returns: IEnumerable<ShippingMethodDto.ShippingMethodRow>

GetRate(IShipment, ShippingMethodDto.ShippingMethodRow, IMarket)

Section titled “GetRate(IShipment, ShippingMethodDto.ShippingMethodRow, IMarket)”

Calculates the rate using the configured plugin.

Parameters

NameTypeDescription
shipmentIShipmentShipment to rate
methodShippingMethodDto.ShippingMethodRowShipping method
marketIMarketMarket context

Returns: ShippingRate

Get available shipping rates
csharp
public class ShippingService
{
  private readonly IShippingManagerFacade _shippingManager;
  private readonly IMarketService _marketService;

  public ShippingService(
      IShippingManagerFacade shippingManager,
      IMarketService marketService)
  {
      _shippingManager = shippingManager;
      _marketService = marketService;
  }

  public IEnumerable<ShippingRate> GetRates(IShipment shipment, MarketId marketId)
  {
      var market = _marketService.GetMarket(marketId);
      var methods = _shippingManager
          .GetShippingMethodsByMarket(marketId, false);

      return methods.Select(m =>
          _shippingManager.GetRate(shipment, m, market));
  }
}
PropertyTypeDescription
ShipmentIdintUnique shipment identifier
ShippingMethodIdGuidSelected shipping method
ShippingMethodNamestringDisplay name of the method
ShippingAddressIOrderAddressDestination address
LineItemsICollection<ILineItem>Items in this shipment
ShipmentTrackingNumberstringCarrier tracking number
ShippingDiscountdecimalDiscount applied to shipping
StatusstringShipment status
Register in dependency injection
csharp
services.AddTransient<IShippingPlugin, FlatRateShippingPlugin>();
ExceptionCause
ShippingExceptionRate calculation failed
InvalidOperationExceptionShipping method not found for market
ArgumentNullExceptionShipment or address is null