Share via


How to Add an OrderAddress to a Basket

An OrderAddress represents a location that items in the Basket will be shipped to. Since the items could be shipped to more than one location -- for example, half of the items could be shipped to the customer's home, and the other half could be shipped to the customer's office -- OrderAddresses are contained in an OrderAddressCollection. The OrderAddressCollection is contained in a Basket.

To add an OrderAddress to a Basket

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

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

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

  4. Create or retrieve a Basket.

  5. Create a new OrderAddress and add it to the OrderAddressCollection in the Basket.

  6. Save the updated Basket.

Example

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

This example builds on the example in the topic How to Create 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 "current".

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

        // Create a new OrderAddress, and add it to the Basket.

        OrderAddress myAddress = new OrderAddress();
        myAddress.Line1 = "One Microsoft Way";
        myAddress.City = "Redmond";
        myAddress.State = "WA";
        myAddress.CountryName = "USA";
        myBasket.Addresses.Add(myAddress);

        // Validate that we've really added the OrderAddress by
        // displaying the OrderAddresses for the Basket.

        foreach (OrderAddress theAddress in myBasket.Addresses)
        {
            Response.Write(theAddress.Line1 + "<br>");
            Response.Write(theAddress.City + "<br>");
            Response.Write(theAddress.State + "<br>");
            Response.Write(theAddress.CountryName + "<br>");

        } // end foreach

        // 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