Exemple #1
0
        public ActionResult AddToCart(int productId)
        {
            var product = _productService.GetProductById(productId);
            var productViewModel = AutoMapper.Mapper.Map<ProductViewModel>(product);
            var result = false;
            var currentCount = 0;
            if (product.ProductId > 0)
            {
                var obj = Session[Common.UserCartKey];

                if (obj != null)
                {
                    var cart = obj as CartViewModel;
                    if (cart != null)
                    {
                        var orderDetail = cart.OrderDetails.FirstOrDefault(x => x.Product.ProductId == productViewModel.ProductId);
                        if (orderDetail != null)
                        {
                            orderDetail.Quantity += 1;

                        }
                        else
                        {
                            orderDetail = new OrderDetailViewModel();
                            orderDetail.Product = productViewModel;
                            orderDetail.Quantity = 1;
                            cart.OrderDetails.Add(orderDetail);
                        }
                        currentCount = cart.OrderDetails.Select(x => x.Quantity).Sum();
                        Session[Common.UserCartKey] = cart;
                    }
                }
                else
                {
                    var cart = new CartViewModel();
                    cart.OrderDetails = new List<OrderDetailViewModel>();
                    var orderDetail = new OrderDetailViewModel();
                    orderDetail.Quantity = 1;
                    orderDetail.Product = productViewModel;
                    cart.OrderDetails.Add(orderDetail);
                    currentCount = cart.OrderDetails.Select(x => x.Quantity).Sum();
                    Session[Common.UserCartKey] = cart;
                }
                result = true;
            }
            return Json(new { result, currentCount });
        }
Exemple #2
0
 public bool CreateOrder(int? userId,CartViewModel cart)
 {
     var order = new Order();
        order.UserId = userId;
        order.ShippingAddress = cart.Street;
        order.ShippingTelephone = cart.Telephone;
        order.OrderDetails=new List<OrderDetail>();
        foreach (var oDetail in cart.OrderDetails)
        {
        var orderDetail = new OrderDetail();
        orderDetail.ProductId = oDetail.Product.ProductId;
        orderDetail.Price = oDetail.Product.Price??0;
        orderDetail.Quantity = oDetail.Quantity;
        order.OrderDetails.Add(orderDetail);
        }
        order.LocationCityId = cart.CityId;
        order.LocationDistrictId = cart.DistrictId;
        order.OrderStatus = Common.OrderStatusNew;
        order.CreatedDate = DateTime.Now;
        order.UpdatedDate = DateTime.Now;
        Context.Orders.Add(order);
        Context.SaveChanges();
        return order.OrderId > 0;
 }
Exemple #3
0
 private void SaveCart(CartViewModel cart)
 {
     Session[Common.UserCartKey] = cart;
 }
Exemple #4
0
 private CartViewModel GetCart()
 {
     var cartModel = new CartViewModel();
     cartModel.OrderDetails = new List<OrderDetailViewModel>();
     var objCart = Session[Common.UserCartKey];
     if (objCart != null)
     {
         var cart = objCart as CartViewModel;
         if (cart != null)
         {
             cartModel = cart;
         }
     }
     return cartModel;
 }