-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (55 loc) · 2.27 KB
/
Copy pathscript.js
File metadata and controls
68 lines (55 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const productos = [
{ id: 1, nombre: "Aceite de Boldo Nativo", categoria: "salud", precio: 12500, img: "🍃" },
{ id: 2, nombre: "Crema Cicatrizante Matico", categoria: "estetica", precio: 8900, img: "🌸" },
{ id: 3, nombre: "Extracto de Quillay (5L)", categoria: "insumos", precio: 45000, img: "📦" },
{ id: 4, nombre: "Serum Facial Alerce", categoria: "estetica", precio: 15600, img: "💧" }
];
let carrito = [];
function renderProducts(filter = 'todos') {
const grid = document.getElementById('product-grid');
grid.innerHTML = '';
const filtrados = filter === 'todos' ? productos : productos.filter(p => p.categoria === filter);
filtrados.forEach(p => {
grid.innerHTML += `
<div class="product-card">
<div style="font-size: 3rem">${p.img}</div>
<h3>${p.nombre}</h3>
<p class="price">$${p.precio}</p>
<div class="controls">
<input type="number" id="qty-${p.id}" value="1" min="1">
<button onclick="addToCart(${p.id})">Agregar</button>
</div>
</div>
`;
});
}
function addToCart(id) {
const qty = parseInt(document.getElementById(`qty-${id}`).value);
const producto = productos.find(p => p.id === id);
const existe = carrito.find(item => item.id === id);
if (existe) {
existe.cantidad += qty;
} else {
carrito.push({ ...producto, cantidad: qty });
}
updateUI();
}
function updateUI() {
document.getElementById('cart-count').innerText = carrito.reduce((acc, item) => acc + item.cantidad, 0);
const cartItems = document.getElementById('cart-items');
const totalSpan = document.getElementById('total-price');
cartItems.innerHTML = '';
let total = 0;
carrito.forEach(item => {
total += item.precio * item.cantidad;
cartItems.innerHTML += `<p>${item.nombre} x${item.cantidad} - $${item.precio * item.cantidad}</p>`;
});
totalSpan.innerText = total;
}
function toggleCart() {
const modal = document.getElementById('cart-modal');
modal.style.display = modal.style.display === 'block' ? 'none' : 'block';
}
function filterProducts(cat) { renderProducts(cat); }
// Inicializar
renderProducts();