feat: add getTotalAmount method to Cart and refactor Purchase model to utilize it

This commit is contained in:
2026-07-06 15:26:41 -03:00
parent c19e00987e
commit 3703ae9bcf
3 changed files with 36 additions and 33 deletions

View File

@@ -59,6 +59,18 @@ class Cart extends Model
return $this->hasMany(CartItem::class, 'cart_id');
}
public function getTotalAmount(): float
{
$items = $this->relationLoaded('items')
? $this->getRelation('items')
: $this->items()->with('variant.product')->get();
return (float) $items->reduce(
fn (float $carry, $item): float => $carry + ((float) ($item->variant?->product?->precio ?? 0) * $item->cantidad),
0.0,
);
}
public function addItem(int $productVariantId, int $quantity): CartItem
{
if ($quantity <= 0) {