diff --git a/src/app/components/inicio/inicio.component.ts b/src/app/components/inicio/inicio.component.ts index 1fb66d8..8d47515 100644 --- a/src/app/components/inicio/inicio.component.ts +++ b/src/app/components/inicio/inicio.component.ts @@ -80,7 +80,7 @@ export class InicioComponent implements OnInit, AfterViewInit { if (res.length === 0) { this.productoAcargar.cantidad = 1; - this.productoService.productos.push(this.productoAcargar); + this.productoService.setProductos(this.productoAcargar); this.productoAcargar = this.productoService.productoAcargar = undefined; } else { @@ -94,7 +94,7 @@ export class InicioComponent implements OnInit, AfterViewInit { var producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar; producto.cantidad = 1; - this.productoService.productos.push(producto); + this.productoService.setProductos(producto); this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined; this.promociones = []; this.popoverDirective.hide(); diff --git a/src/app/services/producto.service.ts b/src/app/services/producto.service.ts index 5c17a58..b1f4fa2 100644 --- a/src/app/services/producto.service.ts +++ b/src/app/services/producto.service.ts @@ -28,6 +28,23 @@ export class ProductoService { setProductos(producto: Producto) { + for (let i = 0; i < this.productos.length; i++) { + + if (this.productos[i].id === producto.id) { + + if (producto.PRO) { + if (this.promosIdenticas(this.productos[i], producto)) { + this.productos[i].cantidad++; + return; + } else { + break; + } + } + this.productos[i].cantidad++; + return; + } + } + this.productos.push(producto); } @@ -66,4 +83,24 @@ export class ProductoService { }); } + private promosIdenticas(promoEnCarrito: Producto, promo: Producto) { + + var sonIdenticas = true; + var productosPromoCarrito = promoEnCarrito.productos; + var productosPromoAcargar = promo.productos; + + if (productosPromoCarrito.length !== productosPromoAcargar.length) { + return false; + } + + for (let i = 0; i < productosPromoCarrito.length; i++) { + + if (productosPromoCarrito[i].id !== productosPromoAcargar[i].id) { + return false; + } + } + + return sonIdenticas; + } + }