📄 shoppingcartmanager.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using Commerce.Providers;
/// <summary>
/// The controller for the shopping cart
/// </summary>
public class ShoppingCartManager {
/// <summary>
/// Returns the users name or a GUID, depending if they are logged in.
/// </summary>
/// <returns></returns>
static string GetUserName() {
string sUserName = "";
if (HttpContext.Current.User.Identity.IsAuthenticated) {
sUserName = HttpContext.Current.User.Identity.Name;
} else {
//we'll tag them with an anon userName until they register
if (HttpContext.Current.Request.Cookies["shopperID"] != null) {
sUserName = HttpContext.Current.Request.Cookies["shopperID"].Value;
} else {
//if we have never seen them, tag them with a cookie so we can track
//their basket.
sUserName = System.Guid.NewGuid().ToString();
HttpContext.Current.Response.Cookies["shopperID"].Value = sUserName;
HttpContext.Current.Response.Cookies["shopperID"].Expires = DateTime.Today.AddDays(365);
}
}
return sUserName;
}
/// <summary>
/// Adds an item to the cart
/// </summary>
/// <param name="productID"></param>
/// <param name="itemName"></param>
/// <param name="itemNumber"></param>
/// <param name="price"></param>
/// <param name="quantity"></param>
public static void AddItem(int productID, int quantity, string modelName,
string modelNumber, string description, double price, string sku,double weight) {
string userName = GetUserName();
bool productIsActive = CatalogManager.IsActive(productID);
if (!productIsActive) {
throw new Exception("This product is no longer active");
} else {
ShoppingCartProvider.Instance.CartAddItem(userName, productID, quantity, modelName, modelNumber, description, price, sku,weight);
}
}
/// <summary>
/// Removes an item from the cart
/// </summary>
/// <param name="ItemNumber"></param>
public static void RemoveItem(int productID) {
ShoppingCartProvider.Instance.CartAdjustQuantity(GetUserName(), productID, 0);
}
/// <summary>
/// Adjusts the quantity of an item in the cart to the new quantity
/// </summary>
/// <param name="ItemNumber"></param>
/// <param name="newQuantity"></param>
public static void AdjustQuantity(int productID, int newQuantity) {
ShoppingCartProvider.Instance.CartAdjustQuantity(GetUserName(), productID, newQuantity);
}
/// <summary>
/// Loads a simple cart object and it's items
/// </summary>
/// <returns></returns>
public static ShoppingCart GetCart() {
IDataReader rdr = ShoppingCartProvider.Instance.CartGetCurrentCart(GetUserName());
ShoppingCart cart = new ShoppingCart();
cart.Load(rdr);
//the items are the next bits
if (rdr.NextResult()) {
cart.Items = new ShoppingCartItems();
DataRow dr;
int counter = 0;
while(rdr.Read()) {
dr = cart.Items.NewRow();
dr["productID"] = (int)rdr["productID"];
dr["ModelName"] = rdr["ModelName"].ToString();
dr["ModelNumber"] = rdr["ModelNumber"].ToString();
dr["Price"] = Convert.ToDouble(rdr["price"].ToString());
dr["LineTotal"] = Convert.ToDouble(rdr["lineTotal"].ToString());
dr["Quantity"] = (int)rdr["quantity"];
cart.Items.Rows.Add(dr);
}
}
rdr.Close();
return cart;
}
public static IDataReader GetCartItems() {
return ShoppingCartProvider.Instance.CartGetCartItems(GetUserName());
}
/// <summary>
/// Transfers a cart from one user to another. This is used mainly with anonymous shopping,
/// where an anonymous user logs in and doesn't want to lose their cart.
/// </summary>
/// <param name="fromUserName"></param>
/// <param name="toUserName"></param>
public static void Transfer(string fromUserName, string toUserName){
ShoppingCartProvider.Instance.CartTransfer(fromUserName,toUserName);
}
public static void UpdateCartPrices(int productID, double newPrice) {
ShoppingCartProvider.Instance.UpdatePrices(productID, newPrice);
}
public static void RemoveAllProducts(int productID) {
ShoppingCartProvider.Instance.RemoveProducts(productID);
}
public static void UpdateCartProducts(int productID,double newPrice,double newWeight,string newName,string newNumber,string newSKU) {
ShoppingCartProvider.Instance.UpdateProducts(productID, newPrice, newWeight, newName, newNumber, newSKU);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -