How to Convert an Anonymous Basket to a User's Basket
For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.
A basket that belongs to a user who has not signed in is called an anonymous basket. A user might add items to a basket before signing in to your site. Later, the user could sign in either by using an existing account or by creating a new account. At this point, you have to associate the anonymous basket with the registered user's account.
To convert an anonymous basket to a user's basket
Create a new Basket object to represent the registered user's basket.
Add the items from the anonymous basket to the user's basket by calling the Add method of the new Basket object, and provide the anonymous Basket object as a parameter.
Create a PipelineInfo object to represent the Basket pipeline.
Add the user's profile to the PipelineInfo object.
Run the Basket pipeline.
Save the Basket object that represents the registered user's basket.
Delete the Basket object that represents the anonymous basket.
Example
The following example converts an anonymous basket to a basket that belongs to a registered user.
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 ID
// of the anonymous basket.
Guid anonymousUserID = Guid.NewGuid();
// Create a Basket object to represent the anonymous
// user's basket. Add two items to the basket.
Basket anonymousBasket = OrderContext.Current.GetBasket(anonymousUserID, "default");
OrderForm orderForm = new OrderForm();
LineItem item1 = new LineItem("clothing", "ABC-123", "", 2);
LineItem item2 = new LineItem("clothing", "DEF-456", "", 1);
orderForm.LineItems.Add(item1);
orderForm.LineItems.Add(item2);
anonymousBasket.OrderForms.Add(orderForm);
// Assume that the user has now signed in. For this
// example, create a new GUID for the user's ID. If this
// were part of a full Commerce Server site, you would get
// the ID of the current user from the CommerceContext
// object.
Guid registeredUserID = Guid.NewGuid();
// Create a Basket object to represent the registered
// user's basket.
Basket registeredUserBasket = OrderContext.Current.GetBasket(registeredUserID, "default");
// Add the items from the anonymous user's basket to the
// registered user's basket.
registeredUserBasket.Add(anonymousBasket);
// Create the basket pipeline.
PipelineInfo pipeline = new PipelineInfo("basket", OrderPipelineType.Basket);
// In a full Commerce Server site, you would add the
// registered user's profile to the PipelineInfo
// object by uncommenting the following line:
// pipeline.Profiles.Add("UserObject", CommerceContext.Current.UserProfile);
registeredUserBasket.RunPipeline(pipeline);
// Save the registered user's basket and delete the
// anonymous basket.
registeredUserBasket.Save();
anonymousBasket.Delete();
// Validate that the registered user's basket contains
// the items that were in the anonymous basket by
// displaying the contents of the basket.
foreach (OrderForm of in registeredUserBasket.OrderForms)
{
foreach (LineItem item in of.LineItems)
{
Response.Write("product ID: " + item.ProductId.ToString());
Response.Write(" quantity: " + item.Quantity.ToString() + "<br />");
}
}
}
}
To run this sample, you must configure the Basket pipeline for your site and configure your site to use MessageManager. For more information about how to configure the Basket pipeline, see pipelines Element. For more information about how to use MessageManager, see messageManager Element.
Compiling the Code
To run this code example, create a Commerce Server Web application and add a reference to Microsoft.CommerceServer.Runtime.dll.