Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. <section class="seccion contenedor">
  2. <h2>Resumen de tu compra</h2>
  3. <?php if(!empty($_SESSION['carrito'])) { ?>
  4. <table class="table table-light">
  5. <thead>
  6. <th width="40%">Productos</th>
  7. <th width="10%">Cantidad</th>
  8. <th width="5%">Talla</th>
  9. <th width="20%">Precio</th>
  10. <th width="20%">Total</th>
  11. <th width="5%"></th>
  12. </thead>
  13. <?php $total = 0;
  14. foreach($_SESSION['carrito'] as $item => $producto) { ?>
  15. <tbody>
  16. <td width="40%"><?php echo $producto['nombre'] ?></td>
  17. <td width="10%"><?php echo $producto['cantidad'] ?></td>
  18. <td width="5%"><?php echo $producto['talla'] ?></td>
  19. <td width="20%">$<?php echo $producto['precio'] ?></td>
  20. <td width="20%">$<?php echo number_format ($producto['precio']*$producto['cantidad'], 2) ?></td>
  21. <td width="5%">
  22. <form class="formCar" action="login.php" method="post">
  23. <input data-id="<?php echo $producto['id'] ?>" type="hidden" name="id" value="<?php echo $producto['id'] ?>">
  24. <input type="hidden" name="btnCar" value="del">
  25. <button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
  26. </form>
  27. </td>
  28. </tbody>
  29. <?php $total = $total + ($producto['precio']*$producto['cantidad']); ?>
  30. <?php } ?>
  31. <tfoot>
  32. <td colspan="4" align="left"><h4>Total</h4></td>
  33. <td align="left"><h4>$<?php echo number_format($total, 2); ?></h4></td>
  34. <td></td>
  35. </tfoot>
  36. </table>
  37. <?php } else { ?>
  38. <div class="alert alert-success">
  39. <p>Aun no has agregado productos a tu carrito, <a href="disenos.php">Hazlo ahora!</a></p>
  40. </div>
  41. <?php } ?>
  42. </section>
  43.  
  44. // funciones carrito
  45. if(isset($_POST['btnCar'])){
  46.  
  47. session_start();
  48.  
  49. switch($_POST['btnCar']){
  50. // Agregar a carrito
  51. case 'add':
  52.  
  53. $idProducto = $_POST['id'];
  54. $nombreProducto = $_POST['nombre'];
  55. $precioProducto = $_POST['precio'];
  56. $cantidadProducto = $_POST['cantidad'];
  57. $tallaProducto = $_POST['talla'];
  58.  
  59. if(!isset($_SESSION['carrito'])){
  60. $producto = array(
  61. 'nombre' => $nombreProducto,
  62. 'id' => $idProducto,
  63. 'precio' => $precioProducto,
  64. 'cantidad' => $cantidadProducto,
  65. 'talla' => $tallaProducto
  66. );
  67. $_SESSION['carrito'][0] = $producto;
  68. $respuesta = array(
  69. 'respuesta' => 'exito',
  70. 'cuenta' => count($_SESSION['carrito'])
  71. );
  72. } else {
  73. $numProd = count($_SESSION['carrito']);
  74. $producto = array(
  75. 'nombre' => $nombreProducto,
  76. 'id' => $idProducto,
  77. 'precio' => $precioProducto,
  78. 'cantidad' => $cantidadProducto,
  79. 'talla' => $tallaProducto
  80. );
  81. $_SESSION['carrito'][$numProd] = $producto;
  82. $respuesta = array(
  83. 'respuesta' => 'exito',
  84. 'cuenta' => count($_SESSION['carrito'])
  85. );
  86. }
  87. die(json_encode($respuesta));
  88. break;
  89. // Borrar de carrito
  90. case 'del':
  91.  
  92. $idEliminado = $_POST['id'];
  93.  
  94. foreach($_SESSION['carrito'] as $item => $producto){
  95. if($producto['id'] == $idEliminado){
  96. unset($_SESSION['carrito'][$item]);
  97. $respuesta = array(
  98. 'respuesta' => 'exito',
  99. 'idEliminado' => $idEliminado,
  100. 'cuenta' => count($_SESSION['carrito'])
  101. );
  102. }
  103. die(json_encode($respuesta));
  104. }
  105. break;
  106. }
  107. }
  108.  
  109. // Borrar de carrito
  110. $('.formCar').on('submit', function(e){
  111. e.preventDefault();
  112.  
  113. var datos = $(this).serializeArray();
  114.  
  115. $.ajax({
  116. type: $(this).attr('method'),
  117. data: datos,
  118. url: $(this).attr('action'),
  119. dataType: 'json',
  120. success: function(data){
  121. Swal.fire({
  122. title: 'Eliminar del carrito?',
  123. text: "Seguro?",
  124. type: 'warning',
  125. showCancelButton: true,
  126. confirmButtonColor: '#3085d6',
  127. cancelButtonColor: '#d33',
  128. confirmButtonText: 'Si, eliminar!',
  129. cancelButtonText: 'Cancelar'
  130. }).then((result) => {
  131. if (result.value) {
  132. $('[data-id="'+data.idEliminado+'"]').parents('tbody').remove();
  133. $('.carCount').empty();
  134. $('.carCount').append(data.cuenta);
  135. console.log(data);
  136. Swal.fire(
  137. 'Ok!',
  138. 'Disenio eliminado!',
  139. 'success'
  140. )
  141. }
  142. })
  143. }
  144. })
  145. });
  146.  
  147. Uncaught (in promise) TypeError: Cannot read property 'idEliminado' of null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement