How to Create a New Basket from a Recurring Order

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

You can create a basket with a name to represent an order that the customer will place more than once. Save the items from the recurring order in the named basket. Every time that the customer places the same order, create a new basket and copy the items from the saved basket to the new basket.

To create a basket from a recurring order

  1. In Visual Studio, create a new Commerce Server Web application.

  2. Add the Microsoft.CommerceServer.Runtime resource to the Web application.

  3. Add a using directive for the Microsoft.CommerceServer.Runtime.Orders namespace.

  4. Display a list of the customer's saved baskets, and ask the customer which basket contains the items to add to the current order.

  5. Create a new Basket object.

  6. Call the Add method of the new basket and pass the name of the saved basket that the user selected.

Example

The following code example illustrates how to create a new Basket object and adds all line items from a basket named "monthly" to the new basket.

using System;
using System.Collections;
using System.Web;
using Microsoft.CommerceServer.Runtime.Orders;

public partial class Default_aspx : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // For this example, create a new GUID for the user's ID.  
        // If this were part of a full Commerce Server site that
        // implemented logons, you could get the ID of the current
        // user from the CommerceContext object.

        Guid userID = Guid.NewGuid();

        // This section of code creates a Basket object that has
        // ten line items to represent an order that the customer
        // places every month. In a real scenario, you would get
        // a list of the customer's baskets, ask the customer to
        // select the basket to create an order from, and get
        // the correct Basket object by calling the GetBasket method
        // of the OrderContext.Current object.

        Basket recurringBasket = OrderContext.Current.GetBasket(userID, "monthly");
        OrderForm orderForm = new OrderForm();
        LineItem item1 = new LineItem("supplies", "ABC-123", "", 25);
        LineItem item2 = new LineItem("supplies", "DEF-456", "", 50);
        LineItem item3 = new LineItem("supplies", "GHI-789", "", 75);
        LineItem item4 = new LineItem("supplies", "JKL-012", "", 100);
        LineItem item5 = new LineItem("supplies", "MNO-345", "", 125);
        LineItem item6 = new LineItem("supplies", "PQR-678", "", 150);
        LineItem item7 = new LineItem("supplies", "STU-901", "", 175);
        LineItem item8 = new LineItem("supplies", "VWY-234", "", 200);
        LineItem item9 = new LineItem("supplies", "WZA-567", "", 225);
        LineItem item10 = new LineItem("supplies", "BCD-890", "", 250);
        orderForm.LineItems.Add(item1);
        orderForm.LineItems.Add(item2);
        orderForm.LineItems.Add(item3);
        orderForm.LineItems.Add(item4);
        orderForm.LineItems.Add(item5);
        orderForm.LineItems.Add(item6);
        orderForm.LineItems.Add(item7);
        orderForm.LineItems.Add(item8);
        orderForm.LineItems.Add(item9);
        orderForm.LineItems.Add(item10);
        recurringBasket.OrderForms.Add(orderForm);
        recurringBasket.Save();

        // Create a new Basket object to represent the current 
        // order.

        Basket newBasket = OrderContext.Current.GetBasket(userID, "current");

        // Add the items from the recurring basket to the new
        // basket.

        newBasket.Add(recurringBasket);

        // Save the new basket.

        newBasket.Save();

        // Validate that the new basket contains the ten items
        // that were in the recurring basket by displaying the
        // contents of the new basket.

        foreach (OrderForm of in newBasket.OrderForms)
        {
            foreach (LineItem item in of.LineItems)
            {
                Response.Write("product ID: " + item.ProductId.ToString());
                Response.Write(" quantity: " + item.Quantity.ToString() + "<br/>");

            } 
        } 
    } 
}

See Also

Other Resources

How to Place Recurring Orders

How to Create a Basket

Working with Baskets