In this part of the “Building a Book Selling website with laravel and Nuxt.js 3” series will handle cart checkout.
Table of Contents
- Preparing Project
- Connecting Laravel with Nuxt 3
- Adding Cart Functionality
- Checkout and Storing Orders
What will we build in this part?
- Building the cart checkout page
- Enter their contact and delivery information.
- Place an order.
- Simple administration module will be provided in the final source code.
Building the checkout page
Now let’s implement the checkout process.
Since we are using guest checkout, we only need a simple form asking the customer for:
- Full name.
- Email address.
- Phone number.
- Delivery address.
We will not build a login step at this stage
Create:
pages/checkout.vue
<script setup lang="ts">
const { cart, cartTotal, clearCart } = useCart()
const config = useRuntimeConfig()
const router = useRouter()
const loading = ref(false)
const cartLoading = ref(true)
const errorMessage = ref('')
const form = reactive({
customer_name: '',
email: '',
phone: '',
address: '',
})
async function placeOrder() {
if (cart.value.length === 0) {
return
}
loading.value = true
errorMessage.value = ''
try {
const response = await $fetch<any>(
`${config.public.apiBase}/orders`,
{
method: 'POST',
body: {
...form,
items: cart.value.map(item => ({
book_id: item.book_id,
quantity: item.quantity,
})),
},
}
)
// Empty the cart after successful checkout
clearCart()
// Navigate to confirmation page
await router.push(
`/order-success?id=${response.order.id}`
)
} catch (error: any) {
errorMessage.value =
error.data?.message || 'Unable to place your order.'
} finally {
loading.value = false
}
}
onMounted(() => {
cartLoading.value = false;
});
</script>
<template>
<div class="max-w-3xl mx-auto">
<h1 class="text-3xl font-bold mb-3">
Checkout
</h1>
<div v-if="cartLoading" class="text-center py-10">
<p class="mb-5">Loading cart...</p>
</div>
<div v-else-if="cart.length === 0 && !cartLoading" class="text-center py-10">
<p class="mb-5">Your cart is empty.</p>
<NuxtLink to="/books" class="text-indigo-700">
Browse Books
</NuxtLink>
</div>
<form v-else @submit.prevent="placeOrder" class="grid grid-cols-2 sm:grid-cols-1 gap-3">
<div class="border rounded-lg px-4 py-5 mb-6">
<h2 class="text-xl font-bold mb-5">
Delivery Information
</h2>
<div class="space-y-4">
<input
v-model="form.customer_name"
type="text"
placeholder="Full Name"
required
class="w-full border rounded-lg px-4 py-3"
/>
<input
v-model="form.email"
type="email"
placeholder="Email Address"
required
class="w-full border rounded-lg px-4 py-3"
/>
<input
v-model="form.phone"
type="tel"
placeholder="Phone Number"
required
class="w-full border rounded-lg px-4 py-3"
/>
<textarea
v-model="form.address"
placeholder="Delivery Address"
required
rows="4"
class="w-full border rounded-lg px-4 py-3"
/>
</div>
</div>
<!-- Order Summary -->
<div class="border rounded-lg px-4 py-5 mb-6">
<h2 class="text-xl font-bold mb-4">
Order Summary
</h2>
<div
v-for="item in cart"
:key="item.book_id"
class="flex justify-between mb-3"
>
<span>
{{ item.title }} × {{ item.quantity }}
</span>
<span>
${{ (item.price * item.quantity).toFixed(2) }}
</span>
</div>
<hr class="my-4" />
<div class="flex justify-between font-bold text-lg">
<span>Total</span>
<span>${{ cartTotal.toFixed(2) }}</span>
</div>
<p class="text-sm text-gray-500 mt-4">
Payment method: Cash on Delivery
</p>
</div>
<p v-if="errorMessage" class="text-red-600 mb-4">
{{ errorMessage }}
</p>
<button
type="submit"
:disabled="loading"
class="w-full bg-indigo-700 text-white py-3 rounded-lg
disabled:opacity-50"
>
{{ loading ? 'Placing Order...' : 'Place Order' }}
</button>
</form>
</div>
</template>
The checkout process is straightforward:
- The customer fills in the delivery information.
- Nuxt sends the customer details and book IDs with quantities to Laravel.
- Laravel validates the request and checks stock.
- Laravel calculates the order total using database prices.
- Laravel creates the order and order items.
- Laravel updates the available stock.
- Nuxt clears the cart and redirects the customer to the confirmation page.
If checkout fails, the cart remains intact so the customer can try again.
Creating the order confirmation page
After successfully placing an order, we redirect the customer to a confirmation page.
Create:
pages/order-success.vue
<script setup lang="ts">
const route = useRoute()
const orderId = computed(() => route.query.id)
</script>
<template>
<div class="max-w-xl mx-auto text-center py-16">
<div class="text-green-600 text-4xl mb-6">
✓
</div>
<h1 class="text-3xl font-bold mb-4">
Order Placed Successfully!
</h1>
<p class="text-gray-600 mb-4">
Thank you for shopping with us.
Your order has been received.
</p>
<p class="font-semibold mb-4">
Order Number: #{{ orderId }}
</p>
<NuxtLink
to="/books"
class="inline-block bg-indigo-700 text-white px-8 py-3 rounded-lg"
>
Continue Shopping
</NuxtLink>
</div>
</template>
We simply display the order reference. We don’t need another API endpoint to retrieve the order details in this tutorial.
Verify the database
After placing an order, check the orders table.
You should find something similar to:
|
id |
customer_name |
total |
status |
|---|---|---|---|
|
1 |
John Smith |
95.00 |
pending |
And in order_items:
|
order_id |
book_title |
quantity |
price |
|---|---|---|---|
|
1 |
Clean Code |
2 |
35.00 |
|
1 |
Rich Dad Poor Dad |
1 |
25.00 |
The stock quantities should also have been reduced accordingly.
Â
Source Code
In the below url is the full source code in the github repository. I added a simple management module to manage the books and categories and can be accessed using the /admin url

