So you’ve built a custom shipping method service in uCommerce, using IShippingMethodService, as in this article:
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?
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:
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.
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.