|
@@ -871,10 +871,106 @@ async function updateCartCount() {
cartCountElements.forEach(element => {
element.textContent = result.count;
});
+
+
}
} catch (error) {
console.error('Error updating cart count:', error);
}
}
+
+// Function to calculate and update cart totals
+function calculateCartTotals() {
+ let subtotal = 0;
+ let itemCount = 0;
+
+ // Get all cart item rows
+ const cartRows = document.querySelectorAll('tbody tr[data-cart-id]');
+
+ console.log('Found cart rows:', cartRows.length);
+
+ cartRows.forEach(row => {
+ const cartId = row.dataset.cartId;
+ const priceInput = document.getElementById(`price_${cartId}`);
+ const quantityInput = row.querySelector('input[type="number"]');
+
+ console.log('Processing cart ID:', cartId, 'Price input:', priceInput, 'Quantity input:', quantityInput);
+
+ if (priceInput && quantityInput) {
+ const price = parseFloat(priceInput.value);
+ const quantity = parseInt(quantityInput.value);
+ subtotal += price * quantity;
+ itemCount += quantity;
+
+ console.log('Price:', price, 'Quantity:', quantity, 'Running subtotal:', subtotal);
+ }
+ });
+
+ console.log('Final subtotal:', subtotal, 'Item count:', itemCount);
+
+ // Update subtotal display
+ const subtotalElement = document.getElementById('cart-subtotal');
+ if (subtotalElement) {
+ subtotalElement.textContent = `Rp ${number_format(subtotal, 0, ',', '.')}`;
+ console.log('Updated subtotal element:', subtotalElement.textContent);
+ }
+
+ // Calculate estimated total (subtotal - savings + tax)
+ const savings = 0; // Fixed savings amount
+ const tax = 0; // Fixed tax amount
+ const estimatedTotal = subtotal - savings + tax;
+
+ // Show/hide tax row based on tax amount
+ const taxRow = document.getElementById('tax-row');
+ if (taxRow) {
+ if (tax > 0) {
+ taxRow.style.display = 'flex';
+ taxRow.querySelector('span').textContent = `Rp ${number_format(tax, 0, ',', '.')}`;
+ } else {
+ taxRow.style.display = 'none';
+ }
+ }
+
+ // Update estimated total display
+ const estimatedTotalElement = document.getElementById('cart-estimated-total');
+ if (estimatedTotalElement) {
+ estimatedTotalElement.textContent = `Rp ${number_format(estimatedTotal, 0, ',', '.')}`;
+ console.log('Updated estimated total element:', estimatedTotalElement.textContent);
+ }
+
+ // Update items count
+ const cartCalcElement = document.getElementById('cart-calc');
+ if (cartCalcElement) {
+ cartCalcElement.textContent = itemCount;
+ console.log('Updated item count:', cartCalcElement.textContent);
+ }
+}
+
+// Initialize cart totals on page load
+document.addEventListener('DOMContentLoaded', function() {
+ calculateCartTotals();
+});
+
+// Update cart totals when cart items are updated
+const originalUpdateCartItem = updateCartItem;
+window.updateCartItem = async function(cartId, action, button) {
+ await originalUpdateCartItem(cartId, action, button);
+
+ // Recalculate totals after successful update
+ setTimeout(() => {
+ calculateCartTotals();
+ }, 500);
+}
+
+// Update cart totals when cart items are deleted
+const originalDeleteCartItem = deleteCartItem;
+window.deleteCartItem = async function(event, form, cartId) {
+ await originalDeleteCartItem(event, form, cartId);
+
+ // Recalculate totals after successful deletion
+ setTimeout(() => {
+ calculateCartTotals();
+ }, 500);
+}
@endsection
diff --git a/resources/views/components/layout/header.blade.php b/resources/views/components/layout/header.blade.php
index 4bf6b22..b3ec196 100644
--- a/resources/views/components/layout/header.blade.php
+++ b/resources/views/components/layout/header.blade.php
@@ -134,7 +134,8 @@
@@ -317,39 +327,39 @@
diff --git a/resources/views/components/shop/add-to-cart.blade.php b/resources/views/components/shop/add-to-cart.blade.php
index 8737158..1266610 100644
--- a/resources/views/components/shop/add-to-cart.blade.php
+++ b/resources/views/components/shop/add-to-cart.blade.php
@@ -28,6 +28,33 @@
+
+
+
+
+
+
+
+
+
+ Please login to add items to cart
+ You need to be logged in to add items to your shopping cart and proceed with checkout.
+
+
+
+
+
+
|