// GET: Checkout public ActionResult Index() { if (!Request.Cookies.AllKeys.Contains("cart")) { return(RedirectToAction("Index", "Cart")); } CheckoutModel model = new CheckoutModel(); if (User.Identity.IsAuthenticated) { SimonStoreEntities entities = new SimonStoreEntities(); var user = entities.Users.FirstOrDefault(X => X.AspNetUser.UserName == User.Identity.Name); if (user != null) { model.FirstName = user.FirstName; model.LastName = user.LastName; model.ContactEmail = user.AspNetUser.Email; model.ContactPhone = user.AspNetUser.PhoneNumber; model.BillingAddress.Street1 = user.BillingStreetAddress1; model.BillingAddress.Street2 = user.BillingStreetAddress2; model.BillingAddress.City = user.BillingCity; model.BillingAddress.State = user.BillingState; model.BillingAddress.PostalCode = user.BillingZip; model.ShippingAddress.Street1 = user.ShippingStreetAddress1; model.ShippingAddress.Street2 = user.ShippingStreetAddress2; model.ShippingAddress.City = user.ShippingCity; model.ShippingAddress.State = user.ShippingState; model.ShippingAddress.PostalCode = user.ShippingZip; } } return(View(model)); }
//This happens after the controller method is called creates cookie for the user to track what is in their cart public void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.Controller.ViewBag.CartItemCount = 0; if (filterContext.RequestContext.HttpContext.Request.Cookies.AllKeys.Contains("cart")) { using (SimonStoreEntities e = new SimonStoreEntities()) { HttpCookie cartCookie = filterContext.RequestContext.HttpContext.Request.Cookies["cart"]; if (cartCookie == null || cartCookie.Value == "") { filterContext.Controller.ViewBag.CartItemCount = 0; } else { var purchaseId = int.Parse(cartCookie.Value); int quantity = e.Orders.Single(x => x.OrderID == purchaseId).OrderedProducts.Sum(x => (x.Quantity ?? 0)); filterContext.Controller.ViewBag.CartItemCount = quantity; } } } }