コード例 #1
0
        public ActionResult Checkout(CheckoutViewModel checkoutViewModel)
        {
            UserHelper userHelper = new UserHelper();


            ShippingCharge       shippingCharge = db.ShippingCharges.FirstOrDefault();
            List <CartViewModel> cartItems      = GetCartItems();
            decimal totalBookWeight             = cartItems.Sum(k => (k.BookWeight * k.BookQuantity));
            decimal totalShippingCharges        = (totalBookWeight / shippingCharge.Weight) * shippingCharge.ShippingRate;



            Order order = new Order();

            order.UserId               = userHelper.GetLoginUserId();
            order.Status               = "processing";
            order.Createdate           = DateTime.Now;
            order.TotalWeight          = totalBookWeight;
            order.TotalShippingCharges = totalShippingCharges;
            db.Orders.Add(order);
            db.SaveChanges();



            foreach (var item in cartItems)
            {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.BookId     = item.BookId;
                orderDetail.Quantity   = item.BookQuantity;
                orderDetail.BookPrice  = item.BookPrice;
                orderDetail.BookWeight = item.BookWeight;
                orderDetail.OrderId    = order.OrderId;
                orderDetail.Createdate = DateTime.Now;
                db.OrderDetails.Add(orderDetail);
                db.SaveChanges();
            }

            CustomerDetail customerDetail = new CustomerDetail();

            customerDetail.Customername    = checkoutViewModel.CustomerName;
            customerDetail.ShippingAddress = checkoutViewModel.ShippingAddress;
            customerDetail.PhoneNo         = checkoutViewModel.PhoneNo;
            customerDetail.OrderId         = order.OrderId;
            customerDetail.Createdate      = DateTime.Now;
            db.CustomerDetails.Add(customerDetail);
            db.SaveChanges();

            EmptyCart();

            ThankyouViewModel thankyouViewModel = new ThankyouViewModel();

            thankyouViewModel.OrderId         = order.OrderId;
            thankyouViewModel.CustomerName    = customerDetail.Customername;
            thankyouViewModel.ShippingAddress = customerDetail.ShippingAddress;

            return(View("~/Views/Cart/Thankyou.cshtml", thankyouViewModel));
        }
コード例 #2
0
        public ActionResult thankyou(int id)
        {
            try
            {
                TblOrder order = orderService.GetByPrimaryKey(id);
                if (order == null)
                {
                    return(RedirectToAction("index", "cart"));
                }

                List <LineItem> lineItems = lineItemService.GetByOrderID(order.OrderID);
                foreach (var item in lineItems)
                {
                    if (item.VariantName == "Default Title")
                    {
                        var thumb = ImageService.GetPathImageFirstOfProduct(item.ProductID);
                        item.ImageUrl = thumb;
                    }
                    else
                    {
                        Variant variant = variantService.GetByPrimaryKey(item.VariantID);
                        if (variant != null)
                        {
                            TblImage image = imageService.GetByPrimaryKey(variant.ImageID);
                            if (image != null)
                            {
                                item.ImageUrl = image.ImageUrl;
                            }
                        }
                    }
                }

                ThankyouViewModel thankyouViewModel = new ThankyouViewModel();
                thankyouViewModel.CustomerEmail     = order.CustomerEmail;
                thankyouViewModel.LineItems         = lineItems;
                thankyouViewModel.ShippingAddressID = order.ShippingAddressID;
                thankyouViewModel.ShippingAddress   = shippingAddressService.GetByPrimaryKey(thankyouViewModel.ShippingAddressID == null ? 0 : thankyouViewModel.ShippingAddressID.Value);
                thankyouViewModel.BillingAddresID   = order.BillingAddressID;
                thankyouViewModel.BillingAddress    = billingAddressService.GetByPrimaryKey(thankyouViewModel.BillingAddresID == null ? 0 : thankyouViewModel.BillingAddresID.Value);
                thankyouViewModel.TotalSubPrice     = order.TotalCount;
                thankyouViewModel.TotalShipping     = order.TotalShipping;
                thankyouViewModel.TotalPrice        = order.TotalCount + order.TotalShipping;
                return(View(thankyouViewModel));
            }
            catch (Exception ex)
            {
                LogService.WriteException(ex);
                throw;
            }
        }