Building a Multi-Step Checkout Wizard with Livewire 4
Multi-step checkout flows are deceptively fiddly to build well: each step needs its own validation, the user needs to be able to go back without losing what they typed, and a page refresh mid-flow shouldn't dump them back to step one. This tutorial builds a three-step checkout — shipping, payment, review — as a single Livewire 4 component with per-step validation.
Step 1: The component class
namespace App\Livewire;
use Livewire\Component;
use Livewire\Attributes\Validate;
class CheckoutWizard extends Component
{
public int $step = 1;
#[Validate('required|string|max:255')]
public string $shippingName = '';
#[Validate('required|string|max:20')]
public string $shippingZip = '';
#[Validate('required|in:card,paypal')]
public string $paymentMethod = '';
#[Validate('required_if:paymentMethod,card|nullable|digits:16')]
public string $cardNumber = '';
public function nextStep(): void
{
$this->validateStep();
$this->step++;
}
public function previousStep(): void
{
$this->step--;
}
protected function validateStep(): void
{
match ($this->step) {
1 => $this->validate([
'shippingName' => 'required|string|max:255',
'shippingZip' => 'required|string|max:20',
]),
2 => $this->validate([
'paymentMethod' => 'required|in:card,paypal',
'cardNumber' => 'required_if:paymentMethod,card|nullable|digits:16',
]),
default => null,
};
}
public function placeOrder(): void
{
$this->validateStep();
Order::create([
'user_id' => auth()->id(),
'shipping_name' => $this->shippingName,
'shipping_zip' => $this->shippingZip,
'payment_method' => $this->paymentMethod,
]);
$this->redirectRoute('checkout.confirmation');
}
public function render()
{
return view('livewire.checkout-wizard');
}
}
Validating per-step, rather than validating the whole set of properties at once, is what makes this feel like a wizard instead of one giant form — a typo in the payment step on step 3 doesn't block someone from advancing past shipping on step 1.
Step 2: The view, keyed by step
<div>
<div class="steps">
<span @class(['active' => $step === 1])>1. Shipping</span>
<span @class(['active' => $step === 2])>2. Payment</span>
<span @class(['active' => $step === 3])>3. Review</span>
</div>
@if ($step === 1)
<input type="text" wire:model="shippingName" placeholder="Full name">
@error('shippingName') <span class="error">{{ $message }}</span> @enderror
<input type="text" wire:model="shippingZip" placeholder="ZIP code">
@error('shippingZip') <span class="error">{{ $message }}</span> @enderror
<button wire:click="nextStep">Continue to payment</button>
@elseif ($step === 2)
<select wire:model="paymentMethod">
<option value="">Select a method</option>
<option value="card">Card</option>
<option value="paypal">PayPal</option>
</select>
@error('paymentMethod') <span class="error">{{ $message }}</span> @enderror
@if ($paymentMethod === 'card')
<input type="text" wire:model="cardNumber" placeholder="Card number">
@error('cardNumber') <span class="error">{{ $message }}</span> @enderror
@endif
<button wire:click="previousStep">Back</button>
<button wire:click="nextStep">Review order</button>
@else
<p>Shipping to {{ $shippingName }}, {{ $shippingZip }}</p>
<p>Paying with {{ $paymentMethod }}</p>
<button wire:click="previousStep">Back</button>
<button wire:click="placeOrder">Place order</button>
@endif
</div>
Step 3: Surviving a page refresh
By default, a hard refresh mid-checkout resets $step and every property back to their initial values, because Livewire's state lives in the rendered HTML and a fresh page load starts a fresh component. If you want the flow to survive a refresh (recommended — customers refresh mid-checkout more than you'd think), persist the step and form data to the session on each transition:
public function mount(): void
{
$this->step = session('checkout.step', 1);
$this->shippingName = session('checkout.shippingName', '');
$this->shippingZip = session('checkout.shippingZip', '');
$this->paymentMethod = session('checkout.paymentMethod', '');
}
public function nextStep(): void
{
$this->validateStep();
$this->step++;
$this->persistToSession();
}
protected function persistToSession(): void
{
session([
'checkout.step' => $this->step,
'checkout.shippingName' => $this->shippingName,
'checkout.shippingZip' => $this->shippingZip,
'checkout.paymentMethod' => $this->paymentMethod,
]);
}
Clear the session keys in placeOrder() once the order is actually created, so a completed checkout doesn't linger and pre-fill a customer's next purchase.
Testing it
it('walks through checkout and places an order', function () {
Livewire::test(CheckoutWizard::class)
->set('shippingName', 'Jamie Rivera')
->set('shippingZip', '94103')
->call('nextStep')
->assertSet('step', 2)
->set('paymentMethod', 'paypal')
->call('nextStep')
->assertSet('step', 3)
->call('placeOrder')
->assertRedirect(route('checkout.confirmation'));
expect(Order::count())->toBe(1);
});
One component, per-step validation, and session persistence gets you a checkout flow that feels considered rather than held together with $_SESSION calls scattered across three separate controller actions.