Develop shopping cart using class

Last post 10-26-2008 2:19 AM by nopsolutions. 6 replies.

Sort Posts:

  • Develop shopping cart using class

    08-07-2008, 5:57 AM
    • Member
      23 point Member
    • wkm1925
    • Member since 05-30-2004, 1:26 AM
    • Posts 32

    Hi,

    From http://forums.asp.net/t/1243553.aspx article, im using ShoppingCart.cs to implement ShoppingCart class.

    Im also trying below code

    ----------------------------

    protected void Button1_Click(object sender, EventArgs e)
    {
        ShoppingCart cart = new ShoppingCart();
        cart.ProductID = 1;
        cart.ProductName = ListBox1.SelectedItem.Value;
        cart.ProductDescription = "Some Description";
        cart.Quantity = Convert.ToInt32(TextBox1.Text);
        cart.Size = DropDownList1.SelectedItem.Value;
        cart.Color = DropDownList2.SelectedItem.Value;
    
        if (cart.ProductName == "Sweaters" && cart.Size == "XX-Large")
            cart.Price = 29.95M;
        else
            cart.Price = 19.95M;
    
        Session["Order"] = cart;
    
        Response.Redirect("~/OrderDetails.aspx");
    }

    ----------------------------

    From my understanding, Session["Order"]  holds 1 object (1 product) at that moment. Still thinking how this class can holds more than 1 product?

    Im really stuck, how to using same class to assign another value but the im still can call the previous value that i've assigned. So, i can have View Cart in my application with more than 1 product.

    Really need techinical explanation.

  • Re: Develop shopping cart using class

    08-07-2008, 8:02 AM

     Hi

    This will help u lot if u have query plz let me know Dont forget that u must use genrices list for product so u can have n number of product in a single session

     

    Cart should be Structured like as given below

    CART 

    public class Cart
    {

            public List<Products> Products = new List<Products>();
            public float TotalAmt;
            public float TotalWeight;
            public float ShippingCharge;
            public float Tax;
            public float TotalSum;
            public int ShippingAddress;
            public float SubTotal;
            public float PaymentAmount;
    }

    PRODUCT

    public class Products
        {
            private int _ProductId;
            private string _ProductName;
            private string _ShortDescription;
            private string _LongDescription;
           // List of Property
            #region "Price attached for display in ProductLarge Page"
               
            private float _Price;
            private int _Quantity;

            public int Quantity
            {
                get { return _Quantity; }
                set { _Quantity = value; }
            }
               

            public float Price
            {
                get { return _Price; }
                set { _Price = value; }
            }
            #endregion

            public int ProductId
            {
                get { return _ProductId; }
                set { _ProductId = value; }
            }

            public string ProductName
            {
                get { return _ProductName; }
                set { _ProductName = value; }
            }
           
            public string ShortDescription
            {
                get { return _ShortDescription; }
                set { _ShortDescription = value; }
            }

            public string LongDescription
            {
                get { return _LongDescription; }
                set { _LongDescription = value; }
            }

         }

    POPULATING

    Session["Carts"] =(Cart)Session["Cart"];

      if (Session["Carts"] != null)
                            {
                               
                  
                                objCart.TotalAmt = 0;
                                foreach (Products objProducts in objCart.Products)
                                {
                                    objCart.TotalAmt += objProducts.Price * objProducts.Quantity;
                                }
                                string l_subtotal = string.Format("{0:f}", objCart.TotalAmt);
                                ltrSubTotal.Text = l_subtotal;
                                if (objCart.Products.Count != 0)
                                {
                                    gvShoppingCart.DataSource = objCart.Products;
                                    gvShoppingCart.DataBind();
                                  
                                }
                              
                            }

    Thanks & Regards
    Ramkumar.V
    www.tnbytes.com
    info@tnbytes.com

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Develop shopping cart using class

    08-07-2008, 11:25 AM
    • Member
      23 point Member
    • wkm1925
    • Member since 05-30-2004, 1:26 AM
    • Posts 32

    objCart.TotalAmt = 0;

    I dont understand this part. where objCart.TotalAmt = 0; come from? No need to declare?

  • Re: Develop shopping cart using class

    08-08-2008, 5:10 AM

    it s declared in cart class and i had declared since at the beginning it should be 0 so that when i itreate the products i can get total amount based on the products price

    check that objcart.totalamt+= where it will be incremented so i used this steps

    Thanks & Regards
    Ramkumar.V
    www.tnbytes.com
    info@tnbytes.com

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Develop shopping cart using class

    08-08-2008, 6:52 AM
    • Member
      23 point Member
    • wkm1925
    • Member since 05-30-2004, 1:26 AM
    • Posts 32

    tq for the explaination.

    im interested when you said, Dont forget that u must use genrices list for product so u can have n number of product in a single session

    Do u have any sample, how to play around with this generic list. The most important thing, in terms of how to add,remove and update this generic list.

    Need your advice.

  • Re: Develop shopping cart using class

    08-08-2008, 7:27 AM
    Answer

    Hi Dude,

    Genrics LIST of product is a class as i given before itself  public List<Products> Products = new List<Products>();DELETE            Cart objCart = (Cart)Session["Carts"];
    ImageButton objImageButton = (ImageButton)sender;
                int ProductId = Convert.ToInt32(objImageButton.CommandArgument);
               
                //string ProductId = gvShoppingCart.DataKeys[e.RowIndex].Value.ToString();
                int x=0;
                foreach (Products objProduct in objCart.Products)
                {
                    if (objProduct.ProductId == ProductId)
                    {
                        objCart.Products.RemoveAt(x);

                        Session["Carts"] = objCart;
                        if (objCart.Products.Count != 0)
                        {
                            gvShoppingCart.DataSource = objCart.Products;
                            gvShoppingCart.DataBind();
                            return;
                        }
                        else
                        {
                            gvShoppingCart.Visible = false;
                            Session["Cart"] = null;
                            hide.Visible = false;
                            ibtnCheckOut.Visible = false;
                            noproduct.Visible = true;
                            return;
                        }
                 

       }
                    x++;
                }

    ADD  if (Session["Cart"] == null)
                    {
                        Cart objCart = new Cart();
                        objProducts.Quantity = 1;
                        objCart.Products.Add(objProducts);
                        Session["Cart"] = objCart;
                    }
                    else
                    {
                        Cart objCart = (Cart)Session["Cart"];
                        bool ProductAlreadyExist = false;
                        foreach (Products CartProduct in objCart.Products)
                        {
                            if (CartProduct.ProductId == objProducts.ProductId)
                            {
                                CartProduct.Quantity = CartProduct.Quantity + 1;
                                ProductAlreadyExist = true;

                            }
                            else
                            {
                                objProducts.Quantity = 1;
                            }

                        }
                        if (!ProductAlreadyExist)
                        {
                            objProducts.Quantity = 1;
                            objCart.Products.Add(objProducts);
                        }
                    }
                    if (Session["User"] != null)
                    {
                        Response.Redirect("shoppingcart.aspx");
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }

    Encouragement REALLY I DID NOT WORKED WITH UPDATE THE LIST.....

    DONT FORGET TO MARK AS ANSWER IF THE POST HELPED U WHICH WILL BE ENCOURAGEMENT OF THE ANSWERS

     
    Thanks & Regards
    Ramkumar.V
    www.tnbytes.com
    info@tnbytes.com

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Develop shopping cart using class

    10-26-2008, 2:19 AM
    • Member
      30 point Member
    • nopsolutions
    • Member since 09-18-2008, 2:29 AM
    • Posts 12

     
    Hi

    We've developed new open-source shopping cart nopCommerce. If you interested you can visit http://www.nopCommerce.com


    nopCommerce is the open-source eCommerce solution. nopCommerce is available for free.
    http://www.nopCommerce.com
Page 1 of 1 (7 items)