Ver Mensaje Individual
  #1 (permalink)  
Antiguo 30/01/2009, 11:57
haga41
 
Fecha de Ingreso: junio-2008
Mensajes: 145
Antigüedad: 16 años, 11 meses
Puntos: 0
Dudas sobre un carrito pasando datos a bases de datos al acabar la compra

Hola a todos,

Estoy intentando crear un carrito de la compra para una web bastante sencilla y de momento todo me iba bien, use el carrito de aspfacil.com y simplemente le añadí el hecho de que sólo se pueda usar el carrito si te has registrado antes.

El caso es que ahora lo que quiero es que cuando el usuario decida finalizar la compra todo lo que este en su carrito pase a una tabla de la base datos llamada pedidos y hay es donde tengo las dudas, he intentado pasar los datos de la session carrito a la base de datos pero no me aclaro y me da un fallo de se espera un objeto en la linea de "tabla3.open sql2,conexion,3,3".

Básicamente creo que lo que no entiendo es el funcionamiento del objeto carrito o la variable de sesión carrito y como puedo sacar los datos de ahí y meterlos en la tabla de pedidos, si puedieráis darme alguna pista de por donde empezar os lo agradecería mucho.

Tampoco tengo demasiado claro como hacer para que la tabla de Pedidos esté bien, es decir entiendo que debe tener:

- idpedido = autonúmerico
- idusuario = texto
- total = numérico (gasto total de ese pedido)
- fecha = obtenida con date() en ASP
- hora = obtenida con time() en ASP

Pero aparte de estos datos, ¿cómo hago para que aparezca el detalle de la compra desglosado? Es decir cuantas unidades y de que producto, podría crear 3 campos de cada producto: idproducto, precio y unidades pero supongo que no será la forma correcta de hacerlo.

Este es el código de la página de un producto:

Código asp:
Ver original
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Tienda</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  7. <link href="/estilos/estilos.css" rel="stylesheet" type="text/css" />
  8. </head>
  9.  
  10. <body>
  11. <%
  12. Sub MostrarCatalogo
  13. Set conexion = Server.CreateObject("ADODB.Connection")
  14. Set tabla = Server.CreateObject("ADODB.recordset")
  15.  
  16. conexion.open "PROVIDER=MICROSOFT.JET.OLEDB.4.0; Data Source="&server.MapPath("../db/Tienda.mdb")
  17.  
  18.  sql = "Select * from Productos where Idproducto =" & request("id")
  19.     tabla.open sql, conexion
  20.     Dim usuario
  21.     Dim nombre
  22.     usuario = session("usuario")
  23.     %>
  24.    
  25. <% if (usuario = "ok") then
  26.     nombre = session("user")%>
  27. <table width="960" border="0" align="center" cellpadding="0" cellspacing="0">
  28.   <tr>
  29.     <td height="31" align="right"><span style="color:#FFFFFF;font-weight:bold">Hola <%=nombre%></span><img src="http://www.forosdelweb.com/iconos/carrito.gif" alt="" width="16" height="13" hspace="5" border="0" align="absmiddle" /><a href="http://www.forosdelweb.com/tienda/index.htm" target="_blank" >Tienda</a> <a href="desconectar.asp?id=1&pag=prod1" >Cerrar sesión</a></td>
  30.   </tr>
  31. <% else %>
  32. <table width="960" border="0" align="center" cellpadding="0" cellspacing="0">
  33. <tr>
  34.     <td height="31" align="right"><img src="http://www.forosdelweb.com/iconos/carrito.gif" alt="" width="16" height="13" hspace="5" border="0" align="absmiddle" /><a href="http://www.forosdelweb.com/tienda/index.htm" target="_blank" >Tienda</a></td>
  35.   </tr>
  36. <% end if%>  
  37.   <tr>
  38.     <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
  39.       <tr>
  40.         <td align="right" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#77786A">
  41.           <tr>
  42.  
  43.             <td height="115" align="left" valign="bottom"><br />              
  44.               <table border="0" cellspacing="0" cellpadding="10">
  45.                 </table></td>
  46.           </tr>
  47.  
  48.         </table></td>
  49.       </tr>
  50.       <tr>
  51.         <td align="center" bgcolor="#757668"><table width="920" border="0" cellpadding="0" cellspacing="0" bgcolor="#000000">
  52.           <tr>
  53.             <td><table width="920" border="0" cellspacing="10" cellpadding="0">
  54.               <tr>
  55.                 <td align="left"><img src="<%=tabla.fields("foto")%>" />                  <table width="420" border="0" cellspacing="0" cellpadding="10">
  56.  
  57.                   <tr valign="top">
  58.                     <td><%=tabla.fields("nombre")%><br /></td>
  59.                     </tr>
  60.                  
  61.                 </table>                  
  62.                 <font color="#FFFFFF"><br />
  63.                   </font>
  64. <% if (usuario = "ok") then
  65.     nombre = session("user")%>
  66.                   <table border="0" cellpadding="10" cellspacing="0" bgcolor="#5C5C4C">
  67.                     <tr>
  68.                       <td><font color="#FFFFFF">Precio : <strong><%=tabla.Fields("precio")%></strong>&euro;</font></td>
  69.                       <td bgcolor="#999966" ><img src="http://www.forosdelweb.com/../imag/ico-carrito.gif" alt="" width="16" height="13" /> <a href="prod1.asp?ac=meter&prod=<%=tabla.Fields("Idproducto")%>&id=1" target="_blank">Comprar</a></td></tr>
  70. </table>      
  71. <% else %>
  72. <table border="0" cellpadding="10" cellspacing="0" bgcolor="#5C5C4C">
  73.                     <tr>
  74.                       <td><font color="#FFFFFF">Precio : <strong>70</strong>&euro;</font></td>
  75.                       <td bgcolor="#999966"><img src="http://www.forosdelweb.com/iconos/carrito.gif" alt="" width="16" height="13" /> <a href="http://www.forosdelweb.com/tienda/index.htm" target="_blank">Comprar</a></td>
  76.                     </tr>
  77.                   </table>
  78. <% end if%>                                        
  79.  <font color="#FFFFFF">&nbsp; </font></td>
  80.               </tr>
  81.             </table></td>
  82.           </tr>
  83.         </table></td>
  84.       </tr>
  85.       <tr>
  86.  
  87.         <td align="center" bgcolor="#757668">&nbsp;</td>
  88.       </tr>
  89.     </table></td>
  90.   </tr>
  91. </table>
  92. </body>
  93. </html>              
  94.                      
  95. <%  tabla.close
  96. End sub
  97.    
  98. ''''''''''''''''''''''''''''''''''''''''SHOP''''''''''''''''''''''''''''''''''''''''''''''
  99.  
  100. Sub MostrarCarrito'*********************************************************
  101.  
  102. Dim tabla, tabla2, SQL
  103. Dim clave
  104. Dim gastos  
  105. Dim total, subtotal
  106. 'gastos=2500 '
  107. gastos=0
  108. total=0
  109. subtotal=0
  110. Set tabla=Server.CreateObject("ADODB.Recordset")
  111. %>
  112.     <TABLE BORDER="1" CELLPADDING="1" CELLSPACING="1">
  113.     <TR>
  114.     <TD>Artículo</TD>
  115.     <TD>Cantidad</TD>
  116.     <TD>Precio/unidad</TD>
  117.     <TD>Quitar del carrito</TD>
  118.     <TD>Total</TD>  
  119.     </TR>
  120. <%
  121. For Each clave In carrito
  122. subtotal=0      
  123. SQL="SELECT * FROM Productos WHERE IdProducto="&clave&";"
  124. Set tabla=conexion.Execute(SQL)
  125. %>
  126.     <TR>
  127. <TD><%=tabla.Fields("Nombre")%></TD>
  128.     <TD><%=carrito(clave)%></TD>
  129.     <TD><%=tabla.Fields("Precio")%></TD>
  130.     <TD><A HREF="prod1.asp?ac=sacar&prod=<%=clave%>&cant=1&id=1">Quitar Uno</A>
  131.      <A HREF="prod1.asp?ac=sacar&prod=<%=clave%>&cant=<%=carrito(clave)%>&id=1">Quitar Todos</A>
  132.     </TD>
  133. <%
  134. subtotal=CSng(FormatNumber(carrito(clave))*tabla.Fields("Precio"))
  135. total=total+subtotal
  136. %>
  137.     <TD><%= subtotal %></TD>
  138.     </TR>
  139. <%
  140. Next
  141. If carrito.Count > 0 Then
  142. tabla.Close
  143. Set tabla=Nothing
  144. End If
  145. %>
  146.     <TR>
  147.     <TD COLSPAN=4>Gastos de envío</TD>
  148.     <TD>
  149.     <%
  150.     If total=0 Then
  151.         Response.Write("0")    
  152.     Else
  153.         Response.Write(gastos)
  154.     End If
  155.     %>
  156.     </TD>
  157.     <TR>
  158.     <TD COLSPAN=4><B>Total:</B></TD>
  159.     <TD>
  160.     <%if total=0 Then
  161.         Response.Write("0")
  162.     Else
  163.         Response.Write(total+gastos)
  164.     End If
  165.     %>  
  166.     </TD>
  167.     </TABLE>
  168. <%
  169. If accion<>"fin" Then
  170. %>
  171.     <center>
  172.     <A HREF=prod1.asp?id=1>Seguir mirando</A> / <A HREF=prod1.asp?ac=fin&id=1>Pasar por caja</A>
  173.     </center>
  174. <%
  175. End If
  176. End Sub'********************************************************************
  177.  
  178.  
  179. Sub MeterArticulo (id)'******************************************************
  180.  
  181. If carrito.Exists(id) Then
  182. carrito(id)=carrito(id)+1
  183. Else
  184. carrito.Add id, 1
  185. End If
  186. Response.Write("Se ha añadido el artículo al carrito<br>")
  187. End Sub'********************************************************************
  188.  
  189.  
  190. Sub SacarArticulo (id, cantidad)'*******************************************
  191.  
  192. If Not carrito.Exists(id) Then
  193. Response.Write("¡No tienes eso en tu carrito!")
  194. Else
  195. If cantidad>1 Then
  196. carrito.Remove(id)
  197. Else
  198. carrito(id)=carrito(id)-1
  199. If carrito(id)<1 Then
  200. carrito.Remove(id)
  201. End If
  202. End If
  203. End If
  204. End Sub'********************************************************************
  205.  
  206. Sub PasarPorCaja'***********************************************************
  207.  
  208. %>
  209.     <H2>¡Gracias por su compra!</H2>
  210. <%
  211. MostrarCarrito
  212. %>
  213. En breve te llegarán a tu casa los productos. Gracias por comprar . <br>
  214. (En un caso real aquí pediríamos los datos, etc.)
  215. <% 
  216.  
  217. Dim tabla3, sql2
  218.  
  219. sql2 = "Select * from Pedidos"
  220.         tabla3.open sql2,conexion,3,3      
  221.         tabla3.addnew
  222.         tabla3.fields("Idusuario") = session("idusuario")
  223.         tabla3.update
  224.         tabla3.Close
  225.         Set tabla3=Nothing
  226.  
  227.  
  228. Set Session("carrito")=Nothing
  229.  
  230.  
  231. End Sub'********************************************************************
  232.  
  233.  
  234. Dim carrito
  235. Dim accion
  236. Dim producto
  237. Dim cant
  238. Dim conexion
  239. %>
  240.  
  241. <%
  242. If IsObject(Session("carrito")) Then
  243.     Set carrito=Session("carrito")
  244. Else
  245.     Set carrito=Server.CreateObject("Scripting.Dictionary")
  246. End If
  247. accion=CStr(Request.QueryString("ac"))  
  248. producto=CInt(Request.QueryString("prod"))  
  249. cant=CInt(Request.QueryString("cant"))  
  250. Select Case accion
  251. Case "ver"
  252. MostrarCarrito
  253. Case "meter"
  254. MeterArticulo Producto
  255. MostrarCarrito
  256. Case "sacar"
  257. Call SacarArticulo (producto, cant)
  258. MostrarCarrito
  259. Case "fin"
  260. PasarPorCaja
  261. Case Else
  262. MostrarCatalogo
  263. End Select
  264. Set Session("carrito")=carrito
  265. conexion.Close
  266. Set conexion=Nothing
  267.  
  268.  
  269. ''''''''''''''''''''''''''''''''''''''''SHOP''''''''''''''''''''''''''''''''''''''''''''''    
  270. %>

Este es link a la página con el archivo original:
http://www.aspfacil.com/codigo/shop.asp?ac=ver