Friday, 27 March 2009

Making a Shopping Cart page

Firstly, I have typed pieces of code between"<% " at the top and " %> " in a page called "shopCart.asp" before the start of -HTML- tags.

Define Constants

CONST CARTPID = 0
CONST CARTPNAME = 1
CONST CARTPPRICE = 2
CONST CARTPQUANTITY = 3


GET THE SHOPPING CART AND OR SET UP SESSION VARIABLE

IF NOT isArray( Session( "cart" ) ) THEN
DIM localCart( 4, 20 )
ELSE
localCart = Session( "cart" )
END IF


GET THE PRODUCT INFORMATION

productID = TRIM( Request( "pid" ) )
productName = TRIM( Request( "productName" ) )
productPrice = TRIM( Request( "productPrice" ) )

ADD ITEM TO SHOPPING CART

IF productID <> "" THEN
foundIT = FALSE
FOR i = 0 TO UBOUND( localCart )
IF localCart( CARTPID, i ) = productID THEN
localCart( CARTPQUANTITY, i ) = localCart( CARTPQUANTITY, i ) + 1
foundIT = TRUE
EXIT FOR
END IF
NEXT
IF NOT foundIT THEN
FOR i = 0 TO UBOUND( localCart, 2 )
IF localCart( CARTPID, i ) = "" THEN
localCart( CARTPID, i ) = productID
localCart( CARTPNAME, i ) = productName
localCart( CARTPPRICE, i ) = productPrice
localCart( CARTPQUANTITY, i ) = 1
EXIT FOR
END IF
NEXT
END IF
END IF


UPDATE THE QUANTITIES IN THE SHOPPING CART

IF Request( "updateQ" ) <> "" THEN
FOR i = 0 TO UBOUND( localCart, 2 )
newQ = TRIM( Request( "pq" & localCart( CARTPID, i ) ) )
IF newQ = "" or newQ = "0" THEN
localCart( CARTPID, i ) = ""
ELSE
IF isNumeric( newQ ) THEN
localCart( CARTPQUANTITY, i ) = newQ
END IF
END IF
NEXT
END IF

UPDATE SESSION VARIABLE WITH ARRAY

Session( "cart" ) = localCart

Secondly, I make a form that contains a simple table

Place the "Back to Shop" button in the left cell (image/backtoshop.gif) and link it to "product.asp".

Place the "Checkout" button in the centre cell (image/checkout.gif) and link it to "shopCart.asp".

Place a standard form "Submit" button in the right cell and label it "Update Cart"

Click on the "Update Cart" button to highlight it and then click on the "Quick Tag Editor" . Change <-input-> tag to read " <-input- type-=-image -src-="buttons/update.giff">


I click inside the form, just before the "Shopping Cart" text and insert a "Hidden Field".

Name the "Hidden Field" as "updateQ" and give it a value of "1".


Afterward, I click inside the right hand cell situated below the word "Quantity" and insert a "Text Field".


In the "Name" field (left on properties window beneath TextField), I type

pq<%=localCart(CARTPID, i )%>

In the "Char Width" field enter "4"

In the "Init Val" field enter

<%=localCart(CARTPQUANTITY,i )%>

Next step is to setup the form Action to shopCart.asp and the "Method" to "POST".

Now, I type some piece of code and put them in somewhere of the form.

I type

<%orderTotal = 0%>
in just after the opening tag.

I type the code below between the end "" of the first row and the beginning "" of the second row.

<% FOR i = 0 TO UBOUND( localCart, 2 ) IF localCart( CARTPID, i ) <> "" THEN
orderTotal = orderTotal + ( localCart( CARTPPRICE, i ) * localCart( CARTPQUANTITY, i ) )
%>

Next to get cart name for product name, I put

<%=Server.HTMLEncode( localCart( CARTPNAME, i ) )%>

into the tables second row , first cell.

In Design View, I can see the code that I just put in is under "Product Name"


Again, I put

<%=formatCurrency( localCart( CARTPPRICE, i ) )%>

into the tables second row , second cell.


So there should a ASP green sign uder "Price"


I put an end for a loop by

<% END IF NEXT %>

It was put between the end "" of the second row and the beginning "" of the third row.

a given code of Order Total


<%=formatCurrency( orderTotal )%>

was put into the third cell of the third row


so in the Design View of Dreamweaver, I see a ASP green sign on the left of "Order Total"


next step is to preview and test a function of shopping cart.




In "Quantity" text field, I replace number 3 instead of 1, then click on "Update To Cart"



However, There is error when I update a number of product quantity



So I post my question on ecommerce forum



Then get a solution



From a given hint from the forum, I fix my code in shopCart.asp



Then testing a fixed code o shopping cart function.

After selecting my favorite product, I come to the shopping cart with number 1 in quantity box. In the field of Unit Total Price and Total Order Price, I can see a price of 54.5 and 57.225 respectively.



After a click on button " Update Cart" , both fields of price are changed as well as a number of product quantity are updated.


No comments:

Post a Comment