Loading...
Area: Episerver Commerce
Applies to versions: 10 and higher
Other versions:

Shipping methods

Recommendations [hide]

A shipping method manages information and rules that determine shipping cost and displays that information on the front-end site during checkout. The shipping cost is added to the total purchase price.

A shipping method is mapped to a Shipping Provider, visible to a back-end administrator in Commerce Manager. So, the public site can display friendly names (such as Ground Shipping), which are mapped to a provider, such as UPS. If a customer picks Ground Shipping, UPS is used.

See Shipping Gateways and Providers for information about creating gateways and providers.

Classes in this topic are available in the Mediachase.Commerce.Orders.Dto namespace.

How it works

  • To add a configuration tab for a shipping providers, create a new .ascx called ConfigureShippingMethod.ascx.
  • This .ascx uses the same inheritance and interface as the shipping provider.
  • The parameter settings for shipping methods are stored in the ShippingMethodParameter table.
  • See the ConsoleManager\Apps\Order\Shipping\Plugins\ WeightJurisdiction\ConfigureShippingMethod.ascx for an example of this.

    Note: This implementation uses AJAX to update the DTO, as the data occurs prior to the SaveChanges method being called.

Adding a shipping method parameters tab

You can store and retrieve information in the following ways.

  • Using the ShippingMethodParameter table

    The ShippingMethodParameter table lets you store name/value pairs for settings related to that shipping method.

    if (_ShippingMethodDto != null && 
      _ShippingMethodDto.ShippingMethodParameter != null &&  
      _ShippingMethodDto.ShippingMethodParameter.Rows.Count > 0) 
      { 
        ShippingMethodDto.ShippingMethodParameterRow
          row = (ShippingMethodDto.ShippingMethodParameterRow)_ShippingMethodDto.ShippingMethodParameter.Rows[0]; 
        txtTypeOfService.Text = row.Value; 
        lblTypeOfService.Text = row.Parameter; 
      }
  • Using the WeightJurisdiction provider

    You can store information in the format seen in the WeightJurisdiction provider, where each row of data represents the following, which is stored in the ShippingMethodCase table.

    • Total (representing the count or weight value)
    • Charge
    • JurisdictionGroupId
    • StartDate
    • EndDate for different shipping scenarios. 
    if (_ShippingMethodDto != null && _ShippingMethodDto.ShippingMethodCase != null && _ShippingMethodDto. ShippingMethodCase.Rows.Count > 0) 
      { 
        ShippingMethodDto.ShippingMethodCaseRow row = (ShippingMethodDto.ShippingMethodCaseRow)_ShippingMethodDto.ShippingMethodCase.Rows[0]; 
        txtTotalWeight.Text = row.Total; 
        txtCharge.Text = row.Charge; 
        txtStartDate.Text = row.StartDate.ToString(); 
      }

Accessing shipping information

The following example shows how to display shipping methods to users and the respective rates for each, based on the current cart.

//Get the list of all shipping methods to be filtered 
ShippingMethodDto methods = ShippingManager.GetShippingMethods(SiteContext.Current.LanguageName);  

// filter the list for only methods that apply to this particular cart's shipping address 
List<ShippingMethodDto.ShippingMethodRow> shippingRows = new List<ShippingMethodDto.ShippingMethodRow>(); 

foreach (ShippingMethodDto.ShippingMethodRow method in methods.ShippingMethod.Rows) 
  {
    shippingRows.Add(method); 
  }

List<ShippingRate> list = new List<ShippingRate>(); 

foreach (ShippingMethodDto.ShippingMethodRow row in shippingRows) 
  { 
    Type type = Type.GetType(row.ShippingOptionRow.ClassName); 
    string message = String.Empty; 
    IShippingGateway provider = (IShippingGateway)Activator.CreateInstance(type); 

    List<LineItem> items = new List<LineItem>(); 

    foreach(LineItem lineItem in CartHelper.LineItems) 
      {
        items.Add(lineItem); 
      }

    if (items.Count > 0) 
      {
        list.Add(provider.GetRate(row.ShippingMethodId, items.ToArray(), ref message)); 
      }
  }

To store information for a selected shipping method, use the LineItem properties. You do not need to create a shipment object, since the Cart Prepare workflow handles this.

ShippingMethodDto methods = ShippingManager.GetShippingMethods(SiteContext.Current.LanguageName); 

ShippingMethodDto.ShippingMethodRow row = methods.ShippingMethod.FindByShippingMethodId(new Guid(ShippingRatesList.SelectedValue)); 
foreach (LineItem lineItem in CartHelper.LineItems) 
  { 
    lineItem.ShippingMethodName = row.DisplayName; 
    lineItem.ShippingMethodId = row.ShippingMethodId; 
  }
Do you find this information helpful? Please log in to provide feedback.

Last updated: Oct 24, 2016

Recommendations [hide]