Code to Manipulate a Basket on an ASP.NET Web Form

Use the following code to manipulate a basket on an ASP.NET Web form. 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" %>

<script language="C#" >

    void Page_Load(object source, EventArgs args) {
        if (!IsPostBack) {
            UpdateBasket();
        }
    }

    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();
    }

    void AddBtn_Click(object source, EventArgs args) {

        OrderContext orders = CommerceContext.Current.OrderSystem;

        // Create and populate an instance of the LineItem class
        // that will be added to the order.  If a product variation
        // was being added, the VariantID would need to be specified
        // for the LineItem instance.
        LineItem item = new LineItem();
        item.ProductCatalog = "Adventure Works Catalog";
        item.ProductID = "AW013-08";
        item.Quantity = 1;

        try {

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

            // Try to add the new line item.  If it fails, we will
            // present the user with a friendly error message.
            shoppingCart.OrderForms["default"].LineItems.Add(item);

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

            shoppingCart.Save();

        } catch (Exception exc) {

            Status.Text = "Basket unavailable.";
            BasketListing.Visible = false;
            AddBtn.Visible = false;
            return;

        }

        UpdateBasket();
    }

</script>

<html>
<head>
<title>Modify a basket</title>
</head>

<body>

<form >

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

    <asp:Button
        id="AddBtn"
        text="Add Item"
        OnClick="AddBtn_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>
                </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>
                </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.