Part 7: Checkout Process in Laravel E-Commerce
In this section of the Laravel E-Commerce Tutorial, we’ll focus on implementing the checkout process. The checkout process is a critical step that allows users to review their orders, provide shipping information, and complete their purchases.
7.1 Updating Cart and Order Models
Step 1: Update Cart Model
Update the Cart model (app/Models/Cart.php) to include a relationship with the Product model.
// app/Models/Cart.php
use App\Models\Product; class Cart extends Model { // Existing code... public function product() { return $this->belongsTo(Product::class); } }
Step 2: Create Order Model
Generate an Order model:
php artisan make:model Order -m
Edit the migration file to define the orders table schema:
// database/migrations/YYYY_MM_DD_create_orders_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrdersTable extends Migration { public function up() { Schema::create('orders', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('user_id'); $table->decimal('total_amount', 8, 2); $table->timestamps(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); }); } public function down() { Schema::dropIfExists('orders'); } }
Run the migration:
php artisan migrate
Define relationships in the Order model (app/Models/Order.php):
// app/Models/Order.php
use App\Models\User; use App\Models\Product; class Order extends Model { protected $fillable = ['user_id', 'total_amount']; public function user() { return $this->belongsTo(User::class); } public function products() { return $this->belongsToMany(Product::class)->withPivot('quantity'); } }
7.2 Implementing Checkout Controller
Step 1: Create Checkout Controller
Generate a controller for managing the checkout process:
php artisan make:controller CheckoutController
Define the necessary methods in the CheckoutController for displaying the checkout form and processing the order.
// app/Http/Controllers/CheckoutController.php
use Illuminate\Http\Request;
use App\Models\Order;
use App\Models\Cart;
class CheckoutController extends Controller
{
public function index()
{
// Display checkout form
}
public function placeOrder(Request $request)
{
// Process the order and redirect to thank you page
}
}
Step 2: Update Cart Controller
Update the CartController to handle the checkout process. When the user proceeds to checkout, you may want to move the items from the cart to the order for processing.
// app/Http/Controllers/CartController.php
use App\Models\Order;
use App\Models\Cart;
class CartController extends Controller
{
// Existing code…
public function checkout()
{
$user = Auth::user();
$cartItems = $user->cartItems;
// Create an order and associate products
$order = new Order([‘total_amount’ => 0]);
$user->orders()->save($order);
foreach ($cartItems as $cartItem) {
$order->products()->attach($cartItem->product_id, [‘quantity’ => $cartItem->quantity]);
$order->total_amount += ($cartItem->product->price * $cartItem->quantity);
$cartItem->delete();
}
$order->save();
return view(‘checkout.index’, compact(‘order’));
}
}
7.3 Implementing Checkout Views
Step 1: Create Checkout Views
Create Blade views for the checkout functionality:
resources/views/checkout/index.blade.php
: Display the checkout form.resources/views/checkout/thank-you.blade.php
: Display a thank you message after completing the order.
Step 2: Define Checkout Routes
Define routes for checkout management in routes/web.php:
use App\Http\Controllers\CheckoutController;
Route::get(‘/checkout’, [CheckoutController::class, ‘index’])->name(‘checkout.index’);
Route::post(‘/checkout/place-order’, [CheckoutController::class, ‘placeOrder’])->name(‘checkout.placeOrder’);
7.4 Implementing Checkout Functionality
Step 1: Update Checkout Views
Update the checkout views to display the order summary and allow users to provide shipping information.
{{-- resources/views/checkout/index.blade.php --}}
<form action="{{ route('checkout.placeOrder') }}" method="post">
@csrf
{{-- Shipping information fields --}}
<button type="submit">Place Order</button>
</form>
{{– resources/views/checkout/thank-you.blade.php –}}<p>Thank you for your order! Your order ID is: {{ $order->id }}</p>
<p>Total Amount: ${{ number_format($order->total_amount, 2) }}</p>
{{– Display other order details as needed –}}
Step 2: Update Checkout Controller
Update the CheckoutController to handle the checkout process. You may want to validate the shipping information, calculate the total amount, and perform any additional steps required for your application.
// app/Http/Controllers/CheckoutController.php
class CheckoutController extends Controller
{
// Existing code…
public function placeOrder(Request $request)
{
$request->validate([
// Add validation rules for shipping information
]);
// Perform additional steps (calculate total amount, apply discounts, etc.)
// Update the order with shipping information
$order = Order::find($request->order_id);
$order->update([
‘shipping_address’ => $request->shipping_address,
// Add other shipping information fields
]);
return view(‘checkout.thank-you’, compact(‘order’));
}
}
7.5 Summary
Congratulations! You’ve successfully implemented the checkout process in your Laravel E-Commerce application. Users can now review their orders, provide shipping information, and complete their purchases. In the next part of the tutorial, we’ll focus on implementing the order management system. Stay tuned for the next steps!