| 68 | } |
| 69 | |
| 70 | function addToCart(product: Product) { |
| 71 | setCart((prev) => { |
| 72 | const existing = prev.find((i) => i.id === product.id); |
| 73 | if (existing) { |
| 74 | return prev.map((i) => |
| 75 | i.id === product.id ? { ...i, qty: i.qty + 1 } : i |
| 76 | ); |
| 77 | } |
| 78 | return [...prev, { ...product, qty: 1 }]; |
| 79 | }); |
| 80 | op.track('add_to_cart', { |
| 81 | product_id: product.id, |
| 82 | product_name: product.name, |
| 83 | price: product.price, |
| 84 | category: product.category, |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | function removeFromCart(id: string) { |
| 89 | const item = cart.find((i) => i.id === id); |