create a shopping cart with three variables...

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

Sort Posts:

  • create a shopping cart with three variables...

    04-05-2008, 8:41 AM
    • Member
      8 point Member
    • 333bob
    • Member since 04-05-2008, 12:33 PM
    • Posts 21

    With VB, trying to create a shopping cart; two ways I think:

    adding the items in a database....Not trying this one.

     Adding the articles in session variables, like:

    session.add("product_id", price).

    then retrieve the data from the database where product_id=product_id..... Ok I think it will work....

    the thing is, by the sessoin I can only add two variables (product_id and price ) for each article....

    And what if there was another variable, for example size or color.

    Can I store 3 variables at a time (product_id, price and color) in a session variable or in something else each time the user adds an item to the cart, and how to?????

  • Re: create a shopping cart with three variables...

    04-05-2008, 4:25 PM
    Answer
    • All-Star
      45,005 point All-Star
    • jimmy q
    • Member since 11-02-2006, 9:01 AM
    • Australia
    • Posts 3,117
    • Moderator
      TrustedFriends-MVPs

    In my opinion best way to do this is to create a class that represents a shopping cart item.

    So a class that has like a product id, quantity, and other attributes.

    So each time a user wants to add an item you would create an instance of this class and add it to session, whether it be directly to session or as a collection of items.

    That way you can easily manage the cart items like change its quantity, remove etc 

  • Re: create a shopping cart with three variables...

    04-06-2008, 12:25 AM
    Answer
    • Star
      8,695 point Star
    • lspence
    • Member since 11-01-2006, 2:12 PM
    • United States
    • Posts 1,342

     I completely agree with Jimmy, it will make maintenance very easy as well as promote a good OO design methodology. I put together an extremely simple example in C#. For the price I only set 2 values and again this is a very very simple example.

     Default.aspx

    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="My Product Page"></asp:Label>
            <br /><br />
            <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True">
                <asp:ListItem>Shirts</asp:ListItem>
                <asp:ListItem>Sweaters</asp:ListItem>
                <asp:ListItem>Coats</asp:ListItem>
                <asp:ListItem>Pants</asp:ListItem>
            </asp:ListBox>
            <br />
            <br />
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                <asp:ListItem>Small</asp:ListItem>
                <asp:ListItem>Medium</asp:ListItem>
                <asp:ListItem>Large</asp:ListItem>
                <asp:ListItem>X-Large</asp:ListItem>
                <asp:ListItem>XX-Large</asp:ListItem>
            </asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
                <asp:ListItem>Black</asp:ListItem>
                <asp:ListItem>White</asp:ListItem>
                <asp:ListItem>Green</asp:ListItem>
                <asp:ListItem>Blue</asp:ListItem>
                <asp:ListItem>Yellow</asp:ListItem>
                <asp:ListItem>Red</asp:ListItem>
                <asp:ListItem>Orange</asp:ListItem>
                <asp:ListItem>Brown</asp:ListItem>
            </asp:DropDownList>
            <asp:TextBox ID="TextBox1" runat="server" Width="46px"></asp:TextBox><br />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Order" /><br />
            <br />
            <br />
        </div>
        </form>

    Default.aspx.cs

    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");
    }
    ShoppingCart.cs 
    public class ShoppingCart
    {
       private string size;
       public string Size
       {
          get { return size; }
          set { size = value; }
       }
        
       private string color;
       public string Color
       {
          get { return color; }
          set { color = value; }
       }
    
       private int productID;
       public int ProductID
       {
          get { return productID; }
          set { productID = value; }
       }
    
       private string productName;
       public string ProductName
       {
          get { return productName; }
          set { productName = value; }
       }
    
       private string productDescription;
       public string ProductDescription
       {
          get { return productDescription; }
          set { productDescription = value; }
       }
    
       private int quantity;
       public int Quantity
       {
          get { return quantity; }
          set { quantity = value; }
       }
    
       private decimal price;
       public decimal Price
       {
          get { return price; }
          set { price = value; }
       }
    }
     
    OrderDetails.aspx
     
    form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Your Order"></asp:Label>
            <br />
            <br />
            <asp:Label ID="Label" runat="server" Text="ProductID:"></asp:Label>
            <asp:Label ID="ProductIDLabel" runat="server" Text="Label"></asp:Label>
            <br />
            <asp:Label ID="Label2" runat="server" Text="Product Name:"></asp:Label>
            <asp:Label ID="ProductNameLabel" runat="server" Text="Label"></asp:Label>
            <br />
            <asp:Label ID="Label3" runat="server" Text="Description:"></asp:Label>
            <asp:Label ID="ProductDescriptionLabel" runat="server" Text="Label"></asp:Label><br />
            <asp:Label ID="Label4" runat="server" Text="Quantity:"></asp:Label>
            <asp:Label ID="ProductQtyLabel" runat="server" Text="Label"></asp:Label><br />
            <asp:Label ID="Label5" runat="server" Text="Size:"></asp:Label>
            <asp:Label ID="ProductSizeLabel" runat="server" Text="Label"></asp:Label><br />
            <asp:Label ID="Label6" runat="server" Text="Color:"></asp:Label>
            <asp:Label ID="ProductColorLabel" runat="server" Text="Label"></asp:Label><br />
            <asp:Label ID="Label7" runat="server" Text="Price:"></asp:Label>
            <asp:Label ID="ProductPriceLabel" runat="server" Text="Label"></asp:Label></div>
        </form>
     
    OrderDetails.aspx.cs 
     
    protected void Page_Load(object sender, EventArgs e)
    {
        ShoppingCart order = new ShoppingCart();
        order = Session["Order"] as ShoppingCart;
    
        ProductIDLabel.Text = order.ProductID.ToString();
        ProductNameLabel.Text = order.ProductName;
        ProductDescriptionLabel.Text = order.ProductDescription;
        ProductQtyLabel.Text = order.Quantity.ToString();
        ProductSizeLabel.Text = order.Size;
        ProductColorLabel.Text = order.Color;
        ProductPriceLabel.Text = order.Price.ToString();
    }
     
     
     
    My Blog
    http://Lspence.blogspot.com

    (Please MARK this post as ANSWERED, if you find it helpful)
  • Re: create a shopping cart with three variables...

    04-06-2008, 7:36 PM
    • Member
      8 point Member
    • 333bob
    • Member since 04-05-2008, 12:33 PM
    • Posts 21
    thanks, exactly that !!! Except i'm on Vb and i didn't find a way yet to translate: "order = Session["Order"] as ShoppingCart;" into VB, the "as shoppingcart" isn't what he expected, but great for the rest, thanks again.
  • Re: create a shopping cart with three variables...

    04-06-2008, 8:37 PM
    • Star
      8,695 point Star
    • lspence
    • Member since 11-01-2006, 2:12 PM
    • United States
    • Posts 1,342

    What you need to do is cast the Session object to a Shopping Cart object. I could have also written that line as such:

    order = (ShoppingCart)Session["Order"]; 

     

    My Blog
    http://Lspence.blogspot.com

    (Please MARK this post as ANSWERED, if you find it helpful)
  • Re: create a shopping cart with three variables...

    04-06-2008, 9:10 PM
    Answer
    • All-Star
      45,005 point All-Star
    • jimmy q
    • Member since 11-02-2006, 9:01 AM
    • Australia
    • Posts 3,117
    • Moderator
      TrustedFriends-MVPs

    333bob:
    order = Session["Order"] as ShoppingCart;
     

    The VB equivalent w ould be

    order = CType(Session("Order"), ShoppingCart) 

  • Re: create a shopping cart with three variables...

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

    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)