feat: add HasCart trait and ShareCartWithInertia

This commit is contained in:
Joel Male
2026-05-27 21:38:44 +10:00
parent a485bfb6f0
commit 0efd4378a8
4 changed files with 225 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Wearepixel\Cart\Concerns;
use Wearepixel\Cart\Cart;
trait HasCart
{
public array $cartItems = [];
public float $cartTotal = 0.0;
public float $cartSubTotal = 0.0;
public int $cartCount = 0;
protected function getCart(): Cart
{
return app('cart');
}
public function refreshCart(): void
{
$cart = $this->getCart();
$this->cartItems = $cart->getContent()->toArray();
$this->cartSubTotal = (float) $cart->getSubTotal(false);
$this->cartTotal = (float) $cart->getTotal();
$this->cartCount = (int) $cart->getTotalQuantity();
}
public function cartAdd(
int|string $id,
string $name,
float $price,
int $quantity = 1,
array $attributes = [],
): void {
$this->getCart()->add($id, $name, $price, $quantity, $attributes);
$this->refreshCart();
}
public function cartRemove(int|string $id): void
{
$this->getCart()->remove($id);
$this->refreshCart();
}
public function cartUpdate(int|string $id, array $data): void
{
$this->getCart()->update($id, $data);
$this->refreshCart();
}
public function cartClear(): void
{
$this->getCart()->clear();
$this->refreshCart();
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Wearepixel\Cart\Concerns;
use Wearepixel\Cart\Cart;
class ShareCartWithInertia
{
public static function data(?Cart $cart = null): array
{
$cart ??= app('cart');
return [
'items' => $cart->getContent()->toArray(),
'subtotal' => (float) $cart->getSubTotal(false),
'total' => (float) $cart->getTotal(),
'count' => (int) $cart->getTotalQuantity(),
];
}
}