GetCustomShippingMethods

Posted by Community Admin on 05-Aug-2018 15:13

GetCustomShippingMethods

All Replies

Posted by Community Admin on 20-Nov-2012 00:00

Hi.

I'm trying to make a shipping calculator for a site, and need to access the Custom Shipping methods.

I'm getting a hard time implementing this function:

private IList<IShippingResponse> GetCustomShippingMethods()
    ShippingManager manager = ShippingManager.GetManager();
    ParameterExpression parameterExpression = Expression.Parameter(typeof(ShippingMethod), "x");
    ParameterExpression[] parameterExpressionArray = new ParameterExpression[1];
    parameterExpressionArray[0] = parameterExpression;
    List<ShippingMethod> list = manager.GetShippingMethods().Where<ShippingMethod>(Expression.Lambda<Func<ShippingMethod, bool>>(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(get_IsActive)), parameterExpressionArray)).ToList<ShippingMethod>();
    List<IShippingResponse> shippingResponses = new List<IShippingResponse>();
    List<IShippingResponse> shippingResponses1 = shippingResponses;
    IEnumerable<GenericShippingResponse> genericShippingResponses = list.Select<ShippingMethod, GenericShippingResponse>(new Func<ShippingMethod, GenericShippingResponse>(this.CalculateShippingMethod));
    shippingResponses1.AddRange(genericShippingResponses.Where<GenericShippingResponse>((GenericShippingResponse x) => x != null));
    return shippingResponses;

The problem is the "get_IsActive" variable. Where does this come form? Tearing my hair off here :-)

Or has anyone another way to access the Custom shipping methods?

OC

Posted by Community Admin on 23-Nov-2012 00:00

Hi Ole,

Thank you for contacting Telerik Support!

In the Ecommerce module there are shipping carriers, which expose shipping services, and shipping methods. The shipping carriers are third parties, like FedEx, UPS, etc. The shipping methods are created from the backend under Ecommerce -> Shipping methods.

To get the shipping methods that you have created in the backend, you can use the following code:

public static IList<ShippingMethod> GetShippingMethods()
    ShippingManager manager = ShippingManager.GetManager();
 
    List<ShippingMethod> shippingMethods = manager.GetShippingMethods().Where(m => m.IsActive).ToList();
 
    return shippingMethods;

This code will return a list of all active shipping methods created by you under Ecommerce -> Shipping methods. You can iterate through them and find the one that corresponds to the desired country, weight, time frame, etc. The ShippingMethod class exposes properties for the specifics of the method, like price, area, countries, etc. If you have any questions about working with the ShippingMethod class and API, don't hesitate to ask.

If you want to get the rates of the carrier services of the shipping carriers that are suitable for the provided country, ZIP code and weight, you can use the following code:

public static IList<IShippingResponse> GetShippingCarrierServices()
    ShippingManager shippingManager = ShippingManager.GetManager();
 
    List<IShippingResponse> availableShippingServices = new List<IShippingResponse>();
 
    //Get the shipping carriers and iterate through them.
    foreach (var shippingCarrier in Config.Get<ShippingConfig>().ShippingCarrierProviders.Values.Where(x => x.IsActive))
    
        //Get an instance of the shipping carrier provider.
        var carrierProvider = shippingManager.GetShippingCarrierProvider(shippingCarrier.Name);
 
        //Get the shipping service that corresponds to the countries you want to ship between, and to the weight.
        //The ShippingResponseContext contains the available services for the specified locations and weight.
        //Note that you have to pass the appropriate country codes according to the shipping carriers. You can find them on the carriers' sites.
        ShippingResponseContext shippingContext = carrierProvider.GetServiceRates(CreateShippingRequest("fromCountry", "fromZip", "toCountry", "toZip", 100));
 
        if (shippingContext.ShippingResponses != null)
        
            //Each shipping response object has a property representing the rate of the service.
            availableShippingServices.AddRange(shippingContext.ShippingResponses);
        
    
 
    return availableShippingServices;
 
public static IShippingRequest CreateShippingRequest(string shipFromCountry, string shipFromZip, string shipToCountry, string shipToZip, double totalWeight)
    GenericShippingRequest request = new GenericShippingRequest(shipFromCountry, shipFromZip, shipToCountry, shipToZip, totalWeight);
 
    return request;

In this case, the method will return all of the services of each carrier, that are suitable for the specified locations and weight. Note that this creation of the shipping request is just for the sample. You can implement it as it is most comfortable for you.

I hope that with these two code snippets I managed to cover you scenario. If not, please share more details on it. Any additional information will be much appreciated!

Thank you kindly in advance!

All the best,
Martin Mihailov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 28-Nov-2012 00:00

Martin,

Thanks. But I have suggested a PITS issue that you make this like for the shipping carriers. One code snipplet should return all sipping options.

OC

Posted by Community Admin on 30-Nov-2012 00:00

Hello Ole,

Thank you for suggesting your issue in PITS! If you need any additional assistance, don't hesitate to come back to us!

Regards,
Martin Mihailov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed