diff --git a/assets/tools/FlowAddToCart.js b/assets/tools/FlowAddToCart.js index 3c84a81..596b88e 100644 --- a/assets/tools/FlowAddToCart.js +++ b/assets/tools/FlowAddToCart.js @@ -43,15 +43,30 @@ export class FlowAddToCart extends HTMLElement { if (!response.ok) throw new Error('Network error'); const data = await response.json(); - if (data.dispo) { - // 4. Add to Cart - const list = JSON.parse(localStorage.getItem('pl_list') || '[]'); + if (data.dispo) { + // 4. Add to Cart + const list = JSON.parse(localStorage.getItem('pl_list') || '[]'); + + // --- Save Options --- + const selectedOptions = Array.from(document.querySelectorAll('.product-option-checkbox:checked')) + .map(cb => cb.value); + + const allOptions = JSON.parse(localStorage.getItem('pl_options') || '{}'); console.log(selectedOptions); + if (selectedOptions.length > 0) { + allOptions[this.productId] = selectedOptions; + } else { + delete allOptions[this.productId]; + } + + localStorage.setItem('pl_options', JSON.stringify(allOptions)); + // -------------------- + if (!list.includes(this.productId)) { list.push(this.productId); localStorage.setItem('pl_list', JSON.stringify(list)); window.dispatchEvent(new CustomEvent('cart:updated')); } - + // Open Cart const cart = document.querySelector('[is="flow-reserve"]'); if (cart) cart.open(); diff --git a/assets/tools/FlowReserve.js b/assets/tools/FlowReserve.js index b610170..4a9bd35 100644 --- a/assets/tools/FlowReserve.js +++ b/assets/tools/FlowReserve.js @@ -51,10 +51,26 @@ export class FlowReserve extends HTMLAnchorElement { } } + getOptions() { + try { + return JSON.parse(localStorage.getItem('pl_options') || '{}'); + } catch (e) { + return {}; + } + } + removeFromList(id) { let list = this.getList(); list = list.filter(itemId => itemId.toString() !== id.toString()); localStorage.setItem(this.storageKey, JSON.stringify(list)); + + // Remove options for this product + const options = this.getOptions(); + if (options[id]) { + delete options[id]; + localStorage.setItem('pl_options', JSON.stringify(options)); + } + window.dispatchEvent(new CustomEvent('cart:updated')); this.refreshContent(); // Re-fetch and render } @@ -211,6 +227,7 @@ export class FlowReserve extends HTMLAnchorElement { footer.innerHTML = ''; const ids = this.getList(); + const options = this.getOptions(); // Retrieve dates from localStorage let dates = { start: null, end: null }; @@ -248,6 +265,7 @@ export class FlowReserve extends HTMLAnchorElement { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ids, + options, start: dates.start, end: dates.end }) @@ -323,8 +341,22 @@ export class FlowReserve extends HTMLAnchorElement { `; } - // --- RENDER PRODUCTS --- - const productsHtml = data.products.map(product => ` + const productsHtml = data.products.map(product => { + let optionsHtml = ''; + if (product.options && product.options.length > 0) { + optionsHtml = '
{{ item.product.description }}
+{{ item.totalPriceHT|number_format(2, ',', ' ') }} € HT
@@ -61,6 +76,24 @@ {% else %}Aucun produit sélectionné.
{% endfor %} + + {# Orphan Options #} + {% if cart.options is defined and cart.options|length > 0 %} +