Sample CS 2006 code for applying and displaying discounts on a Basket

In this post I shall run through some basic sample Commerce Server 2006 runtime site code to create a Basket, add an item to it and run it through the pipelines. We can then display the discounts that applied to the Basket (depending on what types of discounts you have created using the Marketing Manager) and place the order for the user. For the most part the comments should suffice in explaining what is being done by the code.

Please note that this code is just for illustration purposes to ramp up to CS 2006 APIs and not in the least meant to be used as is as part of a production site. Keep in mind that this would typically be done across multiple pages and a production site would have lots of safety measures such as the checkout page being run as a transacted page etc.

void IllustrativeScenario()
{

PipelineInfo basketPipe = new PipelineInfo("basket");
PipelineInfo totalPipe = new PipelineInfo("total");
PipelineInfo checkoutPipe = new PipelineInfo("checkout");

//Assuming Adventure Works Catalog has been imported into the site
LineItem advWorksItem = new LineItem("Adventure Works Catalog", "AW390-12", "1", 2);

//This would typically be the User Profile Id (which should be a Guid if it has to be used in conjuction with the Orders APIs
Guid userId = Guid.NewGuid(); //Dummy unique Guid value for test purposes

Basket shoppingCart = CommerceContext.Current.OrderSystem.GetBasket(userId, "Cart1");

//Now we shall create an OrderForm which will hold all the LineItem (products) purchased by the user
OrderForm userOrderForm = new OrderForm("Main OrderForm"); //You can also have multiple OrderForms per Basket if required for e.g. to group orders from different vendors

//Add the product
userOrderForm.LineItems.Add(advWorksItem);

//Add the OrderForm created to the user Basket
shoppingCart.OrderForms.Add(userOrderForm);

//Run the Basket pipeline to fetch prices of items, check inventory, apply discounts and reserve promocodes etc.
shoppingCart.RunPipeline(basketPipe);

//Now you can show things like the updated product price and whether an item is in stock etc.

//In order to create the Shipments we need to set the ShippingMethodId on each of the LineItems in the Basket

foreach (OrderForm orderForm in shoppingCart.OrderForms)
{
   //This assumes that a ShippingMethod has been created and enabled for this site using the Customer and Orders Manager
   DataSet enabledShippingMethods = CommerceContext.Current.OrderSystem.GetShippingMethods("en-US"); //Only returns enabled methods in language specified

   Guid advWorksShippingMethod = (Guid)enabledShippingMethods.Tables["en-US"].Rows[0]["ShippingMethodId"]; //We will just pick the first ShippingMethod for this sample

   foreach (LineItem item in orderForm.LineItems)
item.ShippingMethodId = advWorksShippingMethod;
}

//Now we can run the total pipeline to calculate the shipments and totals and apply any shipping discounts
shoppingCart.RunPipeline(totalPipe);

//At this point you can display the Basket totals and shipping costs to the user

//Now we can display information such Discounts that applied to this Basket
foreach (OrderForm orderForm in shoppingCart.OrderForms)
{
   //Item and OrderLevel Discounts (keep in mind that irrespective of the type, the discount is finally applied on the LineItem itself)
   foreach (LineItem item in orderForm.LineItems)
{
      //Show details of Item Level Discounts that applied to this LineItem
      foreach (DiscountApplicationRecord itemDiscount in item.ItemLevelDiscountsApplied)
{
         Response.Write("</BR>The friendly name of discount that applied was: " + itemDiscount.BasketDisplayMessage + "</BR>");
Response.Write("</BR>The discount Amount applied was: " + itemDiscount.DiscountAmount + "</BR>");
         switch (itemDiscount.TypeOfDiscount)
{
            case DiscountType.CurrencyValue:
Response.Write("</BR>It was a currency discount of " + itemDiscount.DiscountValue + " units!</BR>");
               break;
            case DiscountType.Percentage:
               Response.Write("</BR>It was a percentage discount of " + itemDiscount.DiscountValue + " percent! </BR>");
               break;
}
         //We can also show information about what PromoCode was used to trigger this discount (if one was required)
         if (itemDiscount.PromoCode != String.Empty)
{
Response.Write("</BR>PromoCode used to apply discount was: " + itemDiscount.PromoCode+ "</BR>");
}
      }
      //Show details of Order Level Discounts that applied to this LineItem
      foreach (DiscountApplicationRecord itemDiscount in item.OrderLevelDiscountsApplied)
      {
         //Similar to previous discount details
      }
}

   //Shipping Discounts (applied to Shipments created)
   foreach (Shipment shipment in orderForm.Shipments)
{
      //Show details of Shipping Discounts that applied to this Shipment
      foreach (ShippingDiscountRecord shippingDiscount in shipment.ShippingDiscounts)
{
         //Similar to previous discount details
      }
}
}

//Run the checkout pipeline for Payment verification, Redeeming promocodes and updating inventory
shoppingCart.RunPipeline(checkoutPipe);

//Now we can save the basket as an Order
PurchaseOrder order = shoppingCart.SaveAsOrder();

}