nagasurendra commited on
Commit
85c4a46
·
verified ·
1 Parent(s): 847696c

Update templates/cart.html

Browse files
Files changed (1) hide show
  1. templates/cart.html +30 -25
templates/cart.html CHANGED
@@ -138,7 +138,11 @@
138
  </div>
139
  </div>
140
  <div class="cart-item-actions">
141
- <div class="text-primary">${{ item.Price__c + item.Add_Ons_Price__c }}</div>
 
 
 
 
142
  <button class="btn btn-danger btn-sm" onclick="removeItemFromCart('{{ item.Name }}')">Remove</button>
143
  </div>
144
  </div>
@@ -172,58 +176,59 @@
172
  <script>
173
  // Example function to handle the increase/decrease button clicks
174
  function updateQuantity(action, itemName, customerEmail) {
175
- let quantity = parseInt(document.querySelector(`input[data-item-name="${itemName}"]`).value); // Get current quantity
 
176
  // Update quantity based on action
177
  if (action === 'increase') {
178
  quantity++;
179
  } else if (action === 'decrease' && quantity > 1) {
180
  quantity--;
181
  }
 
 
182
  if (isNaN(quantity) || quantity < 1) {
183
- console.error("Invalid quantity:", quantity);
184
- alert("Quantity must be a valid number greater than 0.");
185
  return;
186
- }
187
- console.log({
188
- email: customerEmail,
189
- item_name: itemName,
190
- quantity: quantity
191
- });
192
  fetch('/cart/update_quantity', {
193
  method: 'POST',
194
- headers: {
195
- 'Content-Type': 'application/json',
196
- },
197
- body: JSON.stringify({
198
- email: customerEmail, // Customer's email
199
- item_name: itemName, // Item name
200
- quantity: quantity // New quantity
201
- })
202
  })
203
  .then(response => response.json())
204
  .then(data => {
205
  if (data.success) {
 
 
 
 
 
 
206
  calculateSubtotal();
207
- document.querySelector(`input[data-item-name="${itemName}"]`).value = quantity;
208
  } else {
209
  alert("Error updating quantity: " + data.error);
210
  }
211
  })
212
- .catch(error => {
213
- console.error('Error:', error);
214
- });
215
  }
216
 
 
217
  function calculateSubtotal() {
218
  let subtotal = 0;
219
  document.querySelectorAll('.cart-item').forEach(item => {
220
  const quantity = parseInt(item.querySelector('input').value);
221
- const basePrice = parseFloat(item.querySelector('.base-price').innerText.replace('$', '')); // Base Price (Price__c)
222
- const addonsPrice = parseFloat(item.querySelector('.addons-price').innerText.replace('$', '')) || 0; // Add-ons Price (Add_Ons_Price__c)
223
- subtotal += (basePrice * quantity) + addonsPrice;
224
  });
 
 
 
225
  return subtotal;
226
  }
 
227
 
228
  function proceedToOrder() {
229
  fetch('/checkout', {
 
138
  </div>
139
  </div>
140
  <div class="cart-item-actions">
141
+ <div class="text-primary">
142
+ $<span class="base-price">{{ item.Price__c }}</span> +
143
+ $<span class="addons-price">{{ item.Add_Ons_Price__c }}</span>
144
+ </div>
145
+
146
  <button class="btn btn-danger btn-sm" onclick="removeItemFromCart('{{ item.Name }}')">Remove</button>
147
  </div>
148
  </div>
 
176
  <script>
177
  // Example function to handle the increase/decrease button clicks
178
  function updateQuantity(action, itemName, customerEmail) {
179
+ let quantityInput = document.querySelector(`input[data-item-name="${itemName}"]`);
180
+ let quantity = parseInt(quantityInput.value);
181
  // Update quantity based on action
182
  if (action === 'increase') {
183
  quantity++;
184
  } else if (action === 'decrease' && quantity > 1) {
185
  quantity--;
186
  }
187
+
188
+ // Validate quantity
189
  if (isNaN(quantity) || quantity < 1) {
190
+ alert("Invalid quantity!");
 
191
  return;
192
+ }
193
+
194
+ // Send updated quantity to the server
 
 
 
195
  fetch('/cart/update_quantity', {
196
  method: 'POST',
197
+ headers: { 'Content-Type': 'application/json' },
198
+ body: JSON.stringify({ email: customerEmail, item_name: itemName, quantity: quantity })
 
 
 
 
 
 
199
  })
200
  .then(response => response.json())
201
  .then(data => {
202
  if (data.success) {
203
+ // Update the item price and quantity in the UI
204
+ quantityInput.value = quantity;
205
+ let itemPriceElement = document.querySelector(`.cart-item[data-item-name="${itemName}"] .text-primary`);
206
+ itemPriceElement.innerHTML = `$<span class="base-price">${data.new_item_price}</span>`;
207
+
208
+ // Recalculate subtotal dynamically
209
  calculateSubtotal();
 
210
  } else {
211
  alert("Error updating quantity: " + data.error);
212
  }
213
  })
214
+ .catch(err => console.error("Error:", err));
 
 
215
  }
216
 
217
+
218
  function calculateSubtotal() {
219
  let subtotal = 0;
220
  document.querySelectorAll('.cart-item').forEach(item => {
221
  const quantity = parseInt(item.querySelector('input').value);
222
+ const basePrice = parseFloat(item.querySelector('.base-price').innerText.replace('$', '')); // Base Price
223
+ const addonsPrice = parseFloat(item.querySelector('.addons-price').innerText.replace('$', '')) || 0; // Add-ons Price
224
+ subtotal += (basePrice * quantity) + addonsPrice; // Include add-ons price in subtotal
225
  });
226
+
227
+ // Update the subtotal in the HTML
228
+ document.querySelector('.cart-summary p').innerText = `Subtotal: $${subtotal.toFixed(2)}`;
229
  return subtotal;
230
  }
231
+
232
 
233
  function proceedToOrder() {
234
  fetch('/checkout', {