示例#1
0
        protected void addToCartButton_Click(object sender, EventArgs e)
        {
            int    id = Int32.Parse(Request.QueryString["id"]), Quantity = 0;
            string messageError = "";

            messageError = CartController.Add(addToCartQuantityTxt.Text, id);

            if (messageError != "")
            {
                labelErrorAddToCart.Text = messageError;
            }
            else
            {
                User user = (User)Session[Constant.Keys.AUTH];
                Quantity = int.Parse(addToCartQuantityTxt.Text);
                Product product = ProductHandler.get(id);
                if (CartHandler.get(user.ID, product.ID) != null)
                {
                    CartHandler.updateQuantity(user.ID, id, Quantity);
                }
                else
                {
                    CartInformation currentProduct = new CartInformation();
                    currentProduct.ID       = product.ID;
                    currentProduct.Name     = product.Name;
                    currentProduct.Price    = product.Price;
                    currentProduct.Quantity = Quantity;
                    currentProduct.SubTotal = currentProduct.Quantity * currentProduct.Price;
                    CartHandler.add(user.ID, id, Quantity);
                }
                var redirecTo = Constant.Routes.VIEW_CART_ROUTE;
                Response.Redirect(redirecTo);
            }
        }
        public ActionResult CreateOrder(CartInformation cartInformation)
        {
            var shoppingCart = LoadShoppingCart();

            if (orderService.createOrder(shoppingCart, cartInformation))
            {
                TempData["msg"] = "Order success!";
                ClearCart();
            }
            return(Redirect("/Products"));
        }
        public ActionResult CreateOrder(CartInformation cartInfo)
        {
            // load cart trong session.
            var shoppingCart = LoadShoppingCart();

            if (shoppingCart.GetCartItems().Count <= 0)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Bad request"));
            }
            // chuyển thông tin shopping cart thành Order.
            var order = new Order
            {
                TotalPrice    = shoppingCart.GetTotalPrice(),
                MemberId      = 1,
                PaymentTypeId = (PaymentType)Enum.Parse(typeof(PaymentType), cartInfo.PaymentTypeId),
                ShipName      = cartInfo.ShipName,
                ShipPhone     = cartInfo.ShipPhone,
                ShipAddress   = cartInfo.ShipAddress,
                OrderDetails  = new List <OrderDetail>()
            };

            // Tạo order detail từ cart item.
            foreach (var cartItem in shoppingCart.GetCartItems())
            {
                var orderDetail = new OrderDetail()
                {
                    ProductId = cartItem.Value.ProductId,
                    OrderId   = order.Id,
                    Quantity  = cartItem.Value.Quantity,
                    UnitPrice = cartItem.Value.Price
                };
                order.OrderDetails.Add(orderDetail);
            }
            db.Orders.Add(order);
            db.SaveChanges();
            ClearCart();
            //// lưu vào database.
            //var transaction = db.Database.BeginTransaction();
            //try
            //{

            //    transaction.Commit();
            //}
            //catch (Exception e)
            //{
            //    Console.WriteLine(e);
            //    transaction.Rollback();
            //}
            return(RedirectToAction("DisplayCartAfterCreateOrder", new { orderId = order.Id }));
        }
示例#4
0
        public static List <CartInformation> getCartCustomView(int UserID)
        {
            var cart_List            = CartRepo.getCartCustomView(UserID);
            var cartInformation_List = new List <CartInformation>();

            for (int i = 0; i < cart_List.Count; i++)
            {
                CartInformation CInfo = new CartInformation();
                CInfo.ID       = cart_List[i].ProductID;
                CInfo.Name     = ProductHandler.getName(cart_List[i].ProductID);
                CInfo.Price    = ProductHandler.getPrice(cart_List[i].ProductID);
                CInfo.Quantity = getQuantity(UserID, cart_List[i].ProductID);
                CInfo.SubTotal = (ProductHandler.getPrice(cart_List[i].ProductID) * cart_List[i].Quantity);
                cartInformation_List.Add(CInfo);
            }
            return(cartInformation_List);
        }
示例#5
0
        public bool createOrder(ShoppingCart cart, CartInformation cartInformation)
        {
            if (cart.GetCartItems().Count == 0)
            {
                return(false);
            }
            var order = new Order();

            order.MemberId      = 1;
            order.PaymentTypeId = cartInformation.PaymentTypeId;
            order.ShipName      = cartInformation.ShipName;
            order.ShipPhone     = cartInformation.ShipPhone;
            order.ShipAddress   = cartInformation.ShipAddress;
            order.TotalPrice    = cart.GetTotalPrice();
            var  orderDetails = new List <OrderDetail>();
            bool existError   = false;

            foreach (var item in cart.GetCartItems())
            {
                Product product = db.Products.Find(item.ProductId);
                if (product == null)
                {
                    existError = true;
                    break;
                }
                var orderDetail = new OrderDetail();
                orderDetail.ProductId = product.Id;
                orderDetail.OrderId   = order.Id;
                orderDetail.Quantity  = item.Quantity;
                orderDetail.UnitPrice = item.UnitPrice;
                orderDetails.Add(orderDetail);
            }
            if (!existError)
            {
                order.OrderDetails = orderDetails;
                db.Orders.Add(order);
                db.SaveChanges();
                return(true);
            }
            return(false);
        }
示例#6
0
        public async Task <ActionResult> CreateOrder([Bind(Include = "ShipName,ShipPhone,ShipAddress,PaymentTypeId")] CartInformation cartInfo)
        {
            // load cart trong session.
            if (ModelState.IsValid)
            {
                var shoppingCart = LoadShoppingCart();
                if (shoppingCart.GetCartItems().Count <= 0)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Bad request"));
                }
                // chuyển thông tin shopping cart thành Order.
                var order = new Order
                {
                    TotalPrice = shoppingCart.GetTotalPrice(),
                    UserId     = userService.GetCurrentUserId(),
                    // can sua Language
                    //Language = cartInfo.Language,
                    PaymentTypeId = (PaymentType)Enum.Parse(typeof(PaymentType), cartInfo.PaymentTypeId),
                    ShipName      = cartInfo.ShipName,
                    ShipPhone     = cartInfo.ShipPhone,
                    ShipAddress   = cartInfo.ShipAddress,
                    OrderDetails  = new List <OrderDetail>(),
                    CreatedBy     = userService.GetCurrentUserName(),
                    UpdatedBy     = userService.GetCurrentUserName()
                };
                // Tạo order detail từ cart item.
                foreach (var cartItem in shoppingCart.GetCartItems())
                {
                    var orderDetail = new OrderDetail()
                    {
                        FlowerCode = cartItem.Value.FlowerCode,
                        OrderId    = order.Id,
                        Quantity   = cartItem.Value.Quantity,
                        UnitPrice  = cartItem.Value.Price,
                        Status     = OrderDetail.OrderDetailStatus.NotDeleted
                    };
                    order.OrderDetails.Add(orderDetail);
                }
                db.Orders.Add(order);
                db.SaveChanges();
                ClearCart();
                var strHomeUrl = Constant.WebURL + @"ShoppingCart/DisplayCartAfterCreateOrder?orderId=" + order.Id;
                await UserManager.SendEmailAsync(userService.GetCurrentUserId(),
                                                 "Congratulation: You have successfully created order!",
                                                 "Thank for choosing our flowers! Please click <a href=\"" + strHomeUrl + "\">here</a> to have a look at your cart!");

                //// lưu vào database.
                //var transaction = db.Database.BeginTransaction();
                //try
                //{

                //    transaction.Commit();
                //}
                //catch (Exception e)
                //{
                //    Console.WriteLine(e);
                //    transaction.Rollback();
                //}
                return(RedirectToAction("DisplayCartAfterCreateOrder", new { orderId = order.Id }));
            }

            return(View(cartInfo));
        }