Te explico como lo tengo hecho. Tengo una pagina libreria donde estan incluidas las distintas funciones. A esta página la llamo en forma de include en todas las demás:
include("lib_carrito.php"); lib_carrito.php Código PHP:
<?
class carrito {
//atributos de la clase
var $num_productos;
var $array_id_prod;
var $array_familia_prod;
var $array_producto_prod;
var $array_referencia_prod;
var $array_articulo_prod;
var $array_precio_prod;
var $array_cantidad_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,$familia_prod,$producto_prod,$referencia_prod,$articulo_prod,$precio_prod,$cantidad_prod){
$this->array_id_prod[$this->num_productos]=$id_prod;
$this->array_familia_prod[$this->num_productos]=$familia_prod;
$this->array_producto_prod[$this->num_productos]=$producto_prod;
$this->array_referencia_prod[$this->num_productos]=$referencia_prod;
$this->array_articulo_prod[$this->num_productos]=$articulo_prod;
$this->array_precio_prod[$this->num_productos]=$precio_prod;
$this->array_cantidad_prod[$this->num_productos]=$cantidad_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 cellspacing='0' cellpadding='0' border='0'>
<tr>
<td width='711' bgcolor='#164D89' height='1' class='px' colspan='9'>
</td>
</tr>
<tr>
<td class='t9bl' bgcolor='#557FD7' height='20' width='110'>Familia
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='90'>Producto
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='80'>Referencia
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='177'>Artículo
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='70' align='center'>Precio
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='30' align='center'>Cantidad
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='40' align='center'>Descuento
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='80' align='center'>Importe
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='20' align='center'>Eliminar
</td>
</tr>
<tr>
<td width='711' bgcolor='#164D89' height='1' class='px' colspan='9'>
</td>
</tr>
<tr>
<td height='10' class='px' colspan='8'>
</td>
</tr>
<tr>
<td width='711' bgcolor='#164D89' height='1' class='px' colspan='9'>
</td>
</tr>");
for ($i=0;$i<$this->num_productos;$i++){
if($this->array_id_prod[$i]!=0){
echo '<tr>';
echo "<td class='txt3p' height='30'>" . $this->array_familia_prod[$i] . "</td>";
echo "<td class='txt3p' height='30'>" . $this->array_producto_prod[$i] . "</td>";
echo "<td class='txt3p' height='30'>" . $this->array_referencia_prod[$i] . "</td>";
echo "<td class='txt3p' height='30'>" . $this->array_articulo_prod[$i] . "</td>";
echo "<td class='txt3p' height='30' align='center'>" . $this->array_precio_prod[$i] . "</td>";
echo "<td class='txt3p' height='30' align='center'>" . $this->array_cantidad_prod[$i] . "</td>";
if($this->array_familia_prod[$i]=='Material Oficina')
{
echo "<td class='txt3p' height='30'><center>$descuento %</center></td>";
$rebaja = $descuento/100;
$desc = $this->array_precio_prod[$i]*$this->array_cantidad_prod[$i]*$rebaja;
$importe = round($this->array_precio_prod[$i]*$this->array_cantidad_prod[$i]-$desc,2);
echo "<td class='txt3p' height='30'><center>$importe €</center></td>";
echo "<td class='txt3p' height='30'><a href='eliminar_producto.php?linea=$i' class='lkng'>Eliminar</td>";
echo '</tr>';
echo'<tr>';
echo"<td height='10' class='px' colspan='9'>";
echo"</td>";
echo'</tr>';
echo'<tr>';
echo"<td width='545' bgcolor='#164D89' height='1' class='px' colspan='9'>";
echo"</td>";
echo'</tr>';
$suma = $suma+$importe;
}
}
echo"<tr>
<td height='20' class='px' colspan='9'>
</td>
</tr>";
//muestro el total
echo "<tr>
<td class='txt3'><b>TOTAL:</b></td>
<td class='txt3'> <b>$suma €</b></td>
<td> </td>
</tr>";
//total más IVA
$totaliva=round($suma * 1.16, 2);
$_SESSION[pago] = $totaliva;
echo "<tr>
<td class='txt3'><b>TOTAL + IVA (16%):</b></td>
<td class='txt3'><b>$totaliva €</b></td>
<td> </td>
</tr>";
echo "</table>";
}
//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();
}
?>
Y esta es la que envía (
validarpedido.php)
Código PHP:
<?php
include("lib_carrito.php");
if(!$_SESSION[auth])
{
header("Location: index.php");
}
$sfrom="$email"; //cuenta que envia
$sdestinatario="[email protected]"; //cuenta destino
$ssubject="Pedido desde la página web"; //subject
$shtml="<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>Pedido desde la página web</title>
<style>
.txt{
font-family: arial;
font-size: 9px;
color: '#444444';
}
.tit{
font-family: arial;
font-size: 11px;
color: '#444444';
font-weight: bold;
padding-left: 5px;
padding-right: 5px;
}
</style>
<link rel='stylesheet' href='http://www.buraltec.com/estilos.css' type='text/css'>
</head>
<body bgcolor='#E0E4EC' marginwidth='0' marginheight='0' topmargin='40' leftmargin='0'>
<table align='center' bgcolor='ffffff' width='771' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td colspan='2' class='a11gr' height='80'><center><font color='#357EB5'><u>HA RECIBIDO EL SIGUIENTE PEDIDO DESDE LA PÁGINA WEB</u></font></center>
</td>
</tr>
<tr>
<td valign='top' width='40%' height='200' align='center'><img src='http://www.uvedoblestudio.com/img/cereza.jpg' width='150' height='200'>
</td>
<td valign='top' class='tit'><u>ENVÍA:</u><br><br><br><span class='txt'><b>Empresa:</b> $empresa<br><br><b>CIF:</b> $cif<br><br><b>Nombre:</b> $nombre<br><br><b>Dirección:</b> $direccion - $municipio [ $provincia ]<br><br><b>Tlfno.:</b> $telefono - <b>Fax:</b> $fax<br><br><b>E-mail:</b> $email</span><br><br><br><u>PEDIDO:</u><br><br>
</td>
</tr>
<tr>
<td valign='top' colspan='2' class='px'>
</td>
</tr>
<tr>
<td valign='top' colspan='2' class='px'>
<table cellspacing='0' cellpadding='0' border='0'>
<tr>
<td width='711' bgcolor='#164D89' height='1' class='px' colspan='9'>
</td>
</tr>
<tr>
<td class='t9bl' bgcolor='#557FD7' height='20' width='110'>Familia
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='90'>Producto
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='80'>Referencia
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='177'>Artículo
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='70' align='center'>Precio
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='30' align='center'>Cantidad
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='40' align='center'>Descuento
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='80' align='center'>Importe
</td>
<td class='t9bl' bgcolor='#557FD7' height='20' width='20' align='center'>Eliminar
</td>
</tr>
<tr>
<td width='711' bgcolor='#164D89' height='1' class='px' colspan='9'>
</td>
</tr>
<tr>
<td height='10' class='px' colspan='8'>
</td>
</tr>
<tr>
<td width='711' bgcolor='#164D89' height='1' class='px' colspan='9'>
</td>
</tr>
</table>";
$shtml .=imprime_carrito();
$shtml .="</td>
</tr>
<tr>
<td valign='top' colspan='2' height='30'>
</td>
</tr>
</table>
</body>
</html>
"; //mensaje
$sheader="From:".$sfrom."\nReply-To:".$sfrom."\n";
$sheader=$sheader."X-Mailer:PHP/".phpversion()."\n";
$sheader=$sheader."Mime-Version: 1.0\n";
$sheader=$sheader."Content-Type: text/html";
mail($sdestinatario,$ssubject,$shtml,$sheader);
if(mail)
{
echo("Envío correcto");
session_destroy();
}
else
{
echo("Atención! Ocurrió un error, si éste persiste comuniquelo a los responsables del web. Gracias<br /><a href='home.php'>Salir</a>");
}
?>