How to Add Product to The Basket in Commerce Server 2009

Here In this article I will explain the detailed steps to add the product to the user's basket or basket of the anonymous user.

In commerce server, user's order is divided into 3 states: Basket, Order Template and Purchase Order. Here Basket is the customer's shopping cart. (Reference: http://msdn.microsoft.com/en-us/library/microsoft.commerceserver.runtime.orders.basket%28CS.90%29.aspx )

Basket is represented by Basket class and which is in namespace Microsoft.CommerceServer.Runtime.Orders

Step 1: Get the basket instance of current user:

To get the basket instance of the current user, get the UserID of the current user as follows

CommerceContext csContext = CommerceContext.Current;
string UserID = csContext.UserID;

Here,

CommerceContext : This class is available in namespace Microsoft.CommerceServer.Runtime(Reference: http://msdn.microsoft.com/en-us/library/microsoft.commerceserver.runtime.commercecontext%28CS.90%29.aspx )

Once we get the UserID we can get the basket instance for the user as follows

basket = CommerceContext.Current.OrderSystem.GetBasket(new Guid(UserID));

But in our project we have scenario, to add the items for the anonymous users. In this case we will get UserID as a null. So here we are using a ShoppingController class from the microsoft CommerceSharePointExtensibilityKit. Once you installed commerce server 2009 SDK this kit will be available for you. This kit contains all the source code for the commerce server webparts. You can find this kit in your CommerceServer folder (For ex. - C:\Program Files\Microsoft Commerce Server 2007\Microsoft Commerce Server 2009\Sdk\Samples)

ShoppingController class is available in Microsoft.Commerce.Portal.Common namespace. So to get the basket for the anonymous user

ShoppingController  shoppingController =
new ShoppingController(true,
false,
false,
false,
false,
false
);

//Add the basket query request               
shoppingController.AddBasketQueryRequest(ShoppingController.DefaultBasketName);
//Execute the basket query request
shoppingController.ExecuteBrokerRequests();

Microsoft.Commerce.Portal.Common.Basket commonBasket =               
shoppingController.GetBasket(ShoppingController.DefaultBasketName);

basket = CommerceContext.Current.OrderSystem.GetBasket(new Guid(commonBasket.UserId), "default");

Step 2: Get the OrderForm:

OrderForm: Order form is a container that holds products. Order form represents collection of line items. This class is available in Microsoft.CommerceServer.Runtime.Orders namespace.

We will get the OrderForm from the basket instance as follows

OrderForm orderForm = basket.OrderForms[0];

There might be case that there is no order form available for the basket. In this case we can create new order form as follows:

OrderForm orderForm;
if (basket.OrderForms.Count == 0)
                {
                    orderForm = new OrderForm();
                    basket.OrderForms.Add(orderForm);
                }

Step 3: Add the line items to the order form:

For Line Items, LineItem class is available. LineItem represent single line item on an order form. LineItem class is available in Microsoft.CommerceServer.Runtime.Orders namespace.

To add line items to the order form we need to create line item instance as follows:

            
LineItem lineItem = new LineItem();
                lineItem.ProductCatalog ="Adventure Works catalog";
                lineItem.ProductId = "AW074-04";
                //Default quantity
                lineItem.Quantity = 1;
                lineItem.DisplayName = Dunes;
                //Default ListPrice
                lineItem.ListPrice = 98,00;

//Add the product (LineItem) to an order form
                orderForm.LineItems.Add(lineItem);

Step 4: Save the basket:

basket.Save();

Up Next
    Ebook Download
    View all
    Learn
    View all