Exemple #1
0
        public int addVariantToCart(int variantID, int numberVariant)
        {
            try
            {
                string  cookieID = retrieveCookie();
                TblCart cart     = cartService.GetByCookieID(cookieID);
                if (cart == null)
                {
                    cart                 = new TblCart();
                    cart.CookieID        = cookieID;
                    cart.CreatedDateTime = SDateTime.GetYYYYMMddHmmSSNow();

                    cart.CartID = cartService.Insert(cart);
                }
                cart.ModifiedDateTime = SDateTime.GetYYYYMMddHmmSSNow();

                Variant variant = variantService.GetByPrimaryKey(variantID);
                if (variant != null)
                {
                    string where = string.Format("CartID={0} and VariantID={1}", cart.CartID, variantID);
                    List <TblCartItem> cartItems = cartItemService.GetByWhere(where);
                    if (cartItems == null || cartItems.Count == 0)
                    {
                        TblCartItem cartItem = new TblCartItem();
                        cartItem.CartID          = cart.CartID;
                        cartItem.VariantID       = variantID;
                        cartItem.NumberVariant   = numberVariant;
                        cartItem.CreatedDateTime = SDateTime.GetYYYYMMddHmmSSNow();
                        cartItemService.Insert(cartItem);
                    }
                    else
                    {
                        cartItems[0].NumberVariant++;
                        cartItemService.Update(cartItems[0]);
                    }

                    cartService.UpdateTotalPrice(cart.CartID);

                    cartItems = cartItemService.GetByCartID(cart.CartID);
                    if (cartItems != null)
                    {
                        return(cartItems.Count);
                    }
                }

                return(0);
            }
            catch (Exception ex)
            {
                LogService.WriteException(ex);
                throw;
            }
        }
Exemple #2
0
        public ActionResult SendOrder()
        {
            var cart     = ShoppingCart.Instance;
            var checkout = Request.Form["checkout"];

            string Address  = Request.Form["address"];
            string Email    = "";
            string Fullname = Request.Form["first_name"] + " " + Request.Form["last_name"];

            int    OrderID;
            string OrderID_Insert = CartService.Insert(Address, Email, Fullname, cart.Items.Count(), cart.SubTotal);
            bool   isNumerical    = int.TryParse(OrderID_Insert, out OrderID);

            if (isNumerical)
            {
                List <ProductModels> ProductsAdded = new List <ProductModels>();
                foreach (CartItem item in cart.Items)
                {
                    CartItemService.Insert(OrderID, item.ProductItem.Id, item.ProductItem.Name, item.TotalPrice, item.Quantity);
                    ProductModels product = new ProductModels();
                    product.Id      = item.ProductItem.Id;
                    product.Name    = item.ProductItem.Name;
                    product.Price   = item.ProductItem.Price;
                    product.SaleOff = item.ProductItem.SaleOff;
                    ProductsAdded.Add(product);
                }
                if (ProductsAdded.Count() > 0)
                {
                    foreach (var product in ProductsAdded)
                    {
                        cart.RemoveItem(product);
                    }
                }
            }

            return(RedirectToAction("Index", "Home"));
        }