Code to Set Shipping Methods on a Basket on an ASP.NET Web Form

Use the following code to set shipping methods on a basket. The code sample should be used within a Commerce Server project. For more information about creating a Commerce Server project, see Creating a Commerce Project.

<%@ Page Language="C#" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime.Orders" %>
<%@ Import Namespace="System.Data" %>

<script language="C#" >

    DataSet shippingMethods;

    void Page_Load(object source, EventArgs args) {
        if (!IsPostBack) {
            UpdateBasket();
            shippingMethods = GetShippingMethods();
            Page.DataBind();
        }
    }

    void UpdateBasket() {

        OrderContext orders = CommerceContext.Current.OrderSystem;

        // This requires that the CommerceContext.UserID property already
        // be set.  It can either be set dynamically if using Commerce Server
        // authentication, or manually.
        Basket shoppingCart =
            orders.GetBasket(new System.Guid(CommerceContext.Current.UserID));

        // If the shopping cart has just been created, no order forms
        // will exist and thus, it is unusable.  So a new, default
        // order form must be added.
        if (shoppingCart.OrderForms.Count == 0) {
            shoppingCart.OrderForms.Add(new OrderForm("default"));
        }

        // execute the product pipeline to ensure we have the latest product
        // information in the basket
        shoppingCart.RunPipeline(new PipelineInfo("basket"));

        // Bind the default order form to the Repeater control used for
        // displaying its contents.
        BasketListing.DataSource = shoppingCart.OrderForms["default"].LineItems;
        BasketListing.DataBind();

        // Update information about the order.
        SubTotal.Text = String.Format("{0:C}", shoppingCart.SubTotal);

        // This data will not be available after running the basket pipeline
        // alone.
        Tax.Text = String.Format("{0:C}", shoppingCart.TaxTotal);
        Total.Text = String.Format("{0:C}", shoppingCart.Total);

        shoppingCart.Save();

    }

    DataSet GetShippingMethods() {

        // The filter used in looking up the shipping methods.
        string filter = "enabled=1 and language_id='en-US'";

        // Create an array that lists the columns we want to return.
        string[] columns = {"shipping_method_id", "shipping_method_name"};

        // Get a reference to the current instance of the OrderContext.
        OrderContext orders = CommerceContext.Current.OrderSystem;

        // Retrieve the available shipping methods, returning only the columns
        // specified previously.  Note that if there are no available shipping
        // methods, an empty DataSet will be returned.
        return orders.GetShippingMethods(filter, "shipping_method_name", columns);

    }

    void ReCalcBtn_Click(object source, EventArgs args) {

        OrderContext orders = CommerceContext.Current.OrderSystem;

        // This requires that the CommerceContext.UserID property already
        // be set.  It can either be set dynamically if using Commerce Server
        // authentication, or manually.
        Basket shoppingCart =
            orders.GetBasket(new System.Guid(CommerceContext.Current.UserID));

        // Iterate through the line items and the selected values, and then
        // associate each selected shipping method with the appropriate
        // line item.
        for (int i=0; i< BasketListing.Items.Count; i++) {
            DropDownList ShippingMethods =
                BasketListing.Items[i].FindControl("ShippingMethodList") as DropDownList;

            // Retrieve the current line item based on its position in the Repeater.
            LineItem item = shoppingCart.OrderForms["default"].LineItems[i];

            // Associate the selected shipping method with the line item.
            item.ShippingMethodID = "{" + ShippingMethods.SelectedItem.Value + "}";
            item.ShippingMethodName = ShippingMethods.SelectedItem.Text;
        }

        shoppingCart.Save();
    }

</script>

<html>
<head>
<title>Set Shipping Methods</title>
</head>

<body>

<form >

    <asp:Label
        id="Status"
        />
    <br/>

    <asp:Button
        id="ReCalcBtn"
        text="Recalculate"
        OnClick="ReCalcBtn_Click"
        />
    &nbsp;
    <br/>

    <asp:Repeater
        id="BasketListing"
        >

        <HeaderTemplate>
            <table width="100%">
                <tr>
                    <td width="35">
                        <b>Qty</b>
                    </td>
                    <td width="175">
                        <b>Product</b>
                    </td>
                    <td width="50">
                        <b>Price</b>
                    </td>
                    <td>
                        <b>Shipping Option</b>
                    </td>
                </tr>
        </HeaderTemplate>

        <ItemTemplate>
                <tr>
                    <td width="35">
                        <%# DataBinder.Eval(Container.DataItem, "Quantity") %>
                    </td>
                    <td width="175">
                        <%# DataBinder.Eval(Container.DataItem, "[_product_name]") %>
                    </td>
                    <td width="50">
                        <%# DataBinder.Eval(Container.DataItem, "[_product_cy_list_price]", "{0:C}") %>
                    </td>
                    <td>
                    <asp:DropDownList
                        id="ShippingMethodList"
                        DataSource='<%# shippingMethods %>'
                        DataTextField="shipping_method_name"
                        DataValueField="shipping_method_id"
                        />
                </tr>
        </ItemTemplate>

        <FooterTemplate>
            </table>
        </FooterTemplate>

    </asp:Repeater>

    <table border="0" width="100%">
        <tr>
            <td colspan="4">
                <hr>
            </td>
        </tr>
        <tr>
            <td width="52">
                &nbsp;<br/>
            </td>
            <td width="225" colspan="2" align="left">
                <b>Subtotal</b>
            </td>
            <td align="right">
                <asp:label id="SubTotal"  />
            </td>
        </tr>
        <tr>
            <td width="52">
                &nbsp;<br/>
            </td>
            <td width="225" colspan="2" align="left">
                <b>Tax</b>
            </td>
            <td align="right">
                <asp:label id="Tax"  />
            </td>
        </tr>
        <tr>
            <td width="52">
                &nbsp;<br/>
            </td>
            <td width="225" colspan="2" align="left">
                <b>Grand Total</b>
            </td>
            <td align="right">
                <b><asp:label id="Total"  /></b>
            </td>
        </tr>
    </table>

</form>

</body>
</html>

See Also

Basket Class (BCL)

CommerceContext Class (BCL)

OrderContext Class (BCL)

OrderForm Class (BCL)

OrderGroup Class (BCL)

Copyright © 2005 Microsoft Corporation.
All rights reserved.