From 156978e669026d2d784e706ba76c124e6580f539 Mon Sep 17 00:00:00 2001 From: Bayu Lukman Yusuf Date: Mon, 26 Jan 2026 08:44:25 +0700 Subject: [PATCH] fix cart --- app/Http/Controllers/CartController.php | 4 +- .../Member/Cart/MemberCartRepository.php | 4 +- resources/views/checkout/v1-cart.blade.php | 116 +++++++++- .../views/components/layout/header.blade.php | 208 +++++++++--------- .../components/shop/add-to-cart.blade.php | 84 ++++++- 5 files changed, 303 insertions(+), 113 deletions(-) diff --git a/app/Http/Controllers/CartController.php b/app/Http/Controllers/CartController.php index eb073c6..3fc803b 100644 --- a/app/Http/Controllers/CartController.php +++ b/app/Http/Controllers/CartController.php @@ -14,9 +14,9 @@ class CartController extends Controller public function count(Request $request, MemberCartRepository $repository) { $count = $repository->getCount($request->input('location_id')); - + return response()->json([ - 'count' => $count + 'count' => $count, ]); } diff --git a/app/Repositories/Member/Cart/MemberCartRepository.php b/app/Repositories/Member/Cart/MemberCartRepository.php index 62c1b9a..d401c40 100644 --- a/app/Repositories/Member/Cart/MemberCartRepository.php +++ b/app/Repositories/Member/Cart/MemberCartRepository.php @@ -13,9 +13,11 @@ class MemberCartRepository $location = $locationId ?? session('location_id', 22); return Cart::where('user_id', auth()->user()->id) ->where('location_id', $location) - ->count(); + ->sum('qty'); } + + public function getList($request) { $location = (int) $request->input("location_id"); diff --git a/resources/views/checkout/v1-cart.blade.php b/resources/views/checkout/v1-cart.blade.php index e5dbab5..73e7951 100644 --- a/resources/views/checkout/v1-cart.blade.php +++ b/resources/views/checkout/v1-cart.blade.php @@ -65,7 +65,7 @@ @foreach ($carts as $key => $cart) - +
+ href="{{ route('home') }}"> Continue shopping @@ -154,16 +154,16 @@
Order summary
@@ -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 @@ + + +