Laravel 11 E-Commerce - Checkout With Cash On Delivery

Welcome back to the Laravel E-Commerce Project Tutorial. In this video, we will learn about checking out with cash on delivery.

Step 1: Create a Function for Placing an Order

First, let's create a function in the CartController for placing an order:


public function place_order(Request $request)
{
$user_id = Auth::user()->id;

$address = Address::where('user_id',$user_id)->where('isdefault',true)->first();
if(!$address)
{
$request->validate([
'name' => 'required|max:100',
'phone' => 'required|numeric|digits:10',
'zip' => 'required|numeric|digits:6',
'state' => 'required',
'city' => 'required',
'address' => 'required',
'locality' => 'required',
'landmark' => 'required'
]);

$address = new Address();
$address->user_id = $user_id;
$address->name = $request->name;
$address->phone = $request->phone;
$address->zip = $request->zip;
$address->state = $request->state;
$address->city = $request->city;
$address->address = $request->address;
$address->locality = $request->locality;
$address->landmark = $request->landmark;
$address->country = '';
$address->isdefault = true;
$address->save();
}

$this->setAmountForCheckout();

$order = new Order();
$order->user_id = $user_id;
$order->subtotal = session()->get('checkout')['subtotal'];
$order->discount = session()->get('checkout')['discount'];
$order->tax = session()->get('checkout')['tax'];
$order->total = session()->get('checkout')['total'];
$order->name = $address->name;
$order->phone = $address->phone;
$order->locality = $address->locality;
$order->address = $address->address;
$order->city = $address->city;
$order->state = $address->state;
$order->country = $address->country;
$order->landmark = $address->landmark;
$order->zip = $address->zip;
$order->save();

foreach(Cart::instance('cart')->content() as $item)
{
$orderitem = new OrderItem();
$orderitem->product_id = $item->id;
$orderitem->order_id = $order->id;
$orderitem->price = $item->price;
$orderitem->quantity = $item->qty;
$orderitem->save();
}

$transaction = new Transaction();
$transaction->user_id = $user_id;
$transaction->order_id = $order->id;
$transaction->mode = $request->mode;
$transaction->status = "pending";
$transaction->save();

Cart::instance('cart')->destroy();
session()->forget('checkout');
session()->forget('coupon');
session()->forget('discounts');
return redirect()->route('cart.confirmation');
}

Pass an argument Request. Now, inside the function, get the logged-in user and fetch the default address of the logged-in user. Check if the address is not available, then validate the address input data.

Write the following code:

Now, create a new Address model object, set the properties' values, and save the address.

Step 2: Set the Amount for Checkout

Create another function here, setting the amount for checkout:


public function setAmountForCheckout()
{
if(!Cart::instance('cart')->count() > 0)
{
session()->forget('checkout');
return;
}

if(session()->has('coupon'))
{
session()->put('checkout',[
'discount' => session()->get('discounts')['discount'],
'subtotal' => session()->get('discounts')['subtotal'],
'tax' => session()->get('discounts')['tax'],
'total' => session()->get('discounts')['total']
]);
}
else
{
session()->put('checkout',[
'discount' => 0,
'subtotal' => Cart::instance('cart')->subtotal(),
'tax' => Cart::instance('cart')->tax(),
'total' => Cart::instance('cart')->total()
]);
}
}

Now, inside the placeOrder method, let's call this function:

Write `$this->setAmountForCheckout();`.

Step 3: Create a Route for the Function

Now, go to the web.php file and create a route:


Route::post('/place-order',[CartController::class,'place_order'])->name('cart.place.order');

Step 4: Create a Function for Order Confirmation

In CartController, let's create one more function for order confirmation:


public function confirmation()
{
return view('order-confirmation');
}

Step 5: Create a Route for the Order Confirmation Function

Now, let's create a route for this function. Go to the web.php file and create a new route:


Route::get('/order-confirmation',[CartController::class,'confirmation'])->name('cart.confirmation');

Step 6: Create the Order Confirmation View

Now, let's create this view. Go to the resources directory and create a new view, order-confirmation.blade.php file:

Now, extend the layout:


@extends('layouts.app')
@section('content')
@endsection

Step 7: Design the Order Confirmation Page

Now, go to the template directory and open the order-confirmation.html file in a text editor. Copy the main section from here and paste it inside the content section.

Simply display the order placed message and add the shop link as follows:


<div class="order-complete">
<div class="order-complete__message">
<svg width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle cx="40" cy="40" r="40" fill="#B9A16B" />
<path d="M52.9743 35.7612C52.9743 35.3426 52.8069 34.9241 52.5056 34.6228L50.2288 32.346C49.9275 32.0446 49.5089 31.8772 49.0904 31.8772C48.6719 31.8772 48.2533 32.0446 47.952 32.346L36.9699 43.3449L32.048 38.4062C31.7467 38.1049 31.3281 37.9375 30.9096 37.9375C30.4911 37.9375 30.0725 38.1049 29.7712 38.4062L27.4944 40.683C27.1931 40.9844 27.0257 41.4029 27.0257 41.8214C27.0257 42.24 27.1931 42.6585 27.4944 42.9598L33.5547 49.0201L35.8315 51.2969C36.1328 51.5982 36.5513 51.7656 36.9699 51.7656C37.3884 51.7656 37.8069 51.5982 38.1083 51.2969L40.385 49.0201L52.5056 36.8996C52.8069 36.5982 52.9743 36.1797 52.9743 35.7612Z" fill="white" />
</svg>
<h3>Your order is completed!</h3>
<p>Thank you. Your order has been received.</p>
<a href="{{route('shop.index')}}" class="btn btn-info mt-5">Shop More</a>
</div>
</div>

Now it's done! Let's check. Go to the shop page, add some products to the cart, and proceed to checkout. You can see the shipping address form because there is no address saved for this logged-in user. Enter the address and click on the place an order button. You can see the order confirmation message here.

In this way, you can checkout with cash on delivery.

That's all about checking out with cash on delivery.