Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Livewire

Building a Shopping Cart with Livewire v4

Create a dynamic shopping cart component.

1 February 20261 min readLivewireاقرأ بالعربية

On this page

  1. Cart Component

Build a shopping cart with Livewire.

Cart Component

php
@php
new class extends Livewire\Component {
    public array $items = [];
    
    public function addItem(int $productId): void
    {
        $product = Product::find($productId);
        $this->items[$productId] = [
            'name' => $product->name,
            'price' => $product->price,
            'qty' => ($this->items[$productId]['qty'] ?? 0) + 1,
        ];
    }
    
    #[Computed]
    public function total(): float
    {
        return collect($this->items)->sum(fn($i) => $i['price'] * $i['qty']);
    }
}
@endphp
All articles

Keep reading

  • Authorization in Livewire v4 Components1 February 20261 min read
  • Blaze Pre-Compiler: Massive Performance Gains in Livewire v41 February 20261 min read
  • Building a Bookmark Feature with Livewire v41 February 20261 min read