1// /checkout/cart/assets/js/cart-api.js 2class CartAPI { 3 constructor(baseUrl = '/api') { 4 this.baseUrl = baseUrl; 5 } 6 7 async getCart({ 8 sessionId, 9 userId = null, 10 shippingMethod = null, 11 countryCode = null, 12 postalCode = null 13 } = {}) { 14 const params = new URLSearchParams(); 15 16 if (sessionId) params.append('sessionId', sessionId); 17 if (userId) params.append('userId', userId); 18 if (shippingMethod) params.append('shippingMethod', shippingMethod); 19 if (countryCode) params.append('countryCode', countryCode); 20 if (postalCode) params.append('postalCode', postalCode); 21 22 // Détecter dynamiquement le site 23 const hostname = window.location.hostname.toLowerCase(); 24 if (/^(?:www\\.)?francebatterie\\.(?:com|local)$/.test(hostname) || hostname === 'localhost_francebatterie') params.append('site', 'francebatterie'); 25 else if (/^(?:www\\.)?batteriedeportable\\.(?:com|local)$/.test(hostname) || hostname === 'localhost_batteriedeportable') params.append('site', 'batteriedeportable'); 26 else if (/^(?:www\\.)?chargeurdeportable\\.(?:com|local)$/.test(hostname) || hostname === 'localhost_chargeurdeportable') params.append('site', 'chargeurdeportable'); 27 else if (/^(?:www\\.)?batterieland\\.(?:com|local)$/.test(hostname) || hostname === 'localhost_batterieland') params.append('site', 'batterieland'); 28 29 //console.log('Fetching cart with params:', params.toString()); 30 31 const response = await fetch(`${this.baseUrl}?path=cart&${params}`); 32 33 if (!response.ok) { 34 throw new Error(`HTTP error! status: ${response.status}`); 35 } 36 37 return await response.json(); 38 } 39 40 async loadCartFromSession(sessionId) { 41 return await this.getCart({ sessionId }); 42 } 43 44 async addItem(articleId, quantity, sessionId, userId = null, countryCode = null, urlStat = null) { 45 const response = await fetch(`${this.baseUrl}?path=cart/add`, { 46 method: 'POST', 47 headers: { 48 'Content-Type': 'application/json', 49 }, 50 body: JSON.stringify({ 51 idArticle: articleId, 52 quantity: parseInt(quantity), 53 sessionId, 54 userId, 55 countryCode, 56 urlStat 57 }) 58 }); 59 60 if (!response.ok) { 61 throw new Error(`HTTP error! status: ${response.status}`); 62 } 63 64 return await response.json(); 65 } 66 67 async updateQuantity(articleId, quantity, sessionId, userId = null, countryCode = null) { 68 const response = await fetch(`${this.baseUrl}?path=cart/update/${articleId}`, { 69 method: 'PUT', 70 headers: { 'Content-Type': 'application/json' }, 71 body: JSON.stringify({ 72 quantity: parseInt(quantity), 73 sessionId, 74 userId, 75 countryCode 76 }) 77 }); 78 79 if (!response.ok) { 80 throw new Error(`HTTP error! status: ${response.status}`); 81 } 82 83 return await response.json(); 84 } 85 86 async removeItem(articleId, sessionId, userId = null) { 87 const params = new URLSearchParams(); 88 if (sessionId) params.append('sessionId', sessionId); 89 if (userId) params.append('userId', userId); 90 91 const url = `${this.baseUrl}?path=cart/remove/${articleId}&${params}`; 92 93 const response = await fetch(url, { 94 method: 'DELETE', 95 headers: { 96 'Content-Type': 'application/json', 97 } 98 // Pas de body nécessaire ici, tout est dans URL 99 }); 100 101 if (!response.ok) { 102 throw new Error(`HTTP error! status: ${response.status}`); 103 } 104 105 return await response.json(); 106 } 107 108 async clearCart(sessionId, userId = null) { 109 const response = await fetch(`${this.baseUrl}?path=cart/clear`, { 110 method: 'POST', 111 headers: { 112 'Content-Type': 'application/json', 113 }, 114 body: JSON.stringify({ 115 sessionId, 116 userId 117 }) 118 }); 119 120 if (!response.ok) { 121 throw new Error(`HTTP error! status: ${response.status}`); 122 } 123 124 return await response.json(); 125 } 126 127 async testConnection() { 128 const startTime = Date.now(); 129 try { 130 await this.request('/cart/test'); 131 const latency = Date.now() - startTime; 132 return { connected: true, latency }; 133 } catch (error) { 134 return { connected: false, error: error.message }; 135 } 136 } 137 138 async recalculateShipping({ sessionId, userId, shippingMethod, postalCode, countryCode, site }) { 139 const response = await fetch(`${this.baseUrl}?path=cart/recalculate-shipping`, { 140 method: 'POST', 141 headers: { 'Content-Type': 'application/json' }, 142 body: JSON.stringify({ 143 sessionId, 144 userId, 145 shippingMethod, 146 postalCode, 147 countryCode,
148 site 149 }) 150 }); 151 152 if (!response.ok) { 153 throw new Error('Erreur lors du recalcul des frais de port'); 154 } 155 156 return await response.json(); 157 } 158 159 async getUserFromId(userId) { 160 const response = await fetch(`${this.baseUrl}?path=user/${userId}?site=${window.location.hostname}`); 161 162 if (!response.ok) { 163 throw new Error(`HTTP error! status: ${response.status}`); 164 } 165 return await response.json(); 166 } 167 168 async applyCoupon({ sessionId, userId, code, shippingMethod, postalCode, countryCode, site }) { 169 const response = await fetch(`${this.baseUrl}?path=cart/apply-coupon`, { 170 method: 'POST', 171 headers: { 'Content-Type': 'application/json' }, 172 body: JSON.stringify({ 173 sessionId, 174 userId, 175 code, 176 shippingMethod, 177 postalCode, 178 countryCode, 179 site 180 }) 181 }); 182 183 if (!response.ok) { 184 throw new Error(`HTTP error! status: ${response.status}`); 185 } 186 187 return await response.json(); 188 } 189 190} 191 192// Exporter la classe 193window.CartAPI = CartAPI;
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.