Commit 2d442ac850fd678a5c16d80b6bd7d6323517e8ff
1 parent
81bae83d2f
Exists in
master
and in
1 other branch
Logica para sumar cantidad en productos iguales.
Showing
2 changed files
with
39 additions
and
2 deletions
Show diff stats
src/app/components/inicio/inicio.component.ts
| ... | ... | @@ -80,7 +80,7 @@ export class InicioComponent implements OnInit, AfterViewInit { |
| 80 | 80 | if (res.length === 0) { |
| 81 | 81 | |
| 82 | 82 | this.productoAcargar.cantidad = 1; |
| 83 | - this.productoService.productos.push(this.productoAcargar); | |
| 83 | + this.productoService.setProductos(this.productoAcargar); | |
| 84 | 84 | this.productoAcargar = this.productoService.productoAcargar = undefined; |
| 85 | 85 | } else { |
| 86 | 86 | |
| ... | ... | @@ -94,7 +94,7 @@ export class InicioComponent implements OnInit, AfterViewInit { |
| 94 | 94 | |
| 95 | 95 | var producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar; |
| 96 | 96 | producto.cantidad = 1; |
| 97 | - this.productoService.productos.push(producto); | |
| 97 | + this.productoService.setProductos(producto); | |
| 98 | 98 | this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined; |
| 99 | 99 | this.promociones = []; |
| 100 | 100 | this.popoverDirective.hide(); |
src/app/services/producto.service.ts
| ... | ... | @@ -28,6 +28,23 @@ export class ProductoService { |
| 28 | 28 | |
| 29 | 29 | setProductos(producto: Producto) { |
| 30 | 30 | |
| 31 | + for (let i = 0; i < this.productos.length; i++) { | |
| 32 | + | |
| 33 | + if (this.productos[i].id === producto.id) { | |
| 34 | + | |
| 35 | + if (producto.PRO) { | |
| 36 | + if (this.promosIdenticas(this.productos[i], producto)) { | |
| 37 | + this.productos[i].cantidad++; | |
| 38 | + return; | |
| 39 | + } else { | |
| 40 | + break; | |
| 41 | + } | |
| 42 | + } | |
| 43 | + this.productos[i].cantidad++; | |
| 44 | + return; | |
| 45 | + } | |
| 46 | + } | |
| 47 | + | |
| 31 | 48 | this.productos.push(producto); |
| 32 | 49 | } |
| 33 | 50 | |
| ... | ... | @@ -66,4 +83,24 @@ export class ProductoService { |
| 66 | 83 | }); |
| 67 | 84 | } |
| 68 | 85 | |
| 86 | + private promosIdenticas(promoEnCarrito: Producto, promo: Producto) { | |
| 87 | + | |
| 88 | + var sonIdenticas = true; | |
| 89 | + var productosPromoCarrito = promoEnCarrito.productos; | |
| 90 | + var productosPromoAcargar = promo.productos; | |
| 91 | + | |
| 92 | + if (productosPromoCarrito.length !== productosPromoAcargar.length) { | |
| 93 | + return false; | |
| 94 | + } | |
| 95 | + | |
| 96 | + for (let i = 0; i < productosPromoCarrito.length; i++) { | |
| 97 | + | |
| 98 | + if (productosPromoCarrito[i].id !== productosPromoAcargar[i].id) { | |
| 99 | + return false; | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 103 | + return sonIdenticas; | |
| 104 | + } | |
| 105 | + | |
| 69 | 106 | } |