protected void BtnComprar_Click(object sender, EventArgs e) { try { using (ServicioCompras.ServicioClient servicio = new ServicioCompras.ServicioClient()) { Negocio.CarroCompras carro = (Negocio.CarroCompras)Session["carrito"]; Negocio.Pedido nuevoPedido = new Negocio.Pedido(); nuevoPedido.RunCliente = (string)Session["runCliente"]; nuevoPedido.FormaEntrega = this.dropFormaEntrega.SelectedItem.Text; nuevoPedido.Comentario = this.txbComentario.Text; foreach (Negocio.DetalleProductoCarro item in carro.ProductosEnCarro) { nuevoPedido.TotalVenta += item.TotalDetalle; } ; nuevoPedido.DetallePedido = new List <Negocio.DetallePedido>(); foreach (Negocio.DetalleProductoCarro item in carro.ProductosEnCarro) { Negocio.DetallePedido detalle = new Negocio.DetallePedido(); detalle.Producto = new Negocio.Producto { IdProducto = item.Id }; detalle.Cantidad = item.Cantidad; nuevoPedido.DetallePedido.Add(detalle); } if (servicio.GenerarPedido(nuevoPedido)) { this.estadoCompra.Text = "Pedido realizado con éxito"; Session["carrito"] = null; CargarTablaCarro(); } else { this.estadoCompra.Text = "Falla al realizar pedido, intente más tarde"; } } } catch (Exception) { this.estadoCompra.Text = "Falla al realizar pedido, intente más tarde"; } }
protected void CargarTablaCarro() { if (Session["carrito"] == null) { this.estadoCarro.Text = "Carro vacio"; grillaCompras.Visible = false; panelComentario.Visible = false; panelFormaEntrega.Visible = false; BtnAnular.Visible = false; BtnComprar.Visible = false; } else { Negocio.CarroCompras carrito = (Negocio.CarroCompras)Session["carrito"]; grillaCompras.DataSource = carrito.ProductosEnCarro; grillaCompras.DataBind(); } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Negocio.CarroCompras carrito = new Negocio.CarroCompras(); Session["carrito"] = carrito; } else { Negocio.CarroCompras carrito = (Negocio.CarroCompras)Session["carrito"]; if (Request.Params.Get("__EVENTARGUMENT").Equals("ejecutar")) { CargarProductosAlCarrito(); } if (Request.QueryString["idProducto"] != null) { CargarProductosAlCarrito(); } } CargasProductosEnPagina(); }
protected void Ancla_click(object sender, EventArgs e) { int id = Int32.Parse(((HtmlAnchor)sender).ID); Negocio.CarroCompras carrito = (Negocio.CarroCompras)Session["carrito"]; //consulto si existe en el carrito de compras if (carrito.ProductosEnCarro.Exists(obj => obj.Id == id)) { Negocio.DetalleProductoCarro detalle = carrito.ProductosEnCarro.First(obj => obj.Id == id); carrito.ProductosEnCarro.First(obj => obj.Id == id).Cantidad++; carrito.ProductosEnCarro.First(obj => obj.Id == id).TotalDetalle = (detalle.Cantidad + 1) * detalle.PrecioUnidad; } //si no existe en el carrito, se busca en la bd, se mapea, y se agrega al carrito con cantidad 1 else { using (ServicioCompras.ServicioClient servicio = new ServicioCompras.ServicioClient()) { Negocio.DetalleProductoCarro nuevoDetalle = new Negocio.DetalleProductoCarro(); Negocio.Producto productoClickeado = servicio.BuscarProductoID(id); nuevoDetalle.Id = id; nuevoDetalle.Nombre = productoClickeado.Nombre; nuevoDetalle.Cantidad = 1; if (productoClickeado.EnOferta.Equals("1")) { nuevoDetalle.PrecioUnidad = productoClickeado.PrecioOferta; } else { nuevoDetalle.PrecioUnidad = productoClickeado.PrecioNormal; } nuevoDetalle.TotalDetalle = nuevoDetalle.PrecioUnidad; carrito.ProductosEnCarro.Add(nuevoDetalle); } } Session["carrito"] = carrito; }
protected void CargarProductosAlCarrito() { int id = Int32.Parse(Request.Params.Get("__EVENTTARGET")); Negocio.CarroCompras carrito = (Negocio.CarroCompras)Session["carrito"]; //consulto si existe en el carrito de compras if (carrito.ProductosEnCarro.Exists(obj => obj.Id == id)) { Negocio.DetalleProductoCarro detalle = carrito.ProductosEnCarro.First(obj => obj.Id == id); carrito.ProductosEnCarro.First(obj => obj.Id == id).Cantidad++; carrito.ProductosEnCarro.First(obj => obj.Id == id).TotalDetalle = detalle.Cantidad * detalle.PrecioUnidad; } //si no existe en el carrito, se busca en la bd, se mapea, y se agrega al carrito con cantidad 1 else { using (ServicioCompras.ServicioClient servicio = new ServicioCompras.ServicioClient()) { Negocio.DetalleProductoCarro nuevoDetalle = new Negocio.DetalleProductoCarro(); Negocio.Producto productoClickeado = servicio.BuscarProductoID(id); nuevoDetalle.Id = id; nuevoDetalle.Nombre = productoClickeado.Nombre; nuevoDetalle.Cantidad = 1; if (productoClickeado.EnOferta.Equals("1")) { nuevoDetalle.PrecioUnidad = productoClickeado.PrecioOferta; } else { nuevoDetalle.PrecioUnidad = productoClickeado.PrecioNormal; } nuevoDetalle.TotalDetalle = nuevoDetalle.PrecioUnidad; carrito.ProductosEnCarro.Add(nuevoDetalle); } } Session["carrito"] = carrito; }