Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Paul Bennett 30 posts 53 karma points
    Nov 14, 2011 @ 17:47
    Paul Bennett
    0

    Problem with creating a custom shipping method

    Hi I'm trying to create a custom shipping method as it describes on this page:
    http://www.publicvoid.dk/BuildingACustomShippingMethodService.aspx

    Unfortunately it won't let me build the project because:

    'System.Collections.Generic.ICollection<UCommerce.EntitiesV2.OrderLine>' does not contain a definition for 'PurchaseOrders' ...

    'UCommerce.EntitiesV2.Shipment' does not contain a definition for 'ShippingMethods' ...

    'UCommerce.EntitiesV2.Shipment' does not contain a definition for 'OrderAddresses' ...

    'UCommerce.EntitiesV2.Shipment' does not contain a definition for 'ShippingMethods' ...

    Do you know what I am missing?
    Here is my code:
    using System;
    using System.Linq;
    using UCommerce;
    using UCommerce.EntitiesV2;
    using UCommerce.Transactions.Shipping;
    
    namespace AirAmbulanceCustomShippingService
    {
        public class AirAmbulanceCustomShippingService : IShippingMethodService
        {
            public string Name { get; set; }
    
            public Money CalculateShippingPrice(Shipment shipment)
            {
                var numberOfItems = shipment.OrderLines.Count();
                decimal shippingPrice = 0;
                switch (numberOfItems)
                {
                    case 1:
                        shippingPrice = 150;
                        break;
                    case 2:
                        shippingPrice = 250;
                        break;
                    case 3:
                        shippingPrice = 350;
                        break;
                    default:
                        shippingPrice = 450;
                        break;
                }
                return new Money(shippingPrice, shipment.OrderLines.PurchaseOrders.Single().Currencies.Single());
            }
    
            public bool ValidateForShipping(Shipment shipment)
            {
                if (shipment.ShipmentAddressId <= 0)
                    throw new InvalidOperationException("Cannot validate shipment for country. Remember to set the shipping address for shipment.");
                var shippingMethod = shipment.ShippingMethods.SingleOrDefault();
                if (shippingMethod == null)
                    throw new InvalidOperationException("Cannot validate destination country for shipment. It does not contain a shipping method. Remember to add a shipping method to your shipment before validating.");
                return ValidateShippingDestination(shipment.OrderAddresses.Single(), shipment.ShippingMethods.Single());
            }
    
            /// <summary>
            /// Validates the order lines according to their desired destination and configured contries for shipping method
            /// </summary>
            /// <returns></returns>
            protected virtual bool ValidateShippingDestination(OrderAddress shippingAddress, ShippingMethod shippingMethod)
            {
                var eligibleCountries = shippingMethod.EligibleCountries;
                if (eligibleCountries == null) return false;
                return eligibleCountries.SingleOrDefault(x => x.CountryId == shippingAddress.CountryId) != null;
            }        
        }
    }
    
    Cheers
    Paul

     

     

  • Søren Spelling Lund 1797 posts 2786 karma points
    Nov 29, 2011 @ 08:56
    Søren Spelling Lund
    0

    Hi Paul,

    The article describes how to use the V1 API, which is not available in uCommerce 2.0. Whereever you see a ShippingMethods.Single() call in the article you can use ShippingMetod instead. The reason is that our old ORM had an interesting way of expressing relationships in the sense that it would do many to many relationships for everything, even one to one.

    Hope this helps.

  • Paul Bennett 30 posts 53 karma points
    Dec 09, 2011 @ 15:12
    Paul Bennett
    0

    Thanks Søren that did the trick

    For anyone else doing similar I also had difficulty with the line

    return new Money(shippingPrice, shipment.OrderLines.PurchaseOrders.Single().Currencies.Single());

    Using the following worked though:

    return new Money(shippingPrice, shipment.PurchaseOrder.BillingCurrency);

     

     

  • Søren Spelling Lund 1797 posts 2786 karma points
    Dec 13, 2011 @ 13:00
    Søren Spelling Lund
    0

    Forgot to point out that there's an article, which details the differences between the two APIs Migrating Code to UCommerce.EntitiesV2. It should be useful until all the docs are updated to V2.

Please Sign in or register to post replies

Write your reply to:

Draft