Hi everyone,
I'm new to programming and have been working on a shopping cart feature for practice. My code allows users to select a quantity of an item, add it to the cart, adjust the quantity of items in the cart, and remove items from the cart.
Could someone please review my code and let me know if I implemented it correctly? I want to make sure I'm on the right track and not missing any important best practices. Any feedback or suggestions for improvement would be greatly appreciated!
Here's a summary of what the code does:
- Allows users to select the quantity of an item.
- Adds the selected item and quantity to the cart.
- Enables users to adjust the quantity of items in the cart.
- Allows users to remove items from the cart.
Thank you for your help!
index.blade.php
<body>
<livewire:cart-counter />
<livewire:products-table />
<livewire:cart-table />
</body>
cart-counter.blade.php
<div>
Cart ({{ $cart_count }})
</div>
product-table.blade.php
<div>
@forelse ($products as $product)
<p wire:key="{{ $product->id }}">
@if ($cart->where('id', $product->id)->count())
@foreach ($cart->where('id', $product->id) as $item)
<button wire:click="removeFromCart('{{ $item->rowId }}', '{{ $product->id }}')">
Remove for Cart
</button>
@endforeach
@else
<form
wire:submit="addToCart({{ $product->id }})"
>
<input
wire:model="quantity.{{ $product->id }}"
type="number"
>
<button type="submit">
Add to Cart
</button>
</form>
@endif
</p>
@endforelse
</div>
cart-table.blade.php
@foreach ($cart_content as $row)
<p wire:key="{{ $row->rowId }}">
<input
wire:click="changeQuantity('{{ $row->rowId }}', $event.target.value)"
type="number"
value="{{ $row->qty }}"
>
</p>
@endforeach
CartCounter.php
<?php
namespace App\Livewire;
use Livewire\Component;
use Livewire\Attributes\On;
use Gloudemans\Shoppingcart\Facades\Cart;
class CartCounter extends Component
{
#[On('cart_updated')]
public function render()
{
$cart_content = Cart::content();
$cart_count = Cart::content()->count();
return view('livewire.cart-counter', compact('cart_count', 'cart_content'));
}
}
?>
ProductTable.php
<?php
namespace App\Livewire;
use App\Models\Product;
use Livewire\Component;
use Livewire\Attributes\On;
use Livewire\WithPagination;
use Gloudemans\Shoppingcart\Facades\Cart;
class ProductsTable extends Component
{
use WithPagination;
public $quantity = [];
public $perpage = 9;
public function mount()
{
$this->initializeQuantities();
}
public function initializeQuantities()
{
$products = Product::paginate($this->perpage);
foreach ($products as $product) {
if (!isset($this->quantity[$product->id])) {
$this->quantity[$product->id] = 1;
}
}
}
public function removeFromCart($rowId, $product_id)
{
Cart::remove($rowId);
$this->quantity[$product_id] = 1;
$this->dispatch('cart_updated');
}
public function addToCart($product_id)
{
$product = Product::findOrFail($product_id);
Cart::add($product->id, $product->name, $this->quantity[$product_id], $product->price / 100);
$this->quantity[$product_id] = 1;
$this->dispatch('cart_updated');
}
#[On('cart_updated')]
public function render()
{
$products = Product::paginate($this->perpage);
$this->initializeQuantities();
$cart = Cart::content();
return view('livewire.products-table', compact('cart', 'products'));
}
}
?>
CartTable.php
<?php
namespace App\Livewire;
use Livewire\Component;
use Livewire\Attributes\On;
use Gloudemans\Shoppingcart\Facades\Cart;
class CartTable extends Component
{
public function changeQuantity($rowId, $quantity)
{
Cart::update($rowId, $quantity);
if ($quantity == 0) {
$this->dispatch('cart_updated');
}
}
#[On('cart_updated')]
public function render()
{
$cart_content = Cart::content();
$cart_subtotal = Cart::subtotal();
$cart_tax = Cart::tax();
$cart_total = Cart::total();
return view('livewire.cart-table', compact('cart_content', 'cart_subtotal', 'cart_tax', 'cart_total'));
}
}
?>