lil_carrito.php
Código PHP:
<?
class carrito {
//atributos de la clase
var $num_productos;
var $array_id_prod;
var $array_nombre_prod;
var $array_talle_prod;
var $array_color_prod;
var $array_precio_prod;
//constructor. Realiza las tareas de inicializar los objetos cuando se instancian
//inicializa el numero de productos a 0
function carrito () {
$this->num_productos=0;
}
//Introduce un producto en el carrito. Recibe los datos del producto
//Se encarga de introducir los datos en los arrays del objeto carrito
//luego aumenta en 1 el numero de productos
function introduce_producto($id_prod,$nombre_prod,$talle_prod,$color_prod,$precio_prod){
$this->array_id_prod[$this->num_productos]=$id_prod;
$this->array_nombre_prod[$this->num_productos]=$nombre_prod;
$this->array_talle_prod[$this->num_productos]=$talle_prod;
$this->array_color_prod[$this->num_productos]=$color_prod;
$this->array_precio_prod[$this->num_productos]=$precio_prod;
$this->num_productos++;
}
//Muestra el contenido del carrito de la compra
//ademas pone los enlaces para eliminar un producto del carrito
function imprime_carrito(){
$suma = 0;
echo '<table border=1 cellpadding="3">
<tr>
<td><b>Nombre producto</b></td>
<td><b>Talle</b></td>
<td><b>Color</b></td>
<td><b>Precio</b></td>
<td> </td>
</tr>';
for ($i=0;$i<$this->num_productos;$i++){
if($this->array_id_prod[$i]!=0){
echo '<tr>';
echo "<td>" . $this->array_nombre_prod[$i] . "</td>";
echo "<td>" . $this->array_talle_prod[$i] . "</td>";
echo "<td>" . $this->array_color_prod[$i] . "</td>";
echo "<td>" . $this->array_precio_prod[$i] . "</td>";
echo "<td><a href='eliminar_producto.php?linea=$i'>Eliminar producto</td>";
echo '</tr>';
$suma += $this->array_precio_prod[$i];
}
}
//muestro el total
echo "<tr> <td><b>TOTAL:</b></td> <td> </td> <td> </td> <td><b>$suma</b></td> <td> </td> </tr>";
//total más IVA
echo "<tr> <td><b>IVA (16%):</b></td> <td> </td> <td> </td> <td><b>" . $suma * 1.16 . "</b></td> <td> </td> </tr>";
echo "</table>";
}
// gemera el carrito para mandar por mail
function _genera_carrito(){
$suma = 0;
$carrito = '<table border=1 cellpadding="3">
<tr>
<td><b>Nombre producto</b></td>
<td><b>Precio</b></td>
</tr>';
for ($i=0;$i<$this->num_productos;$i++){
if($this->array_id_prod[$i]!=0){
$carrito .= '<tr>';
$carrito .= "<td>" . $this->array_nombre_prod[$i] . "</td>";
$carrito .= "<td>" . $this->array_talle_prod[$i] . "</td>";
$carrito .= "<td>" . $this->array_color_prod[$i] . "</td>";
$carrito .= "<td>" . $this->array_precio_prod[$i] . "</td>";
$carrito .= '</tr>';
$suma += $this->array_precio_prod[$i];
}
}
//muestro el total
$carrito .= "<tr> <td><b>TOTAL:</b></td> <td> </td> <td> </td> <td><b>$suma</b></td> </tr>";
//total más IVA
$carrito .= "<tr> <td><b>IVA (21%):</b></td> <td> </td> <td> </td> <td><b>" . $suma * 1.21 . "</b></td> </tr>";
$carrito .= "</table>";
// devolver el resultado al llamarse.
return $carrito;
}
//manda el mail con el carrito generado
function enviar_email($de,$para,$asunto,$nombre,$comentario){
// Componer el cuerpo del mensaje .. añade tus "cabeceras y pié" del mismo si corresponde + lo que el método "_genera_carrito()" ya hace por sí solo.
$msg = "<b>Solicitud Cotización</b><br><br>";
$msg .= "<br>";
$msg .= "Nombre: " . $nombre . "<br>";
$msg .= "Email: " . $de . "<br>";
$msg .= "Comentarios: " . $comentario . "<br>";
$msg .= "<br>";
$msg .= $this->_genera_carrito()."<br><br>";
$msg .= "Fecha solicitud: ".date ("d/m/Y H:i:s"). "<br>";
$msg .= "Desde IP:".$_SERVER['REMOTE_ADDR']."<br>";
// Cabeceras que definen que el e-mail es de formato HTML.
$cabeceras = 'MIME-Version: 1.0' . "\r\n";
$cabeceras .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$cabeceras .= 'From: '. $de . "\r\n";
$cabeceras .= 'CC: '. $de . "\r\n";
mail($para,$asunto,$msg,$cabeceras);
}
//elimina un producto del carrito. recibe la linea del carrito que debe eliminar
//no lo elimina realmente, simplemente pone a cero el id, para saber que esta en estado retirado
function elimina_producto($linea){
$this->array_id_prod[$linea]=0;
}
}
//inicio la sesión
session_start();
//si no esta creado el objeto carrito en la sesion, lo creo
if (!isset($_SESSION["ocarrito"])){
$_SESSION["ocarrito"] = new carrito();
}
?>
eliminar_producto.php
Código PHP:
<?
include("lib_carrito.php");
$_SESSION["ocarrito"]->elimina_producto($_GET["linea"]);
?>
<html>
<head>
<title>Introduce Producto</title>
</head>
<body>
<table width="740" border="0" align="center">
<tr>
<td><div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Producto
eliminado. <br>
<br>
<br>
<a href="index.php"> Volver</a> <br>
<br>
<a href="ver_carrito.php">Ver carrito</a> </font></div></td>
</tr>
</table>
</body>
</html>
formulario
Código PHP:
<?
include("lib_carrito.php");
?>
<html>
<head>
<title>Mándanos tus comentarios</title>
</head>
<body bgcolor="#cccc66" text="#003300" link="#006060" vlink="#006060">
<?
if (!$HTTP_POST_VARS){
?>
nuevo 6
<form action="formulario.php" method=post>
Nombre: <input type=text name="nombre" size=16>
<br>
Email: <input type=text name=email size=16>
<br>
Comentarios: <textarea name=coment cols=32 rows=6></textarea>
<br>
<input type=submit value="Enviar">
</form>
<?
}else{
//Estoy recibiendo el formulario, compongo el cuerpo
$cuerpo = "Formulario enviado\n";
$cuerpo .= "Nombre: " . $HTTP_POST_VARS["nombre"] . "\n";
$cuerpo .= "Email: " . $HTTP_POST_VARS["email"] . "\n";
$cuerpo .= "Comentarios: " . $HTTP_POST_VARS["coment"] . "\n";
//mando el correo...
$_SESSION["ocarrito"]-> enviar_email($HTTP_POST_VARS["email"],"[email protected]","Solicitud cotización desde la web",$HTTP_POST_VARS["nombre"],$HTTP_POST_VARS["coment"]);
//doy las gracias por el envío
echo "Gracias por rellenar el formulario. Se ha enviado correctamente.";
}
?>
</body>
</html>