CS 2007 - Recurring Orders Sample Code

Here is a code sample which illustrates how you can achieve a common scenario on a B2B / B2C eCommerce site – placing repeated Orders containing the same set of products over and over again, without the hassle of having to search for and save the set of products you are interested in (think of an someone placing an Order for the same set of supplies every few months for e.g.).

 

In the Commerce Server 2007 Order capture system you can achieve this in a variety of ways, including iterating over and saving the collections you are interested in, in your own module (especially if you have an extended Order system where you want to control this completely). The sample I provide below uses the Named Baskets feature of CS 2007 and I think this is most appropriate here, because it gives a lot of flexibility (you can have as many wish lists as you want for each user browsing the site) and ease of site code development.

 

We first create a named Basket called “WishList” and add some products to it (in a helper function). We save this “WishList” and use the Add(OrderGroup) method to add all the items in this wish list to new Baskets created for the user (same userId Guid has to be used). Once the items are present in a new Basket, we can do the various operations such as running the pipelines etc. before checking out completely via the SaveAsOrder method.

 

                Basket myWishList = OrderContext.Current.GetBasket(uid, "WishList");

                AddOrderFormsWithValidLineItems(1, myWishList, 2);

                myWishList.Save();

                Basket myBasket1 = CommerceContext.Current.OrderSystem.GetBasket(uid, "myBasket1");

                myBasket1.Add(myWishList);

                //Run the pipelines etc. and then save as order

                PurchaseOrder myOrder1 = myBasket1.SaveAsOrder();

                Basket myBasket2 = CommerceContext.Current.OrderSystem.GetBasket(uid, "myBasket2");

                myBasket2.Add(myWishList); //Same items are being checked out again

                //Run the pipelines etc. and then save as order

                PurchaseOrder myOrder2 = myBasket2.SaveAsOrder();

Hope you find the sample useful.