Share via


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();

}

Comments

  • Anonymous
    January 05, 2006
    Nihitk,

    Very cool.

    Could you explain or post a little more detail on your statment "You can also have multiple OrderForms per Basket if required for e.g. to group orders from different vendors"?

    This feature sounds like an interesting way to split orders without a lot of custom code.

    Jeff

  • Anonymous
    January 05, 2006
    Hi Jeff,

    Will do that, i.e. explain some more on the use of multiple OrderForms in another post. Thanks for the feedback.

    Nihit

  • Anonymous
    January 17, 2006
    The comment has been removed

  • Anonymous
    January 19, 2006
    Andy - The property to be used in both cases is the same, DiscountValue, which is just a number. This number is either a dollar amount if the TypeOfDiscount is CurrencyValue or specifies the %age discount if the TypeOfDiscount is Percentage. Does that make sense (it sounds confusing so probably re-read it and see if it sounds correct or not).

  • Anonymous
    July 09, 2007
    The comment has been removed

  • Anonymous
    July 09, 2007
    Hi Minah, Have you taken a look at the MinMaxShipping sample included with the CS 2007 SDK? That should give you good ideas around this. You can also see this webcast for more details around the Order system and pipeline development etc. http://blogs.msdn.com/commerce/archive/2007/06/04/friday-june-8th-2007-7-00am-8-30am-live-from-redmond-commerce-server-2007-order-system-development-by-vinod-kumar.aspx Thansk, Nihit

  • Anonymous
    July 09, 2007
    The comment has been removed

  • Anonymous
    July 10, 2007
    I would suggest you post your queries to the Newsgroups - that should get you more responses from people who might have tried similar things. Thanks, Nihit