Link to home
Start Free TrialLog in
Avatar of globalwm
globalwm

asked on

Simple Intranet Shopping Cart - just ID, Description and Qty

I'm working on a simple Intranet Shopping Cart - just ID, Description and Qty. I want to add mutiples to a "cart" at once via form:

Part_Code     Part_Desc_1     Qty
-----------      --------------     ----
1                  item #1           [   ]
2                  item #2           [   ]
3                  item #3           [   ]

       [add to cart]


The Qty field is a text box where they choose how many (if any) they want.

Question #1: What's the best implementation for this?  
Question #2: How does the "add to cart" code look to accomplish Question #1?

Need also to be able to display the cart contents and modify (add/remove items) at any time...


ps. I have implemented Charon Cart v3 on another site. Is it easier to do something from scratch or modify Charon Cart code to work in this simple scenario?

Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

I would modify the Charon Cart.

FtB
Avatar of globalwm
globalwm

ASKER

HI (again) Fritz!  :)

Are you familiar with Charon Cart  code b/c I started doing this but have some questions.

(I can increase pts to continue?)
Basically, I want to do something like this:

http://www.charon.co.uk/charon_cart_v3/Results3.asp


It uses this asp to process:


<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="classes/Cart.asp" -->
<%
Set objCart=new Cart
%>

<!--#include file="Connections/CharonCart.asp" -->
<%
set Products = Server.CreateObject("ADODB.Recordset")
Products.ActiveConnection = MM_CharonCart_STRING
Products.Source = "SELECT *  FROM Products  "
Products.CursorType = 0
Products.CursorLocation = 2
Products.LockType = 3
Products.Open()
Products_numRows = 0
%>
<%
'This part has been handcoded
while not Products.eof
ProductID=Products("ProductID")
if Request.Form("Qty" & ProductID) <> "" then
objCart.AutoIncrement="true"
objCart.AddCartItem "ProductID",Products("ProductID")
objCart.AddCartItem "Quantity",Request.Form("Qty" & ProductID)
objCart.AddCartItem "Name",Products("Product")
objCart.AddCartItem "Price",Products("Price")
objCart.AddToCart
end if
Products.Movenext
wend
Response.Redirect "ShoppingCart.asp"
%>
<html>
<head>
<title>Add from Link</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
</body>
</html>
<%
Set objCart=Nothing
%>
<%
Products.Close()
%>



I haven't worked with this before, it is just that shopping cart code can be very tricky, and if you have worked with this product in the past, iit may cause you much less distress to work with that than to reinvent the wheel so to speak.

FtB
I see.

One more?  Can you explain to me what this code is doing in the above:

objCart.AddCartItem "ProductID",Products("ProductID")
objCart.AddCartItem "Quantity",Request.Form("Qty" & ProductID)
objCart.AddCartItem "Name",Products("Product")
objCart.AddCartItem "Price",Products("Price")

See, I don't have these fields on my present form.

What does Request.Form("Qty" & ProductID) accomplishing?


My "part_code" is a string btw. I thought I could use that as ProductID:

ProductID ->  part_code
Quantity -> Request.Form("Qty")  <-- same
Name -> part_desc_1
Price -> not used
I can't say for sure without seeing the code for the objCart object. What it appears to do is to accept two parameters:

1) the freindly name that will be displayed
2) the value for the related item.

FtB
If you look at the code that creates the objCart object, you should find the code for the .AddCartItem() method.

FtB
Cart.asp:

<%
class Cart
      public AutoIncrement
      public SubTotal,NumberItems,TotalNumberItems,Shipping,Discount,SalesTax,TotalWeight,RedirectUrl,isCartEmpty
      public OrderKey,OrderID,OrdersConnectionString,OrdersTable,OrderDetailsConnectionString,OrderDetailsTable,Bindingvalues,Bindingcolumns,OrderIdColumn
      private CCcart,CartColumns,objXML,objNodeXML,MaxCartItems,ItemDelimiter,ColumnDelimiter,CartName,NumberColumns,rs,LCID
      private i,j
      
      private Sub Class_Initialize

              Set objXML = Server.CreateObject("Microsoft.XMLDOM")
              objXML.validateOnParse=True
              objXML.load(Server.MapPath("xml/cart.xml"))
              set objNodeXML=objXML.documentElement
            set CartColumns=objNodeXML.selectSingleNode("CartColumns")
              NumberColumns=objNodeXML.selectSingleNode("CartColumns").childnodes.length
              MaxCartItems=objNodeXML.selectSingleNode("MaxCartItems").Text
              LCID=objNodeXML.selectSingleNode("LCID").Text
            Set objXML = Nothing

            Session.LCID=LCID
            ItemDelimiter="#item#"
            ColumnDelimiter="#col#"
            CartName="CharonCart"

            if Request.Cookies(CartName) = "" then
            redim CCcart(NumberColumns-1,MaxCartItems)
            else
            GetCartFromCookie
            end if

            SubTotal=0
            SalesTax=0
            Shipping=0
            Discount=0
            NumberItems=0
            TotalNumberItems=0
            TotalWeight=0
            isCartEmpty=true
            for i=0 to ubound(CCcart,2)
            if CCcart(GetColumnPosition("Key"),i) <> "" then
            SubTotal=SubTotal + (cdbl(CCcart(GetColumnPosition("Quantity"),i))*cdbl(CCcart(GetColumnPosition("Price"),i)))
            TotalWeight=TotalWeight + (cdbl(CCcart(GetColumnPosition("Quantity"),i))*cdbl(EvalWeight(CCcart(GetColumnPosition("Weight"),i))))
            NumberItems=NumberItems + 1
            TotalNumberItems=TotalNumberItems+cint(CCcart(GetColumnPosition("Quantity"),i))
            isCartEmpty=false
            end if
            next

      end sub

      public function AddCartItem(column,value)

            for i=0 to ubound(CCcart,2)
            if CCcart(GetColumnPosition("Key"),i) = "" then
            CCcart(GetColumnPosition(column),i)=value
            exit for
            end if
            next

      end function

      public function AddToCart

            if not DoIncrement then
            for i=0 to ubound(CCcart,2)
            if CCcart(GetColumnPosition("Key"),i) = "" then
            CCcart(GetColumnPosition("Key"),i)=GetKey
            exit for
            end if
            next
            end if
            PutCartToCookie

      end function

      private Key,Quantity,isDelete
      public function UpdateCart

            if Request("CC_UpdateCart") <> "" then
                  for i=0 to ubound(CCcart,2)
                  Key=GetCartValue("Key",i)
                  Quantity=trim(Request.Form("Qty" & Key))
                  isDelete=trim(Request.Form("Delete" & Key))
                  if Quantity = "" or Quantity="0" or isDelete <> "" then
                  CCcart(GetColumnPosition("Key"),i)=""
                  else
                  if IsNumeric(Quantity) then
                  CCcart(GetColumnPosition("Quantity"),i)=Quantity
                  end if
                  end if
                  next
                  PutCartToCookie
                  Response.Redirect Request.ServerVariables("SCRIPT_NAME") & "?UpdateNumber=" & GetKey
            end if

      end function

      public function InsertOrder
            set rs = Server.CreateObject("ADODB.Recordset")
            rs.ActiveConnection = OrdersConnectionString
            rs.Source = "SELECT *  FROM " & OrdersTable
            rs.CursorType = 3
            rs.CursorLocation = 3
            rs.LockType = 3
            rs.Open()
            rs.AddNew
            For Each fld in rs.Fields
            if Len(Request(fld.Name)) > 0 then
            fld.Value = Request(fld.Name)
            end if
            Next
            rs.Update
            rs.Requery
            rs.Sort=OrderKey & " desc "
            OrderID=rs(OrderKey)
      end function

      public function InsertOrderDetails
            set rs = Server.CreateObject("ADODB.Recordset")
            rs.ActiveConnection = OrderDetailsConnectionString
            rs.Source = "SELECT *  FROM " & OrderDetailsTable
            rs.CursorType = 3
            rs.CursorLocation = 3
            rs.LockType = 3
            rs.Open()
            BindingvaluesArray=split(Bindingvalues,",")
            BindingcolumnsArray=split(Bindingcolumns,",")
            for i=0 to ubound(CCcart,2)
            if CCcart(GetColumnPosition("Key"),i) <> "" then
            rs.AddNew
            rs(OrderIdColumn)=OrderID
            For j=0 to ubound(BindingvaluesArray)
            if BindingvaluesArray(j) <> "" then
            rs(BindingvaluesArray(j)) = CCcart(GetColumnPosition(BindingcolumnsArray(j)),i)
            end if
            Next
            rs.Update
            end if
            next
      end function

      public function LineTotal(row)
            LineTotal=cdbl(GetCartValue("Price",row)) * cdbl(GetCartValue("Quantity",row))
      end function

      public function GrandTotal
            GrandTotal=cdbl(SubTotal) + cdbl(Shipping) + cdbl(SalesTax) - cdbl(Discount)
      end function

      public function LineWeight(row)
            LineWeight=cdbl(EvalWeight(GetCartValue("Weight",row))) * cdbl(GetCartValue("Quantity",row))
      end function

      public function PutCartToCookie
            'on error resume next
            mystring=""
            for j=0 to ubound(CCcart,2)
            if CCcart(GetColumnPosition("Key"),j) <> "" then
            for i=0 to NumberColumns-1
            mystring=mystring & CCcart(i,j) & ItemDelimiter
            next
            mystring=left(mystring,len(mystring)-len(ItemDelimiter))
            mystring=mystring & ColumnDelimiter
            end if
            next
            if mystring <> "" then mystring=left(mystring,len(mystring)-len(ColumnDelimiter))
            Response.Cookies(CartName)=mystring
            Response.Cookies(CartName).Expires=date+7
      end function

      private function GetCartFromCookie
            redim CCcart(NumberColumns-1,MaxCartItems)
            mystring=Request.Cookies(CartName)
            productarray=split(mystring,ColumnDelimiter)
            for j=0 to ubound(productarray)
            itemarray=split(productarray(j),ItemDelimiter)
            for i=0 to NumberColumns-1
            if itemarray(i) <> "" then
            CCcart(i,j)=itemarray(i)
            else
            CCcart(i,j)=null
            end if
            next
            next
      end function

      public function GetColumnPosition(column)            
            on error resume next
            GetColumnPosition=cint(CartColumns.selectSingleNode("Column[@name='" & column & "']").getAttribute("position"))
            if err<>0 then
            EvalError "Error in function GetColumnPosition"
            end if
      end function

      public function GetCartValue(column,pos)            
            on error resume next
            GetCartValue=CCcart(GetColumnPosition(column),pos)
            if err<>0 then
            EvalError "Error in function GetCartValue"
            end if
      end function

      private function DoIncrement

            isExists=false
            CurrentRow=""
            if AutoIncrement <> "false" then
            for j=0 to ubound(CCcart,2)
            if CCcart(GetColumnPosition("Key"),j) = "" and CCcart(GetColumnPosition("ProductID"),j) <> "" then
            CurrentQuantity=CCcart(GetColumnPosition("Quantity"),j)
            for i=0 to NumberColumns-1
            if not (i=GetColumnPosition("Quantity") or i=GetColumnPosition("Key")) then
            CurrentRow=CurrentRow & CCcart(i,j) & ItemDelimiter
            end if
            next
            end if
            next
            for j=0 to ubound(CCcart,2)
            ItemRow=""
            if CCcart(GetColumnPosition("Key"),j) <> "" then
            for i=0 to NumberColumns-1
            if not (i=GetColumnPosition("Quantity") or i=GetColumnPosition("Key")) then
            ItemRow=ItemRow & CCcart(i,j) & ItemDelimiter
            end if
            next
            end if
            if ItemRow=CurrentRow then
            isExists=true
            CCcart(GetColumnPosition("Quantity"),j)=cint(CCcart(GetColumnPosition("Quantity"),j))+cint(CurrentQuantity)
            exit for
            end if
            next
            end if
            DoIncrement=isExists
            
      end function

      public function KillCart
            if Request("RemoveAll") <> "" then
                  Response.Cookies(CartName).Expires=Date-1
                  if RedirectUrl & "" <> "" then
                  Response.Redirect RedirectUrl
                  end if
            end if
      end function

      private function EvalError(message)
            Response.Write message
            Response.End
      end function

      private function EvalWeight(w)
            if w & "" <> "" then
            EvalWeight=w
            else
            EvalWeight=0
            end if
      end function

      private function GetKey
            Randomize Timer
            GetKey=int(rnd*9999999)+10000000
      end function

end class
%>
ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, scratch all this. I got the following working:

Item search
Add to Cart

Now I want help with my delete from cart script. The problem is that it's just deleting the ItemNumber (cartProductId) and not the whole entry:

<%

' REMOVE AN ITEM FROM THE CART
sAction = CStr(Request.QueryString("action"))
sVal = CStr(Request.QueryString("pq"))
IF sAction = "del" THEN
    for i = 0 to Ubound(CartArray,2)
      IF CartArray(cartProductId, i) = sVal THEN
         CartArray(cartProductId, i) = ""
         END IF
    NEXT
END IF

%>
This is the HTML for the delete button:

<a href="displaycart2.asp?pq=<%= CartArray(cartProductId, i )%>&action=del"><img border="0" src="images/delete.gif" width="51" height="17">
Wow! I hadn't realized that this question was still open! Were you just going to leave it here to languish?

FtB
Okay, I don't know what the array looks like, but your code only tells it to remove one element of the array rather than all of them:

  IF CartArray(cartProductId, i) = sVal THEN
        CartArray(cartProductId, i) = ""
        END IF

FtB
Yes, how do I tell it to remove the whole line item?

Array:

<%

CartArray = Session("Cart")

CART_COLUMS = 3
Const cartProductId = 0
Const cartDescription = 1
Const cartQty = 2

for i = 0 to Ubound(CartArray,2)
%>
Oh, should I start a new question?
This question is no longer valid. I'll ask for closure if no one objects.
You can close it yourself by choosing the comment(s) that helped you the most,

FtB