Create a custom shipping carrier provider class
To create a custom shipping carrier provider class, you must perform the following:
- Create a class to represent the shipping carrier provider.
In the folder for your custom shipping carrier, add a new class item.
- Make the class public.
Define the class to use the public access specifier.
- Implement the IShippingCarrierProvider interface.
The Telerik.Sitefinity.Modules.Ecommerce.Shipping.Carriers.IShippingCarrierProvider interface requires the following member:
- ShippingResponseContext GetServiceRates(IShippingRequest shippingData);
Returns a collection of the shipping rates for the carrier. In this method you have to gather the available rates for the carrier (e.g. call an external service), and wrap them in GenericShippingReponse objects.
Implement the GetServiceRates method
The GetServiceRates method accepts one argument. It is of type Telerik.Sitefinity.Ecommerce.Shipping.Model.IShippingRequest. It has the following members:
Represents the default height of the box used for shipping.
Represents the default length of the box used for shipping.
Represents the default width of the box used for shipping.
Represents the current cart order. The cart order contains the list of products that are going to be purchased. Using it, you can calculate the quantity of each product and its weight.
Specifies whether the measurements are in the metric system.
Represents the address that the products will be shipped from.
Represents the second address that the products will be shipped from.
Represents the city that the products will be shipped from.
Represents the country that the products will be shipped from.
Represents the state that the products will be shipped from.
Represents the ZIP code that the products will be shipped from.
Represents the address that the products will be shipped to.
Represents the second address that the products will be shipped to.
Represents the city that the products will be shipped to.
Represents the country that the products will be shipped to.
Represents the state that the products will be shipped to.
Represents the ZIP code that the products will be shipped to.
Represents the total weight of the products.
In this method you have to get a collection of the available shipping service for your provider. To do this you must perform the following:
- Get the shipping manager.
Get an instance of the ShippingManager object.
- Get the carrier provider settings.
To get the settings for the provider, call the GetShippingCarrierSetting and pass the name of the provider as an argument. The ShippingCarrierProviderSettings instance will contain the settings that you have entered in the administration backend for the provider. You can use one or more of these values to communicate with an external shipping provider. At this time these are the only fields available for holding shipping provider data. If you require additional data for particular provider, you could hardcode that data in this class or you might consider concatenating that data with other data in one of the existing carrier settings properties and parsing the data values as needed. For more information about configuring the provider in Sitefinity CMS, read the For developers: Install the custom shipping carrier provider.
- Calculate the shipping.
Calculate the shipping parameters like size of the package and weight. You can calculate the parameters of the shipping either manually or by calling an external service.
- Get the shipping services.
Call an external service provided by the shipping carrier to get the available carrier services and their rates. Wrap each of the services in a GenericShippingResponse object and add them to a ShippingResponseContext collection. It is recommended that you specify the carrier services that you want to use with your provider in the Sitefinity CMS backend. Then, you can filter the returned carrier services by the predefined ones. This will secure that you display to the customer only the services that you want. For more information about setting up carrier services in the backend and using them to filter the response from the service carrier, see For developers: Predefine carrier services.
- Return the collection of carrier service.
The method should return the obtained carrier services. Return the ShippingResponseContext collection.
Use the following code sample: