Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <section class="seccion contenedor">
- <h2>Resumen de tu compra</h2>
- <?php if(!empty($_SESSION['carrito'])) { ?>
- <table class="table table-light">
- <thead>
- <th width="40%">Productos</th>
- <th width="10%">Cantidad</th>
- <th width="5%">Talla</th>
- <th width="20%">Precio</th>
- <th width="20%">Total</th>
- <th width="5%"></th>
- </thead>
- <?php $total = 0;
- foreach($_SESSION['carrito'] as $item => $producto) { ?>
- <tbody>
- <td width="40%"><?php echo $producto['nombre'] ?></td>
- <td width="10%"><?php echo $producto['cantidad'] ?></td>
- <td width="5%"><?php echo $producto['talla'] ?></td>
- <td width="20%">$<?php echo $producto['precio'] ?></td>
- <td width="20%">$<?php echo number_format ($producto['precio']*$producto['cantidad'], 2) ?></td>
- <td width="5%">
- <form class="formCar" action="login.php" method="post">
- <input data-id="<?php echo $producto['id'] ?>" type="hidden" name="id" value="<?php echo $producto['id'] ?>">
- <input type="hidden" name="btnCar" value="del">
- <button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
- </form>
- </td>
- </tbody>
- <?php $total = $total + ($producto['precio']*$producto['cantidad']); ?>
- <?php } ?>
- <tfoot>
- <td colspan="4" align="left"><h4>Total</h4></td>
- <td align="left"><h4>$<?php echo number_format($total, 2); ?></h4></td>
- <td></td>
- </tfoot>
- </table>
- <?php } else { ?>
- <div class="alert alert-success">
- <p>Aun no has agregado productos a tu carrito, <a href="disenos.php">Hazlo ahora!</a></p>
- </div>
- <?php } ?>
- </section>
- // funciones carrito
- if(isset($_POST['btnCar'])){
- session_start();
- switch($_POST['btnCar']){
- // Agregar a carrito
- case 'add':
- $idProducto = $_POST['id'];
- $nombreProducto = $_POST['nombre'];
- $precioProducto = $_POST['precio'];
- $cantidadProducto = $_POST['cantidad'];
- $tallaProducto = $_POST['talla'];
- if(!isset($_SESSION['carrito'])){
- $producto = array(
- 'nombre' => $nombreProducto,
- 'id' => $idProducto,
- 'precio' => $precioProducto,
- 'cantidad' => $cantidadProducto,
- 'talla' => $tallaProducto
- );
- $_SESSION['carrito'][0] = $producto;
- $respuesta = array(
- 'respuesta' => 'exito',
- 'cuenta' => count($_SESSION['carrito'])
- );
- } else {
- $numProd = count($_SESSION['carrito']);
- $producto = array(
- 'nombre' => $nombreProducto,
- 'id' => $idProducto,
- 'precio' => $precioProducto,
- 'cantidad' => $cantidadProducto,
- 'talla' => $tallaProducto
- );
- $_SESSION['carrito'][$numProd] = $producto;
- $respuesta = array(
- 'respuesta' => 'exito',
- 'cuenta' => count($_SESSION['carrito'])
- );
- }
- die(json_encode($respuesta));
- break;
- // Borrar de carrito
- case 'del':
- $idEliminado = $_POST['id'];
- foreach($_SESSION['carrito'] as $item => $producto){
- if($producto['id'] == $idEliminado){
- unset($_SESSION['carrito'][$item]);
- $respuesta = array(
- 'respuesta' => 'exito',
- 'idEliminado' => $idEliminado,
- 'cuenta' => count($_SESSION['carrito'])
- );
- }
- die(json_encode($respuesta));
- }
- break;
- }
- }
- // Borrar de carrito
- $('.formCar').on('submit', function(e){
- e.preventDefault();
- var datos = $(this).serializeArray();
- $.ajax({
- type: $(this).attr('method'),
- data: datos,
- url: $(this).attr('action'),
- dataType: 'json',
- success: function(data){
- Swal.fire({
- title: 'Eliminar del carrito?',
- text: "Seguro?",
- type: 'warning',
- showCancelButton: true,
- confirmButtonColor: '#3085d6',
- cancelButtonColor: '#d33',
- confirmButtonText: 'Si, eliminar!',
- cancelButtonText: 'Cancelar'
- }).then((result) => {
- if (result.value) {
- $('[data-id="'+data.idEliminado+'"]').parents('tbody').remove();
- $('.carCount').empty();
- $('.carCount').append(data.cuenta);
- console.log(data);
- Swal.fire(
- 'Ok!',
- 'Disenio eliminado!',
- 'success'
- )
- }
- })
- }
- })
- });
- Uncaught (in promise) TypeError: Cannot read property 'idEliminado' of null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement