Share via


Step 2: Implement the AddtoBasket.aspx Page

The AddtoBasket.aspx page adds a selected product to the users shopping basket (cart) and then redirects users to the Basket.aspx page.

In this step, we will implement the AddtoBasket.aspx page to add selected products to the basket and then redirect the user to the basket page.

  1. Click Start, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.

  2. In the Microsoft Development Environment [design] – Start Page screen, click Open Project.

  3. In the Open Project dialog box, select the NorthwindTraders project, and then click Open.

  4. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Default.aspx window, in the Solution Explorer window, right-click NorthwindTraders, point to Add, and then click Add Web Form.

  5. In the Add new Item - NorthwindTraders dialog box, in the Name box, type AddToBasket.aspx, and then click Open.

  6. In the NorthwindTraders – Microsoft Visual C# .NET [design] – AddToBasket.aspx window, in the Solution Explorer window, click View Code.

  7. Add the following references in the using section:

    using Microsoft.CommerceServer.Runtime;
    using Microsoft.CommerceServer.Runtime.Profiles;
    using Microsoft.CommerceServer.Runtime.Orders;
    using Microsoft.CommerceServer.Runtime.Pipelines;
    using Microsoft.CommerceServer.Runtime.Catalog;
    using Microsoft.CommerceServer.Runtime.Diagnostics;
    using Microsoft.CommerceServer.Runtime.Caching;
    
  8. Add the following code to the Page_Load() event handler.

             // Receive the item to add to the basket from the ProductID
             // (and SKU if the product has variants).
             // Validate login and redirect if user if not.
             Microsoft.CommerceServer.Runtime.Orders.Basket basket;
             CommerceContext ctx = CommerceContext.Current;
             Profile userProfile = null;
             bool isLoggedOn = ctx.AuthenticationInfo.IsAuthenticated();
    
             // This simple process does not carry the product that the
             // user was attempting to enter.  This would be good to do 
             // in a real site...
    
             if(!isLoggedOn) {Response.Redirect("login.aspx");}
    
             // Extract the userID to get the basket
             ProfileContext profContext = ctx.ProfileSystem;
             string userId = ctx.UserID;
             basket = CommerceContext.Current.OrderSystem.GetBasket(new Guid(userId));
    
             userProfile = profContext.GetProfile(ctx.UserID,"UserObject");
             //Extract the form variables that identify the product
             //These will be set as line item properties
    
             //Create a new line item
             LineItem li = new LineItem();
    
    
             li.ProductID = Request.Params["ProductID"];
    
             // Set the SKU if it is appropriate.  If the product
             // in question does not have variations, we can skip
             // this step.
             if (! ((Request.Params["SKU"] == null) || 
                (Request.Params["SKU"] == "")))
             {
                li.ProductVariantID = Request.Params["SKU"];
             }
             li.ProductCatalog = "BuildingMaterials1033";
             li.Quantity = 1;
    
             OrderContext oc = CommerceContext.Current.OrderSystem;
             OrderForm of;
    
             if (basket.OrderForms.Count == 0)
             {
                basket.OrderForms.Add(new OrderForm("default"));
             }
             of = basket.OrderForms["default"];   
             of.LineItems.Add(li);
             basket.Save();
    
             Response.Redirect("basket.aspx");
    

Copyright © 2005 Microsoft Corporation.
All rights reserved.