So you’ve built a custom shipping method service in uCommerce, using IShippingMethodService, as in this article:

Building a Custom Shipping Method Service

Often, you want to display the cost of each shipping method to the user during the checkout process, before they actually select them.

Wiggle does this well:

But how do you do this when you’re using a custom uCommerce IShippingMethodService, and you have your own logic for calculating the shipping costs of an order?

Why you can’t use ShippingMethod.GetPriceForCurrency()

Once you’ve told uCommerce to use your new IShippingMethodService for a shipping method..

.. you might assume that you can use ShippingMethod.GetPriceForCurrency() to get the cost to ship your order, like this:

var allShippingMethods = TransactionLibrary.GetShippingMethods(country);
var currency = purchaseOrder.BillingCurrency;

foreach (var shippingMethod in allShippingMethods)
{
	var cost = shippingMethod.GetPriceForCurrency(currency);
}

Unfortunately, this uCommerce method only returns the first price from the shipping method’s Pricing tab that matches the given currency:

It does not account for the calculations in your IShippingMethodService.

#Correctly get the cost for each shipping method

The correct way to do it is to manually call the IShippingMethodService, passing your current shipment.

At run-time we do not know which implementation of IShippingMethodService is set against a shipping method in uCommerce. Helpfully, uCommerce exposes this for us through the method GetShippingMethodService().

Using this, we can simply get the relevant IShippingMethodService for a given shipping method, construct a fake Shipment object (using our real basket of course) and pass it to the service to find out how much it would cost to ship our basket with that shipping method.

var purchaseOrder = TransactionLibrary.GetBasket().PurchaseOrder;
var allShippingMethods = TransactionLibrary.GetShippingMethods(country);

foreach (var shippingMethod in allShippingMethods)
{
  // Get the IShippingMethodService for this ShippingMethod
  var shippingService = shippingMethod.GetShippingMethodService();

  // Construct a fake shipping method to call the service with
  var shipment = new Shipment
  {
	  ShippingMethod = shippingMethod,
	  PurchaseOrder = purchaseOrder,
	  ShipmentAddress = purchaseOrder.BillingAddress
  };

  var shippingMethodPrice
 	 = shippingService.CalculateShippingPrice(shipment);
}

Of course, you might want some null checks and you will need to actually do something with the resulting price to display it, but you get the idea.

The default implementation of IShippingMethodService requires that you populate the Country in the ShipmentAddress, and the PurchaseOrder (as above), so make sure you set those in addition to whatever your custom service needs, just in case a given shipping method is using the default service.

And that’s it! Once you know how, it’s fairly simple to display the costs of custom IShippingMethodServices to the user.