Exemple #1
0
        public int CreateOrder(Order order)
        {
            decimal orderTotal = 0;

            var cartItems = GetCartItems();

            // Iterate over the items in the cart, adding the order details for each
            foreach (var item in cartItems)
            {
                var orderDetail = new OrderDetail
                {
                    ProductId = item.ProductId,
                    OrderId = order.OrderId,
                    UnitPrice = item.Product.Price,
                    Quantity = item.Count
                };

                // Set the order total of the shopping cart
                orderTotal += (item.Count * item.Product.Price);

                storeDB.OrderDetails.Add(orderDetail);

            }

            // Set the order's total to the orderTotal count
            order.Total = orderTotal;

            // Save the order
            storeDB.SaveChanges();

            // Empty the shopping cart
            EmptyCart();

            // Return the OrderId as the confirmation number
            return Convert.ToInt32(order.OrderId);
        }
Exemple #2
0
        public ActionResult Order(Order order)
        {
            try
            {
                if (ValidateInput(order))
                {
                    order.OrderDate = DateTime.UtcNow;
                    order.Status = (byte)OrderStatus.New;
                    order.OrderId = Guid.NewGuid();
                    order.Description = order.Description ?? "";
                    ProductOrderViewModel viewData = (ProductOrderViewModel)Session["ProductOrderViewModel"];
                    order.Total = viewData.CartTotal;
                    foreach (var cart in viewData.CartItems)
                    {
                        OrderDetail orderDetail = new OrderDetail();
                        orderDetail.OrderDetailId = Guid.NewGuid();
                        orderDetail.Quantity = cart.Count;
                        orderDetail.UnitPrice = cart.Product.Price;
                        orderDetail.ProductId = cart.ProductId;
                        orderDetail.OrderId = order.OrderId;
                        db.OrderDetails.Add(orderDetail);
                    }

                    db.Orders.Add(order);
                    db.SaveChanges();
                    Session["ProductOrderViewModel"] = null;
                    return Json(new Order { IsSuccess = true, ErrorMessage = "" });
                }
            }
            catch(Exception ex)
            {
                return Json(new Order { IsSuccess = false, ErrorMessage = ErrorMessage });
            }
            return Json(new Order { IsSuccess = false, ErrorMessage = ErrorMessage });
        }