Add Shipping Information

Developer tasks for using service method AddShippingInfo.

AddShippingInfo is used to add shipping information to an existing cart.

At your visual studio solution setup:

  1. Reference the Sitecore.Commerce.Connect.CommerceServer and Sitecore.Commerce.dll.
  2. Pick the class in your solution where want to use this service method.
  3. Paste in the code below to use the service method.
    CartServiceProvider provider = new CartServiceProvider();             
    // You should get a real user id.            
    string userId = Guid.NewGuid().ToString("B");
    string anonymousUserId = Guid.NewGuid().ToString("B");             
    var loadRequest = new LoadCartByNameRequest("Website", "MyBasket", userId);            
    var loadResult = provider.LoadCart(loadRequest);             
    CommerceCart cart = loadResult.Cart as CommerceCart;             
    // Add the party (address) information.            
    CommerceParty party = new CommerceParty()            
    {                
    ExternalId = Guid.NewGuid().ToString("B"),                 
    Name = "MyFirstAddress",                
    Address1 = "Address 1 field",                
    Address2 = "Address 2 field",                
    City = "City field",                
    Company = "Company field",                
    Country = "Country field",                 
    CountryCode = "CND",               
    Email = "a@b.com",                
    EveningPhoneNumber = "111 111-1111",                
    FaxNumber = "222 222-2222",                
    FirstName = "First name field",                
    LastName = "Last name field",                
    PhoneNumber = "333 333-3333",                
    RegionCode = "01",                
    RegionName = "Region field",                
    State = "State field",                
    ZipPostalCode = "HOH OHO"            
    };             
    AddPartiesRequest request = new AddPartiesRequest(cart, new List<Party> { party });
    var result = provider.AddParties(request);             
    party = result.Parties.ElementAt(0) as CommerceParty;            
    cart = result.Cart as CommerceCart;             
    // Get the shipping methods            
    CommerceOrderServiceProvider orderProvider = new CommerceOrderServiceProvider();             
    GetShippingMethodsRequest shippingMethodsRequest = new GetShippingMethodsRequest("en-US");            
    var shippingMethodsResult =  orderProvider.GetShippingMethods(shippingMethodsRequest);             
    CommerceShippingMethod shippingMethod = shippingMethodsResult.ShippingMethods.ElementAt(0) as CommerceShippingMethod;             
    ShippingInfo shippingInfo = new ShippingInfo() { ShippingMethodID = shippingMethod.ExternalId, PartyID = party.ExternalId};            
     List<string> lineItemIdList = new List<string>();             
    // Add all cart lines to theshipping info.            
    foreach (CommerceCartLine lineItem in cart.Lines)            
    {               
            lineItemIdList.Add(lineItem.ExternalCartLineId);            
    }             
    shippingInfo.LineIDs =lineItemIdList;             
    AddShippingInfoRequest shippingInfoRequest = new AddShippingInfoRequest(cart, new List<ShippingInfo> { shippingInfo});                   
    provider.AddShippingInfo(shippingInfoRequest);