Example - Custom shipping option selection

The following example customizes ShippingOptionInfoProvider, which the E-commerce solution uses to manage shipping options for product orders. The sample customization modifies the system so that the system offers shipping options according to target destination countries of checkout processes.

First, you must create a custom field to shipping options where store administrators can select countries to which the shipping options are available. Second, you must customize the ShippingOptionInfoProvider class.

Creating a custom field in shipping options

Before the system can offer only allowed shipping options, store administrators must be able to assign countries to shipping options. To do that, you must create a custom field to the Shipping option class form.

  1. Add a field to the Shipping option class in the E-commerce module.
  2. Enter following values to properties of the new field:
    • Field name: ShippingOptionLimitToCountries
    • Field caption: Limit to countries
    • Form control: Uni selector
    • Object type: cms.country
    • Return column name: CountryThreeLetterCode
    • Selection mode: Multiple
      Custom field settings
  3. Save the field.

Now, when store administrators add or edit shipping options in the Store configuration application, they can choose countries. However, the selected countries aren't used for anything yet.

Adding allowed countries

Customizing ShippingOptionInfoProvider

Customize the ShippingOptionInfoProvider class to set up the system so that it offers only those shipping options that are set as available for a given country by store administrators.

  1. Open your web project in Visual Studio.
  2. Create a new class in the App_Code folder (or Old_App_Code on web application installations). For example, name the class CustomShippingOptionInfoProvider.cs.

    using System.Linq;
    using CMS;
    using CMS.Ecommerce;
    using CMS.Globalization;
    
    [assembly: RegisterCustomProvider(typeof(CustomShippingOptionInfoProvider))]
    
    /// <summary>
    /// Sample shipping option info provider. 
    /// </summary>
    public class CustomShippingOptionInfoProvider : ShippingOptionInfoProvider
    {
        /// <summary>
        /// Ensures that the shipping option is applicable only if the country in the target address of an checkout process is from the list with allowed countries.
        /// </summary>
        /// <param name="cart">Shopping cart data.</param>
        /// <param name="shippingOption">Shipping option which is being checked for applicability.</param>
        /// <returns>True if the shipping option is allowed to be applied in the target country, otherwise returns false.</returns>
        protected override bool IsShippingOptionApplicableInternal(ShoppingCartInfo cart, ShippingOptionInfo shippingOption)
        {
            // Does not check availability if shopping cart or shipping option object is not available
            if ((cart == null) || (shippingOption == null))
            {
                return true;
            }
    
            // Gets data for the ShippingOptionLimitToCountries field
            var shippingOptionLimitToCountries = shippingOption.GetValue("ShippingOptionLimitToCountries");
    
            // Does not check availability if no countries were permitted
            if (shippingOptionLimitToCountries == null)
            {
                return true;
            }
    
            // Parses retrieved data
            var allowedCountriesList = shippingOptionLimitToCountries.ToString().Split(';');
            
            // Gets address and country
            var address = cart.ShoppingCartShippingAddress ?? cart.ShoppingCartBillingAddress;
            var country = CountryInfoProvider.GetCountryInfo(address.AddressCountryID);
    
    		// Does not check availability if country was not found
    		if (country == null)
            {
                return true;
            }
    
            // Returns if customer's country was found in the list with allowed countries
            return allowedCountriesList.Contains(country.CountryThreeLetterCode);
        }
    }
  3. Save the CustomShippingOptionInfoProvider.cs file.

If store administrators now enter available countries to shipping options, the system offers customers only those shipping options which:

  • are allowed for countries in the destination addresses of customers' checkout processes, or
  • do not have any countries set.

Was this page helpful?