Il codice completo è:
codice:
<%
' Estrazione ID del prodotto
productID = TRIM( Request( "pid" ) )
' Aggiunta articoli nel carrello, con controllo quantità massima
IF productID <> "" THEN
sqlString = "SELECT * FROM carrello " &_
"WHERE carrello_utenteID=" & userID & " " &_
"AND carrello_prodottoID='" & productID & "'"
SET RS = Con.Execute( sqlString )
IF RS.EOF THEN
sqlString = "INSERT INTO carrello ( " &_
"carrello_utenteID, " &_
"carrello_prodottoID, " &_
"carrello_quantita " &_
") VALUES ( " &_
userID & ", '" &_
productID & "', 1 )"
ELSE
IF RS( "carrello_quantita" ) <= 5 THEN
sqlString = "UPDATE carrello SET " &_
"carrello_quantita=carrello_quantita+1 " &_
"WHERE carrello_id=" & RS( "carrello_id" )
ELSE
sqlString = "UPDATE carrello SET " &_
"carrello_quantita = 6 " &_
"WHERE carrello_id=" & RS( "carrello_id" )
END IF
END IF
RS.Close
SET RS = Nothing
Con.Execute sqlString
END IF
' estrazione della nazione utente
sqlString = "SELECT utenti_nazione FROM utenti " &_
"WHERE utenti_id=" & userID
SET RS = Con.Execute( sqlString )
nation = RS( "utenti_nazione" )
' determinazione spese di spedizione
sqlString = "SELECT spedizioni_prezzo FROM spedizioni " &_
"WHERE spedizioni_nazione='" & nation &"'"
SET RS = Con.Execute( sqlString )
spesespedizione = RS( "spedizioni_prezzo" )
' Aggiornamento quantità carrello della spesa
IF Request( "updateQ" ) THEN
SET RS = Server.CreateObject( "ADODB.Recordset" )
RS.ActiveConnection = Con
RS.CursorType = adOpenDynamic
RS.LockType = adLockOptimistic
sqlString = "SELECT * FROM carrello " &_
"WHERE carrello_utenteID=" & userID
RS.Open sqlString
WHILE NOT RS.EOF
newQ = TRIM( Request( "pq" & RS( "carrello_id" ) ) )
' controllo quantità valida, max 6 pezzi per codice
IF newQ = "" OR newQ = "0" THEN
RS.Delete
ELSE
IF isNumeric( newQ ) THEN
RS( "carrello_quantita" ) = newQ
ELSE
newQ = 1
END IF
IF newQ <= 5 THEN
RS( "carrello_quantita" ) = newQ
ELSE
RS( "carrello_quantita" ) = 6
END IF
END IF
RS.MoveNext
WEND
RS.Close
SET RS = Nothing
END IF
%>
Poi parte il codice html che è così strutturato:
codice:
<html>
<head><title>Carrello spesa</title></head>
<body bgcolor="white">
<center>
<font face="Arial" size=3 color="darkgreen">
Carrello della spesa di <u><%=Server.HTMLEncode(username)%></u>
</font>
<%
' Creazione carrello
sqlString = "SELECT carrello_id, prodotti_nomeITA, " &_
"prodotti_prezzo, carrello_quantita " &_
"FROM carrello, prodotti " &_
"WHERE carrello_utenteID=" & userID & " " &_
"AND carrello_prodottoID = prodotti_id " &_
"ORDER BY carrello_id DESC"
SET RS = Con.Execute( sqlString )
IF RS.EOF THEN
%>
Non hai acquistato niente pertanto il carrello è vuoto
<form action="default.asp">
<input type="submit" value="Torna ad acquistare">
</form>
<%
ELSE
orderTotal = 0
%>
<form method="post" action="cart.asp">
<input name="updateQ" type="hidden" value="1">
<input name="username" type="hidden" value="<%=username%>">
<input name="password" type="hidden" value="<%=password%>">
<table bgcolor="white" border="0" cellpadding="1" cellspacing="0" width="100%">
<tr bgcolor="lightgreen">
<th> </th>
<th align="left" width="50%">Descrizione prodotto</th>
<th>Prezzo unitario</th>
<th>Qtà (max 6 pz.)</th>
<th>Subtotale</th>
</tr>
<%
WHILE NOT RS.EOF
suborder = RS("prodotti_prezzo") * RS("carrello_quantita")
orderTotal = orderTotal + (RS("prodotti_prezzo") * RS("carrello_quantita"))
'formula per determinare sconto quantità
IF orderTotal => 120 THEN
sconto = 8
ELSE
sconto = 0
END IF
totgenerale = orderTotal + spesespedizione - sconto
%>
<tr>
<td>
<input type="button" value="X" onclick="this.form.pq<%=RS("carrello_id")%>.value=0; this.form.submit();">
</td>
<td>
<%=Server.HTMLEncode(RS("prodotti_nomeITA"))%>
</td>
<td align="center">
€ <%=formatNumber(RS("prodotti_prezzo"), 2)%>
</td>
<td align="center">
<input name="pq<%=RS("carrello_id")%>" type="text" size="2" value="<%=RS("carrello_quantita")%>">
</td>
<td align="center">
€ <%=formatNumber(suborder, 2)%>
</td>
</tr>
<%
RS.MoveNext
WEND
%>
<tr bgcolor="yellow">
<td colspan=4 align="right">
Totale merce :
</td>
<td align="center">
€ <%=formatNumber(orderTotal, 2)%>
</td>
</tr>
<tr bgcolor="yellow">
<td colspan=4 align="right">
Spese spedizione :
</td>
<td align="center">
€ <%=formatNumber(spesespedizione, 2)%>
</td>
</tr>
<tr bgcolor="yellow">
<td colspan=4 align="right">
Sconti :
</td>
<td align="center">
- € <%=formatNumber(sconto, 2)%>
</td>
</tr>
<tr bgcolor="azure">
<td colspan=4 align="right">
TOTALE GENERALE :
</td>
<td align="center">
€ <%=formatNumber(totgenerale, 2)%>
</td>
</tr>
<tr>
<td colspan="5" align="center">
<font color="#808080" face="verdana" size="2">Tutti i prezzi sono IVA inclusa</font>
</td>
</tr>
<tr>
<td colspan="5" align="center">
<table border="0" width="80%">
<tr>
<td align="center">
...................................................................................
Se cambi una quantità clicca su "Aggiorna carrello" per confermare la scelta
<input type="submit" value="Aggiorna carrello">
</td>
</tr>
</table>
</td>
</tr>
</form>