@@ -3,120 +3,141 @@ listarProductosCarrito();
33
44function listarProductosCarrito ( ) {
55
6- // Obtener el carrito desde localStorage
7- let carrito = obtenerCarritoDeLocalStorage ( )
6+ let carrito = obtenerCarritoDeLocalStorage ( ) // Obtener el carrito desde localStorage. En cart.js
7+
8+ let productosAgrupados = carrito . reduce ( ( agrupar , producto ) => { // Agrupar los productos por id
9+ if ( ! agrupar [ producto . id ] ) {
10+ agrupar [ producto . id ] = { ...producto , cantidad : 0 } ;
11+ }
12+ agrupar [ producto . id ] . cantidad += 1 ;
13+ return agrupar ;
14+ } , { } ) ;
815
9- // Agrupar los productos por id
10- let productosAgrupados = carrito . reduce ( ( acc , producto ) => {
11- if ( ! acc [ producto . id ] ) {
12- acc [ producto . id ] = { ...producto , cantidad : 0 } ;
13- }
14- acc [ producto . id ] . cantidad += 1 ;
15- return acc ;
16- } , { } ) ;
16+ productosAgrupados = Object . values ( productosAgrupados ) ; // Convertir el objeto en un array
1717
18- // Convertir el objeto en un array
19- productosAgrupados = Object . values ( productosAgrupados ) ;
18+ const carritoContainer = document . getElementById ( 'carrito-items' ) ; // Obtener el contenedor del carrito
2019
21- // Obtener el contenedor del carrito
22- const carritoContainer = document . getElementById ( 'carrito-items' ) ;
2320
24- // Mostrar los productos agrupados en el contenedor
25- productosAgrupados . forEach ( producto => {
21+ productosAgrupados . forEach ( producto => { // Mostrar los productos agrupados en el contenedor
2622
2723 const total = producto . precio * producto . cantidad ;
2824 const productoElemento = document . createElement ( 'tr' ) ;
2925
30- productoElemento . innerHTML = `
31- <tbody>
32- <tr>
33- <TD> ${ producto . nombre } </TD>
34- <TD> ${ producto . precio } $ </TD>
35- <TD> ${ producto . cantidad } </TD>
36- <TD> ${ total } $ </TD>
37- <td> <button onclick="cambiarCantidad(${ producto . id } ,-1)"> - </button>
38- <button onclick="cambiarCantidad(${ producto . id } ,1)"> + </button>
39- </td>
40- </tr>
41- </tbody>
26+ productoElemento . innerHTML = `
27+ <tbody>
28+ <tr>
29+ <TD> ${ producto . nombre } </TD>
30+ <TD> ${ producto . precio } $ </TD>
31+ <TD> ${ producto . cantidad } </TD>
32+ <TD> ${ total } $ </TD>
33+ <td> <button onclick="borrarItem(${ producto . id } ,-1)"> Quitar </button>
34+ </tr>
35+ </tbody>
36+ ` ;
37+
38+ carritoContainer . appendChild ( productoElemento ) ;
39+ } ) ;
40+ /* console.log(productosAgrupados); */
41+ if ( productosAgrupados . length <= 0 ) {
42+
43+ const productoElemento = document . createElement ( 'tr' ) ;
44+ productoElemento . innerHTML = `
45+ <tbody>
46+ <Tr>
47+ <td colspan="5">CARRITO VACIO</td>
48+ </Tr>
49+ </tbody>
50+ ` ;
51+ carritoContainer . appendChild ( productoElemento ) ;
52+ }
4253
43-
44- ` ;
45- carritoContainer . appendChild ( productoElemento ) ;
46- } ) ;
47- totalCarrito ( )
54+ totalCarrito ( )
4855}
4956
57+ function obtenerCarritoDeLocalStorage ( ) {
58+ let carrito = JSON . parse ( localStorage . getItem ( 'carrito' ) ) || [ ] ;
59+ return carrito
60+ }
61+
5062/**
5163 * INSTRUCCIONES AL TERMINAR LA COMPRA
5264 */
5365function comprarCarrito ( ) {
5466 let carrito = obtenerCarritoDeLocalStorage ( )
5567 let total = 0 ;
5668 let IVA = 22 ;
57- carrito . forEach ( item => {
58- total += item . precio ;
59- } ) ;
60- let aPagar = total + ( total * ( IVA ) / 100 ) ;
61- console . log ( aPagar ) ;
69+
70+ carrito . forEach ( item => {
71+ total += item . precio ; } ) ;
72+
73+ let aPagar = total + ( total * ( IVA ) / 100 ) ;
74+ console . log ( aPagar ) ; //Verificacion valores
6275}
6376
6477
6578function totalCarrito ( ) {
6679 let IVA = 22 ;
6780 let carrito = obtenerCarritoDeLocalStorage ( )
68-
69- // Obtener el contenedor del carrito
70- const carritoTtoal = document . getElementById ( 'carrito-values' ) ;
81+ const carritoTtoal = document . getElementById ( 'carrito-values' ) ; // Obtener el contenedor del carrito
7182
72- // Calcular el total de los precios
73- let total = 0 ;
74- carrito . forEach ( item => {
75- total += item . precio ;
76- } ) ;
77-
78- // Crear la tabla HTML dinámicamente
79- const tabla = document . createElement ( 'tr' ) ;
80- let aPagar = total + ( total * ( IVA ) / 100 ) ;
81- tabla . innerHTML = `
82- <tbody>
83- <Tr>
84- <Td> ${ total } </Td>
85- <Td> ${ IVA } % </Td>
86- <Td> </Th>
87- <Td> ${ aPagar } </Td>
88- </Tr>
89- </tbody>
90- ` ;
91- carritoTtoal . innerHTML = '' ;
92- carritoTtoal . appendChild ( tabla ) ;
83+ // Calcular total
84+ let total = 0 ;
85+ carrito . forEach ( item => {
86+ total += item . precio ; } ) ;
87+ if ( carrito != 0 ) {
88+ const tabla = document . createElement ( 'tr' ) ;
89+ let aPagarSinRedondear = ( total + ( total * ( IVA ) / 100 ) ) ;
90+ let aPagar = aPagarSinRedondear . toFixed ( 2 ) ; //DEVUELVE EN FORMATO STRING
91+ tabla . innerHTML = `
92+ <tbody>
93+ <Tr>
94+ <Td> ${ total } </Td>
95+ <Td> ${ IVA } % </Td>
96+ <Td> </Td>
97+ <Td> ${ aPagar } </Td>
98+ </Tr>
99+ </tbody>
100+ ` ;
101+ /* carritoTtoal.innerHTML = '';//VERIFICACION DE VALORES */
102+ carritoTtoal . appendChild ( tabla ) ;
103+ } else {
104+ const tabla = document . createElement ( 'tr' ) ;
105+ tabla . innerHTML = `
106+ <tbody>
107+ <Tr>
108+ <td colspan="4">SIN ITEMS PARA SUMAR</td>
109+ </Tr>
110+ </tbody>
111+ ` ;
93112
94- } //FIN DEL SCRIPT
113+ carritoTtoal . appendChild ( tabla ) ;
114+ }
115+ }
116+
95117
118+ function borrarItem ( id ) {
119+ let carrito = localStorage . getItem ( 'carrito' ) ;
120+ if ( carrito ) {
121+ carrito = JSON . parse ( carrito ) ;
96122
123+ const indice = carrito . findIndex ( producto => producto . id === id ) ;
97124
98- // Función para cambiar la cantidad de un producto en el carrito
99- function cambiarCantidad ( productId , cambio ) {
100- // Obtener el carrito desde localStorage
101- let carrito = obtenerCarritoDeLocalStorage ( )
125+ if ( indice !== - 1 ) {
126+ carrito . splice ( indice , 1 ) ;
102127
103- // Encontrar el producto en el carrito y cambiar su cantidad
104- let productoIndex = carrito . findIndex ( producto => producto . id === productId ) ;
128+ localStorage . setItem ( 'carrito' , JSON . stringify ( carrito ) )
105129
106- if ( productoIndex !== - 1 ) {
107- if ( cambio > 0 || ( cambio < 0 && carrito [ productoIndex ] . cantidad > 1 ) ) {
108- carrito [ productoIndex ] . cantidad = ( carrito [ productoIndex ] . cantidad || 1 ) + cambio ;
109- }
130+ console . log ( `Elemento eliminado con ID: ${ id } ` ) ;
131+ location . reload ( )
132+ } else {
133+ console . log ( `No s eencontro el id: ${ id } ` ) ;
134+ location . reload ( )
135+ } ;
110136
111- // Si la cantidad es 0 o menor, eliminar el producto del carrito
112- if ( carrito [ productoIndex ] . cantidad <= 0 ) {
113- carrito . splice ( productoIndex , 1 ) ;
137+ } else {
138+ console . log ( `No exite elemento` ) ;
139+ location . reload ( )
114140 }
141+ } //FIN DEL SCRIPT
115142
116- // Guardar el carrito actualizado en localStorage
117- localStorage . setItem ( 'carrito' , JSON . stringify ( carrito ) ) ;
118-
119- // Actualizar la vista del carrito
120- listarProductosCarrito ( ) ;
121- }
122- }
143+
0 commit comments