/// <summary> /// Add a product to the guest cookie /// /// The cookie template is id-size-quantity /// /// </summary> /// <param name="cookies">HttpCookieCollection from the controller. Usually referenced as Request.Cookies</param> /// <param name="id">The id of the new product to be added to the shopping cart</param> /// <param name="size">The size of the product to be added to the shopping cart</param> /// <param name="quantity">The quantity of this product to be added to the shopping cart</param> /// <returns>The new cookie to be replaced</returns> public static HttpCookie AddProductToCart(HttpCookieCollection cookies, int id, ProductSize size, int quantity) { // Get the Shoppin Cart Cookie HttpCookie shoppingCartCookie = cookies["shoppingCart"]; string cookie = ""; // If there is atleast one product in the Shopping Cart if (shoppingCartCookie != null && shoppingCartCookie.Value != "") { // If the product and size already exists, just change the quantity if (shoppingCartCookie.Value.Contains(id + "-" + size.ToString())) { foreach (string item in shoppingCartCookie.Value.Split(',')) { if (item.Contains(id + "-" + size.ToString())) { string q = item.Replace(id + "-" + size.ToString() + "-", ""); if (Int32.TryParse(q, out int x)) { cookie = shoppingCartCookie.Value.Replace(item, id + "-" + size.ToString() + "-" + (x + quantity)); } } } string test = shoppingCartCookie.Value.Split(',')[0].Replace(id + "-" + size.ToString(), ""); } else { // The product doesn't exist, so create it cookie = shoppingCartCookie.Value + "," + cookie + id + "-" + size + "-" + quantity + ";"; } } else { // This is the first product to be added to the cookie (cookie was empty) cookie = cookie + id + "-" + size + "-" + quantity + ";"; } HttpCookie newCookie = new HttpCookie("shoppingCart", cookie) { Expires = DateTime.Now.AddMonths(6) }; return(newCookie); }
public static string Display(this ProductSize size) { return(size.ToString().SplitCamelCase()); }