/** * Cart Component - Handles shopping cart functionality */ const CartComponent = { /** * Initialize cart state * @returns {Object} */ getInitialState() { return { cart: [], shipping: '8.95' }; }, /** * Get cart methods for Alpine.js component * @returns {Object} */ getMethods() { return { /** * Add item to cart * @param {Object} item - Item with id, name, price, and optional variation_id */ addToCart(item) { this.cart.push({ id: parseInt(item.id), variation_id: item.variation_id || 0, name: item.name, price: item.price }); }, /** * Remove item from cart by index * @param {number} index */ removeFromCart(index) { this.cart.splice(index, 1); }, /** * Check if product is in cart * @param {number} id * @returns {boolean} */ isInCart(id) { return this.cart.some(i => parseInt(i.id) === parseInt(id)); }, /** * Toggle upsell product in cart * @param {Object} product */ toggleUpsell(product) { const idx = this.cart.findIndex(i => parseInt(i.id) === parseInt(product.id)); if (idx > -1) { this.cart.splice(idx, 1); } else { this.cart.push({ id: parseInt(product.id), name: product.name, price: product.price }); } }, /** * Clear the cart */ clearCart() { this.cart = []; }, /** * Reset cart and shipping for new order */ resetCart() { this.cart = []; this.shipping = '8.95'; } }; }, /** * Get computed properties for Alpine.js component * @returns {Object} */ getComputed() { return { /** * Calculate total including shipping * @returns {string} */ total() { const itemsTotal = this.cart.reduce((sum, item) => sum + parseFloat(item.price), 0); return (itemsTotal + (parseFloat(this.shipping) || 0)).toFixed(2); } }; } };