다음을 통해 공유


How to Add a LineItem to an OrderForm

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

A LineItem represents a product that the user placed in a shopping cart. A LineItem identifies both the product that it represents and the quantity of that product that the user placed in the shopping cart.

A LineItem is contained in a LineItemCollection, which is contained, indirectly, in a Basket.

To add a LineItem to a Basket

  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. Create or retrieve a Basket.

  5. Create or retrieve an OrderForm.

  6. Create a new LineItem and add the LineItem to the LineItemCollection in the OrderForm.

  7. Save the updated Basket.

Example

The following code shows an example of how to add two LineItems to a Basket.

This example builds on the example in the topic How to Add an OrderForm to a 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 CommerceServer site that
        // implemented logons, we could get the ID of the current
        // user from the CommerceContext.

        Guid userID = Guid.NewGuid();

        // Create a new Basket named "wish list".

        Basket myBasket = OrderContext.Current.GetBasket(userID, "wish list");

        // Create a new OrderForm.

        OrderForm orderForm = new OrderForm();

        // Create two LineItems, and add them to the OrderForm.

        LineItem item1 = new LineItem("Holiday", "ABC-123", "", 1);
        LineItem item2 = new LineItem("Holiday", "XYZ-789", "", 5);
        orderForm.LineItems.Add(item1);
        orderForm.LineItems.Add(item2);

        // Add the OrderForm to the OrderFormCollection
        // in the Basket.

        myBasket.OrderForms.Add(orderForm);

        // Validate that we've really added the LineItems by
        // displaying the contents of the Basket.

        foreach (OrderForm theOrder in myBasket.OrderForms)
        {
            foreach (LineItem theItem in theOrder.LineItems)
            {
                Response.Write("product ID: " + theItem.ProductId.ToString());
                Response.Write(" quantity: " + theItem.Quantity.ToString() + "<br>");

            } // end foreach LineItem
        } // end foreach OrderForm

        // Save the Basket.

        myBasket.Save();

    } // end Page_Load method

} // end Default_aspx class

See Also

Other Resources

Working with Baskets

How to Add Items to a Basket

Orders Runtime Object Model