Skip to content

Commit

Permalink
Merge pull request #452 from liberu-ecommerce/sweep/Enhance-Shopping-…
Browse files Browse the repository at this point in the history
…Cart-Functionality-and-Inventory-Management

Enhance Shopping Cart Functionality and Inventory Management
  • Loading branch information
curtisdelicata authored Feb 15, 2025
2 parents 811e046 + 80cfd9f commit 97fc86e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 8 deletions.
41 changes: 41 additions & 0 deletions app/Http/Controllers/CartController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class CartController extends Controller
{
public function add(Request $request, Product $product)
{
$quantity = $request->input('quantity', 1);

if ($product->inventory_count < $quantity) {
return redirect()->back()->with('error', 'Not enough inventory available.');
}

$cart = Session::get('cart', []);

if (isset($cart[$product->id])) {
$cart[$product->id]['quantity'] += $quantity;
} else {
$cart[$product->id] = [
'name' => $product->name,
'price' => $product->price,
'quantity' => $quantity,
'is_downloadable' => $product->is_downloadable,
];
}

Session::put('cart', $cart);

return redirect()->back()->with('success', 'Product added to cart successfully!');
}

public function index()
{
return view('cart.index');
}
}
15 changes: 15 additions & 0 deletions app/Http/Controllers/CheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Notifications\LowStockNotification;
use Illuminate\Support\Facades\Notification;
use App\Factories\PaymentGatewayFactory;
use App\Models\Product; // Add this line to import the Product model

class CheckoutController extends Controller
{
Expand Down Expand Up @@ -66,6 +67,14 @@ public function processCheckout(Request $request)
->with('error', 'Your cart is empty');
}

// Verify inventory before processing
foreach ($cart as $productId => $item) {
$product = Product::find($productId);
if (!$product || $product->inventory_count < $item['quantity']) {
return redirect()->back()->with('error', 'Some items in your cart are no longer available in the requested quantity.');
}
}

// Calculate total amount
$subtotal = collect($cart)->sum(function($item) {
return $item['price'] * $item['quantity'];
Expand Down Expand Up @@ -110,6 +119,12 @@ public function processCheckout(Request $request)
}
}

// Update inventory after successful payment
foreach ($cart as $productId => $item) {
$product = Product::find($productId);
$product->decrement('inventory_count', $item['quantity']);
}

// Clear cart
Session::forget('cart');

Expand Down
14 changes: 13 additions & 1 deletion app/Http/Livewire/ShoppingCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ public function addToCart($productId, $name, $price, $quantity = 1)
{
$product = Product::findOrFail($productId);

// Verify inventory
if ($product->inventory_count < $quantity) {
session()->flash('error', 'Not enough inventory available.');
return;
}

if (isset($this->items[$productId])) {
$this->items[$productId]['quantity'] += $quantity;
$newQuantity = $this->items[$productId]['quantity'] + $quantity;
if ($newQuantity > $product->inventory_count) {
session()->flash('error', 'Cannot add more items than available in stock.');
return;
}
$this->items[$productId]['quantity'] = $newQuantity;
} else {
$this->items[$productId] = [
'name' => $name,
Expand All @@ -42,6 +53,7 @@ public function addToCart($productId, $name, $price, $quantity = 1)

Session::put('cart', $this->items);
$this->emit('cartUpdated');
session()->flash('success', 'Product added to cart successfully!');
}

public function hasPhysicalProducts()
Expand Down
14 changes: 10 additions & 4 deletions resources/views/products/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ class="bg-blue-100 text-blue-800 text-xs font-semibold px-2.5 py-0.5 rounded dar
<div class="flex items-center justify-between">
<span
class="text-3xl font-bold text-gray-900 dark:text-white">${{ number_format($product->price, 2) }}</span>
<a href="#"
class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Add
to cart</a>
<form action="{{ route('cart.add', $product) }}" method="POST" class="d-inline">
@csrf
<button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">
<svg class="-ms-2 me-2 h-5 w-5 inline" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4h1.5L8 16m0 0h8m-8 0a2 2 0 1 0 0 4 2 2 0 0 0 0-4Zm8 0a2 2 0 1 0 0 4 2 2 0 0 0 0-4Zm.75-3H7.5M11 7H6.312M17 4v6m-3-3h6" />
</svg>
Add to cart
</button>
</form>
</div>
</div>
</div>
Expand All @@ -85,4 +91,4 @@ class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none
</div>
</div>
</div>
@endsection
@endsection
10 changes: 10 additions & 0 deletions resources/views/products/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ class="form-control"
@if($product->inventory_count > 0)
<form action="{{ route('cart.add', $product) }}" method="POST" class="d-inline">
@csrf
<div class="flex items-center gap-2 mb-4">
<label for="quantity" class="text-sm font-medium">Quantity:</label>
<input type="number"
name="quantity"
id="quantity"
class="w-20 rounded border-gray-300"
value="1"
min="1"
max="{{ $product->inventory_count }}">
</div>
<button type="submit" class="btn btn-success mt-2">Add to Cart</button>
</form>
@endif
Expand Down
6 changes: 3 additions & 3 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Http\Controllers\SiteSettingController;
use App\Http\Controllers\StripePaymentController;
use App\Http\Controllers\SubscriptionController;
use App\Http\Controllers\CartController; // New controller for cart

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -87,9 +88,8 @@
Route::delete('/paypal/subscription/cancel', [PayPalPaymentController::class, 'cancelSubscription'])->name('paypal.subscription.cancel');

// Cart route
Route::get('/cart', function() {
return view('cart.index');
})->name('cart.index');
Route::post('/cart/add/{product}', [CartController::class, 'add'])->name('cart.add'); // New route
Route::get('/cart', [CartController::class, 'index'])->name('cart.index'); // New route

// Ratings and reviews
Route::get('/product/{product}/reviews', [ReviewController::class, 'show'])->name('reviews.show');
Expand Down

0 comments on commit 97fc86e

Please sign in to comment.