Free Shipping
for Sitefinity Ecommerce

Nov 03

I recently had a requirement for Sitefinity Ecommerce to supply free shipping for orders over a certain amount. Along with that, it needed to be turned on and off for promotions. This feature isn't supplied out of the box with Sitefinity so I had to write an extension.

With the help of the Sitefinity team, who really did all the work, I got this extension class written. I thought I would post it up for everyone to see and use.

I made my own config class to allow admins to set the conditions of the free shipping. I also created a 'Free Shipping' shipping method with a cost of $0. Finally I registered the service in the Bootstrapper initilized event.

public class MyEcommerceShippingMethodService : EcommerceShippingMethodService
{
    public override IQueryable<Telerik.Sitefinity.Ecommerce.Shipping.Model.IShippingResponse> GetApplicableShippingMethods(Telerik.Sitefinity.Modules.Ecommerce.Orders.Web.UI.CheckoutViews.CheckoutState checkoutState, Telerik.Sitefinity.Ecommerce.Orders.Model.CartOrder cartOrder)
    {
        MyConfig config = Config.Get<MyConfig>();
 
        Boolean isDateConditionSatisfied = false;
 
        if(config.Active)
        {
            DateTime discountStartDate = config.StartDate;
            DateTime discountEndDate = config.EndDate;
 
            isDateConditionSatisfied = DateTime.Now >= discountStartDate && DateTime.Now <= discountEndDate;
        }
            
 
        Decimal totalPrice = cartOrder.Total;
 
        // Convert total price if currency is different than USD.
        if (cartOrder.Currency != "USD")
        {
            EcommerceExchangeRateConverter exchangeRateConverter = new EcommerceExchangeRateConverter();
            totalPrice = exchangeRateConverter.ConvertPrice(cartOrder.Total, "USD");
        }
 
        // Get the predefined free shipping method.
        var shippingManager = ShippingManager.GetManager();
        ShippingMethod freeShippingMethod = shippingManager.GetShippingMethods().FirstOrDefault(s => s.Title == "Free Shipping");
 
        IQueryable<IShippingResponse> result = new List<IShippingResponse>().AsQueryable();
 
        // Apply free shipping discount if required conditions are fulfilled.
        if (config.Active && isDateConditionSatisfied && totalPrice > config.PurchaseValue)
        {   
            Decimal price = 0.0m;
            GenericShippingResponse customPriceShippingResponce = new GenericShippingResponse()
            {
                ShippingMethodId = freeShippingMethod.Id,
                ServiceName = freeShippingMethod.Title,
                ServicePrice = price,
                ServiceCode = freeShippingMethod.Name,
                ShippingCarrierName = freeShippingMethod.Name,
                ShippingServiceName = freeShippingMethod.Name,
                SortOrder = freeShippingMethod.SortOrder
            };
 
            result = new List<IShippingResponse>() { customPriceShippingResponce }.AsQueryable();
        }
        else
        {
            var shippingMethods = base.GetApplicableShippingMethods(checkoutState, cartOrder).ToList();
            var freeShipping = shippingMethods.Where(sm => sm.ShippingMethodId.Equals(freeShippingMethod.Id)).SingleOrDefault();
 
            // Remove the predefined free shipping method from options.
            if (freeShipping != null)
            {
                shippingMethods.Remove(freeShipping);
                result = shippingMethods.AsQueryable();
            }                
        }
 
        return result;
    }
}


ObjectFactory.Container.RegisterType<IEcommerceShippingMethodServiceMyEcommerceShippingMethodService>(new ContainerControlledLifetimeManager(), new InjectionConstructor());

Darrin Robertson - Sitefinity Developer

Thanks for reading and feel free to comment
Darrin Robertson


Leave a comment
Load more comments

Make a Comment

Please type the code above

comment-avatar