Skip to content

Commit

Permalink
feat: Implement ShoppingCart Livewire component fo
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] committed Mar 11, 2024
1 parent 1ca198e commit 771c4f2
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions app/Http/Livewire/ShoppingCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Illuminate\Support\Facades\Session;

class ShoppingCart extends Component
{
public $items = [];

public function mount()
{
$this->items = Session::get('cart', []);
}

public function render()
{
return view('livewire.shopping-cart', ['items' => $this->items]);
}

public function addToCart($productId, $name, $price, $quantity = 1)
{
if (isset($this->items[$productId])) {
$this->items[$productId]['quantity'] += $quantity;
} else {
$this->items[$productId] = [
'name' => $name,
'price' => $price,
'quantity' => $quantity,
];
}

Session::put('cart', $this->items);
}

public function updateQuantity($productId, $quantity)
{
if (isset($this->items[$productId]) && $quantity > 0) {
$this->items[$productId]['quantity'] = $quantity;
Session::put('cart', $this->items);
}
}

public function removeItem($productId)
{
if (isset($this->items[$productId])) {
unset($this->items[$productId]);
Session::put('cart', $this->items);
}
}

public function clearCart()
{
$this->items = [];
Session::forget('cart');
}
}

0 comments on commit 771c4f2

Please sign in to comment.