Charon Cart Version 2 Tutorial

This is a comprehensive tutorial outlining the steps neccessary to implement a shopping cart on a site using the excellent Charon Cart Extension. It is suitable for all but the very novice Ultradev user. The only requisite skills are an ability to create filtered recordsets and some familiarity with the Goto Link Page behaviour.

There are well illustrated steps to all the Charon Cart features, a short discussion on database design and a commented version of the server side code included with this extension.

This tutorial has been written with the kind permission of the extension author Jules Roberts. Corrections and suggestions are welcomed.

Inc_CharonCart.asp

This is a commented copy of the Inc_CharonCart.asp file that must be included in all pages that have any Cart functionality. The code is shown in blue and the character indicates a line break that must not appear in the code.


The first section sets up the array positions of the default cart columns.

CONST CC_ProductID = 0
CONST CC_Quantity = 1
CONST CC_Name = 2
CONST CC_Price = 3
CONST CC_UniqueKey = 4

A check is made to see if the CharonCart cookie exists.

if Request.Cookies("CharonCart") = "" then

If it does NOT exist yet an empty 5 by 50 array is created. The 5 columns refer to the 5 constants set above which correspond to ProductID, Quantity, Name, Price and UniqueID of every item in the cart.

dim CCcart(5,50)

If the cart DOES exist the function CookieToCart is called on the CharonCart cookie that moves the cookie into the cart array.

else
CCcart=CookieToCart("CharonCart")
end if

Resets those variables that are not stored in the cookie but in the CCcart array (Charon Cart psudorecordset).

CCcart_SubTotal=0
CCcart_numItems=0
CCcart_Shipping=0
CCcart_Discount=0
CCcart_SalesTax=0

Assumes that the cart is empty.

isFound=false

Creates a loop counter based on the number of items in the CCcart array.

for i=0 to ubound(CCcart,2)

Checks the contents of the ProductID at row number i in the array.

if CCcart(CC_ProductID,i) <> "" then
isFound=true

Adds the value of Item i to the current subtotal.

CCcart_SubTotal=CCcart_SubTotal+(CCcart(CC_Quantity,i)
* CCcart(CC_Price,i))

Adds one to the number of items in the cart.

CCcart_numItems=CCcart_numItems + 1
end if

Loops back to the FOR statement i times

next

Defines a function that calculates the CCcart_LineTotal when called

function CCcart_LineTotal

This function is called from a CartRepeat Region loop where i is defined similarly to above using a ubound command to count the number of items in the cart

CCcart_LineTotal=CCcart(CC_Quantity,i)*CCcart(CC_Price,i)
end function


Function that calculates the CCcart_GrandTotal when called.

function CCcart_GrandTotal
CCcart_GrandTotal=CCcart_SubTotal + CCcart_Shipping + CCcart_SalesTax - CCcart_Discount
end function


Important function that converts the CCcart array to a cookie the arrayname (CCcart) and the Cookiename are supplied when the function is called.

function CartToCookie(thearray,cookiename)
on error resume next
mystring=""

Sets the first loop for the number of items in the cart using Ubound.

for j=0 to ubound(CCcart,2)
if CCcart(CC_ProductID,j) <> "" then

If there is a product then add its 5 properties to the cookie. The hat symbol (^) is inserted to provide a character that breaks the string that is feed to the cookie (mystring) which will enable the code to identify rows and columns in the string.

for i=0 to 5
mystring=mystring & CCcart(i,j) & "^"
next

Once the 5 properties have been added there is a ^ left which has been added at the end of the last loop. This is deleted.

mystring=left(mystring,len(mystring)-1)

Adds a | sign to act as the end of row delimeter for later array conversion and loops round for the next item.

mystring=mystring & "|"
end if
next

The last | that has been added at the end of the loop of items is not required and is deleted

mystring=left(mystring,len(mystring)-1)

The string is saved as a cookie.

Response.Cookies(cookiename)=mystring
end function


Function that converts the cookie to the CCcart array.

function CookieToCart(cookiename)
dim myarray(5,50)
mystring=Request.Cookies(cookiename)

Splits the cookie into item rows using the | sign as a delimiter.

productarray=split(mystring,"|")

Sets item loop number using Ubound.

for j=0 to ubound(productarray)

Splits each row of item properties into columns using the ^ delimiter and each 5 item properties are added to the array

itemarray=split(productarray(j),"^")
for i=0 to 5
if itemarray(i) <> "" then
myarray(i,j)=itemarray(i)
else

Adds a null item if there is nothing in the properties.

myarray(i,j)=null
end if
next
next

Sets the CookieToCart value which is returned to myarray

CookieToCart=myarray
end function
%>

1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18

Comments

A VERY important tip for use with Dreamweaver MX

November 15, 2002 by Ivan Halen

I found that storing the Order and the Cart to a Database with Dreamweaver MX requires a slight modify to both the rsOrders and rsOrderDetails Recordsets (pages 13/14 of the tutorial).

In order to work properly, you have to change the LockType from Read Only (this is the default LockType in DreamweaverMX) to Optimistic. To do so, just select your Recordsets in the Server Behaviors Panel one at a time, then go to the Properties bar and change the Lock Type as I said above.

Maybe on Ultradev the default Lock Type is Optimistic (remember that's NOT so on Dreamweaver MX), so Rolf didn't mentioned in his tutorial. If you don't change the Lock Type, you'll get this error:

"ADODB.Recordset (0x800A0CB3)
Current Recordset does not support updating. This may be a limitation of
the provider, or of the selected locktype."

(as you see you find the word "locktype" in the error, and it's quite easy to understand: not so if you are using a non-english version of IIS or PWS, as me)

Oh, Rolf! Please change the Image029.gif on page 14: the selected Recordset is rsOrders and not rsOrderDetails, and that maybe confusing for the new user.

Happy Carting! :-)

About the Tutorial

November 17, 2002 by Kamran Aslam

I think the tutorial is tooo long. it will be better if it will be a little shorter or in other means " to the point" But anyways its good. but boaring too beacuse of too much text i guess.

Thanks anyways for the good things.

 

Pretty Good Until SSL

January 1, 2003 by James Threadgill

This was a pretty good tutorial until you left me hanging out to dry without a way to pass my cart over to my SSL connection. I found a form variable or a querystring works fine for this. In order to implement such a scheme one must create a modified version of the inc_CharonCart.asp, so that the function CookieToCart reads the values in from the form variable or querystring after one establishes the SSL connection. I modified it like so and saved it as ssl_inc_CharonCart.asp:

<%
CONST CC_ProductID = 0
CONST CC_Quantity = 1
CONST CC_Name = 2
CONST CC_Price = 3
CONST CC_UniqueKey = 4

CCcart=CookieToCart("SoftwareCart")
CCcart_SubTotal=0
CCcart_numItems=0
CCcart_Shipping=0
CCcart_Discount=0
CCcart_SalesTax=0
isFound=false

for i="0" to ubound(CCcart,2)
if CCcart(CC_ProductID,i) <> "" then
isFound=true
CCcart_SubTotal=CCcart_SubTotal + (CCcart(CC_Quantity,i)*CCcart(CC_Price,i))
CCcart_numItems=CCcart_numItems + 1
end if
next

function CCcart_LineTotal
CCcart_LineTotal=CCcart(CC_Quantity,i)*CCcart(CC_Price,i)
end function

function CCcart_GrandTotal
CCcart_GrandTotal=CCcart_SubTotal + CCcart_Shipping + CCcart_SalesTax - CCcart_Discount
end function

function CookieToCart(cookiename)
mystring=Request("Cart")
dim myarray(5,50)
productarray=split(mystring,"|")
for j="0" to ubound(productarray)
itemarray=split(productarray(j),"^")
for i="0" to 5
if itemarray(i) <> "" then
myarray(i,j)=itemarray(i)
else
myarray(i,j)=null
end if
next
next
CookieToCart=myarray
end function
%>

Notice the variable "mystring" in function CookieToCart now gets the cookie using a generic request.  The other difference is--unless you are allowing Cart Updates on your checkout page--is the the CartToCookie function and the code to initialize the cart array are no longer needed and can be removed for clarity as I have.  

RE: A VERY important tip for use with Dreamweaver MX

January 1, 2003 by James Threadgill
Thanks for this--saved me some head banging.
See all 20 Comments

You must me logged in to write a comment.