//把每一筆購買商品記錄到order中 public int CreateOrder(Order order, decimal disCount) { decimal orderTotal = 0; var cartItems = GetCartItems(); //先取得所有商品 // Iterate over the items in the cart, // adding the order details for each foreach (var item in cartItems) //每筆存成orderDetail { var orderDetail = new OrderDetail { AlbumId = item.AlbumId, OrderId = order.OrderId, UnitPrice = item.Album.Price, Quantity = item.Count }; // Set the order total of the shopping cart orderTotal += (item.Count * item.Album.Price)*disCount; //累計取得訂單總價 storeDB.OrderDetails.Add(orderDetail); } // Set the order's total to the orderTotal count storeDB.Orders.Single(o => o.OrderId == order.OrderId).Total = orderTotal; // Save the order storeDB.SaveChanges(); // Empty the shopping cart EmptyCart(); // Return the OrderId as the confirmation number return order.OrderId; }
public ActionResult AddressAndPayment(FormCollection values) { var order = new Order { }; TryUpdateModel(order); try { if (string.Equals(values["PromoCode"], PromoCode, StringComparison.OrdinalIgnoreCase) == true) { order.Username = User.Identity.Name; order.OrderDate = DateTime.Now; //Save Order storeDB.Orders.Add(order); storeDB.SaveChanges(); //Process the order var cart = ShoppingCart.GetCart(this.HttpContext); cart.CreateOrder(order, 0.8m); return RedirectToAction("OrderDetails", new { id = order.OrderId}); //return RedirectToAction("OrderDetail", order); //return View(order); } else { order.Username = User.Identity.Name; order.OrderDate = DateTime.Now; //Save Order storeDB.Orders.Add(order); storeDB.SaveChanges(); //Process the order var cart = ShoppingCart.GetCart(this.HttpContext); cart.CreateOrder(order,1); return RedirectToAction("OrderDetails", new { id = order.OrderId}); } } catch { //Invalid - redisplay with errors return View(order); } }