Sana Assistant (online)
Table of Contents

Settings Per Shipping Method

Shipping service provider may support several different shipping methods and each shipping method may require its own specific settings. Sana provides a possibility to display specific settings per each shipping method.

From this article you will learn how to add shipping method settings to shipping method configuration page.

In this developer's guide we are not going to create a new extension, instead of this we will refer to the shipping extension created in Implementing new shipping service provider as extension article.

Add shipping method settings

Step 1: Create a new class CustomShippingMethodSettings inherited from ShippingMethodSettings.

public class CustomShippingMethodSettings : ShippingMethodSettings
{
    //Properties
}

Step 2: Add needed properties to the CustomShippingMethodSettings.

Add properties to this class per each custom shipping method setting. We'll add a service type setting so that every shipping method can be configured for one the three types of delivery - ground delivery, air delivery and freight services for heavy parcels:

public enum CustomShippingServiceType
{
    Ground,
    Air,
    Freight
}

public class CustomShippingMethodSettings : ShippingMethodSettings
{
    [Display(Name = "Service type")]
    public CustomShippingServiceType ServiceType { get; set; }
}

Step 3: Entity inherited from ShippingMethodSettings - is a model that will be rendered on shipping method configuration page, therefore we can use DataAnnotations attributes for its properties. Also there is a possibility to create and use own editor templates.

Let's add Display attribute to our property to set more informative field name.

public class CustomShippingMethodSettings : ShippingMethodSettings
{
    [Display(Name = "Service type")]
    public CustomShippingServiceType ServiceType { get; set; }
}

Step 4: Specify 'CustomShippingMethodSettings' as a generic type parameter for ShippingExtension in 'CustomShippingServiceExtension.cs'. So it should look like this:

[ShippingModuleId("CustomShippingService")]
public class CustomShippingServiceExtension : ShippingExtension<CustomShippingMethodSettings>
{
    // Properties and methods
}

Result: After these steps we can see the following block with the "Service type" input on the Shipping method configuration page.

New Shipping Method Settings

How to get access to shipping method settings

Shipping method settings are stored in Settings property of context.Methods collection items, so that you can easily access them like in an example below.

ShippingRate CreateShippingRate(ShippingMethod method)
{
    var rate = new ShippingRate
    {
        ShippingMethodId = method.Id
    };
    switch (((CustomShippingMethodSettings)method.Settings).ServiceType)
    {
        case CustomShippingServiceType.Ground:
            rate.Cost = 10m;
            break;
        case CustomShippingServiceType.Air:
            rate.Cost = 12m;
            break;
        case CustomShippingServiceType.Freight:
            rate.Cost = 15m;
            break;
    }
    return rate;
}

See also