I'm going to repost this series of "development tips" related to Commerce Server 2007 to help other folks get started with their development work. All of these "tips" come directly from what I've learned (the hard way) over the past eight months and are intended to save you time and effort. All code samples are now based upon the RTM version and have been tested in our production environment.

Tip #6 – Adding Items to your Basket

The basic funtionality of the LineItem class has changed little from Commerce Server 2002 and adding an item to your basket works pretty much as it did in the previous version.

public void AddItemToBasket(decimal Qty, 
                    string UnitOfMeasure, 
                    string ProductID, 
                    string DisplayName, 
                    decimal Price, 
                    string Description, 
                    string Catalog, 
                    string GCSPartNumber, 
                    string CustomerPartNumber)
        // If the Basket Doesn't already have an OrderFormCollection
        if (_Basket.OrderForms[OrderForm.DefaultOrderFormName] == null)
            // Add a new OrderForm to the Basket
            _Basket.OrderForms.Add(new OrderForm(OrderForm.DefaultOrderFormName));
        // Create a new LineItem instance
        LineItem item = new LineItem();
        // Set the required LineItem properties
        item.Quantity = Qty;
        item.ProductId = ProductID;
        item.PlacedPrice = Price;
        item.Description = Description;
        item.ProductCatalog = Catalog;
        item.DisplayName = DisplayName;
        // Set any custom weakly typed properties
        item["gcs_part_number"] = GCSPartNumber;
        item["customer_part_number"] = CustomerPartNumber;
        item["unit_of_measure"] = UnitOfMeasure;
        // Add the LineItem to the OrderForm's LineItemCollection
        // Update and Save the Basket

The first thing to do is create a new LineItem instance, set the required and optional (weakly-typed) properties and then add the LineItem to the OrderForm's LineItemCollection.

As with every action taken in the Basket, you should always update the Basket and run the appropriate pipeline as shown here.

public void UpdateBasket(string pipelinename)
        // Run the pipeline
        // Save the Basket
        // Dispose of the PipelineInfo object

In Commerce Server 2007, it's also important to remember to dispose of the PipeLineInfo object to ensure that resources are released in a timely manner.

Also, since Commerce Server 2007 now supports multiple "named" baskets that are active at the same time, you may need to extend your code to accommodate this new feature.

Updated per reader request to include the GetBasketPipelineInfo call:

private PipelineInfo GetBasketPipelineInfo(string pipelinename)
  // Create a PipelineInfo instance
  PipelineInfo info = new PipelineInfo(pipelinename);

  // Set the language property
  info.Language = @"en-US";

  // Add the current user's profile
  info.Profiles.Add("User", CommerceContext.Current.UserProfile);

  // Set a weakly typed property
  info["catalog_language"] = @"en-US";

  // Return the object
  return info;


Technorati Tags: Commerce Server 2007

Share this post: Email it! | bookmark it! | digg it! | reddit!| kick it!