Partager via


How to Set the ShippingMethodId for a LineItem

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

A ShippingMethod object provides a way that customers can ship items that they purchase.

Before you run the Total pipeline to process a Basket object, you must assign a ShippingMethodId value to each LineItem object in the Basket. The Splitter pipeline component in the Total pipeline divides the line items into shipments, and the ShippingMethodRouter pipeline component calls the appropriate shipping component to calculate the shipping cost for each Shipment object.

To assign ShippingMethodIds to LineItem objects

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

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

  3. Add using directives for the following namespaces:

  4. Get the OrderContext object from the current CommerceContext object.

  5. Get the Shipping Methods from the OrderContext instance.

  6. Extract each shipping method from the dataset that contains the valid shipping methods.

  7. Ask the customer how to ship each item.

  8. Assign a ShippingMethodId value to each LineItem.

Example

The following code example illustrates how to add a ShippingMethodId to each LineItem in a Basket.

This example builds on the example in the topic How to Add a LineItem to an OrderForm.

using System;
using System.Collections;
using System.Data;
using System.Web;
using Microsoft.CommerceServer.Runtime;
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 would get the ID of the current
        // user from the CommerceContext object.

        Guid userID = Guid.NewGuid();

        // Create a new Basket with an OrderForm and two
        // LineItem objects.

        Basket myBasket = OrderContext.Current.GetBasket(userID, "primary");
        OrderForm orderForm = new OrderForm();
        LineItem item1 = new LineItem("Holiday", "ABC-123", "", 1);
        LineItem item2 = new LineItem("Holiday", "XYZ-789", "", 5);

        // Get the valid shipping methods.

        OrderContext orderContext = CommerceContext.Current.OrderSystem;
        DataSet validShippingMethods = orderContext.GetShippingMethods("en-US");

        // Ship the first item by using the first shipping
        // method, and ship the second item by using the 
        // second shipping method.

        DataRowCollection shippingMethodRows = validShippingMethods.Tables[0].Rows;
        try
        {
            item1.ShippingMethodId = (Guid)(shippingMethodRows[0]["ShippingMethodId"]);
            item2.ShippingMethodId = (Guid)(shippingMethodRows[1]["ShippingMethodId"]);
        }
        catch (IndexOutOfRangeException ex)
        {
            Response.Write("Fewer than two shipping methods.<br>");
            return;
        }

        // Add the LineItem objects to the OrderForm.

        orderForm.LineItems.Add(item1);
        orderForm.LineItems.Add(item2);

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

        myBasket.OrderForms.Add(orderForm);

        // Validate that the shipping methods were added by
        // getting the ShippingMethodId for each LineItem, and
        // then displaying the ShippingMethodName that is 
        // associated with the ShippingMethodId.

        foreach (OrderForm of in myBasket.OrderForms)
        {
            foreach (LineItem item in of.LineItems)
            {
                Response.Write("product ID: " + item.ProductId.ToString());
                try
                {
                    DataRow[] results = validShippingMethods.Tables[0].Select("ShippingMethodId" + " = '" + item.ShippingMethodId.ToString() + "'");
                    Response.Write(" shipping method: " + results[0]["ShippingMethodName"].ToString() + "<br>");
                }
                catch (Exception ex)
                {
                    Response.Write("<br>No valid shipping method with ShippingMethodId = " + item.ShippingMethodId.ToString() + ".<br>");
                    return;
                }
            }
        }

        // Save the Basket.

        myBasket.Save();
  }
}

To run this sample, you must have configured at least two shipping methods for your Commerce Server application. For more information about how to configure shipping methods, see How to Create a Shipping Method.

See Also

Other Resources

Orders Runtime Object Model

How to Add a LineItem to an OrderForm